zoukankan      html  css  js  c++  java
  • 五子棋算法(c#)

    思路

    野路子实现,完全自己想出来的实现方式,将棋盘转成一维数组,寻找横向、纵向、左斜、右斜元素下标的关系。
    横向:元素之间下标相差1;
    纵向:元素之间下标相差棋盘宽度;
    右斜:元素之间下标相差宽度+1;
    左斜:元素之间下标相差宽度-1;

    客户端

    没有写界面,先用测试数据

    public static void Main(string[] args)
            {
                //棋盘测试数据
                int[] qipanArr = new int[] {
                1, 0, 1, 1, 1, 2, 0, 0,
                0, 1, 1, 1, 0, 2, 0, 1,
                0, 0, 1, 0, 0, 2, 1, 1,
                0, 1, 1, 1, 0, 1, 0, 0,
                1, 0, 1, 1, 1, 2, 0, 0,
                0, 0, 0, 0, 0, 1, 0, 0,
                0, 0, 0, 1, 0, 1, 0, 0,
                2, 2, 2, 2, 2, 1, 0, 0,};
                int index = 56;//落子位置
    
                bool isWin = Check(index, qipanArr);
                if (isWin)
                {
                    Console.WriteLine($"{Enum.GetName(typeof(EType), (EType)qipanArr[index])} win!");
                }
                else
                {
                    Console.WriteLine("继续落子");
                }
                Console.ReadKey();
            }
    //输出: 黑子 win!
    

    检查逻辑

            static int width = 8;//可以设置此值,代表棋盘的长宽
            static int length = width * width;
            static int HengxiangIncr = 1;
            static int ZongxiangIncr = width;
            static int RigthIncr = width + 1;
            static int LeftIncr = width - 1;
            static bool Check(int index, int[] qipanArr)
            {
                //检查横向
                bool isWin = CheckHengxiang(index, qipanArr);
                //检查纵向
                if (!isWin)
                {
                    isWin = CheckZongxiang(index, qipanArr);
                }
                //检查右斜
                if (!isWin)
                {
                    isWin = CheckRight(index, qipanArr);
                }
                //检查左斜
                if (!isWin)
                {
                    isWin = CheckLeft(index, qipanArr);
                }
                return isWin;
            }
            /// <summary>
            /// 检查横向
            /// </summary>
            /// <param name="index"></param>
            /// <param name="qipanArr"></param>
            /// <returns></returns>
            static bool CheckHengxiang(int index, int[] qipanArr)
            {
                int oriIndex = index;
                int oriRow = indexRowNumerDict[index];
                int matchCount = 1;//匹配数量
                int type = qipanArr[index];
                //检查横向
                int cycleCount = 0;
                while (cycleCount++ < 5)
                {
                    index -= HengxiangIncr;
                    if (index < 0 || index >= length)
                    {
                        break;
                    }
                    if (indexRowNumerDict[index] != oriRow)
                    {
                        break;
                    }
                    if (qipanArr[index] != type)
                    {
                        break;
                    }
                    if (++matchCount == 5)
                    {
                        break;
                    }
                }
                if (matchCount < 5)
                {
                    index = oriIndex;
                    while (cycleCount++ < 5)
                    {
                        if (indexRowNumerDict[index] != oriRow)
                        {
                            break;
                        }
                        index += HengxiangIncr;
                        if (index < 0 || index >= length)
                        {
                            break;
                        }
                        if (qipanArr[index] != type)
                        {
                            break;
                        }
                        if (++matchCount == 5)
                        {
                            break;
                        }
                    }
                }
                return matchCount == 5 ? true : false;
            }
            /// <summary>
            /// 检查纵向
            /// </summary>
            /// <param name="index"></param>
            /// <param name="qipanArr"></param>
            /// <returns></returns>
            static bool CheckZongxiang(int index, int[] qipanArr)
            {
                int oriIndex = index;
                int oriRow = indexRowNumerDict[index];
                int matchCount = 1;//匹配数量
                int type = qipanArr[index];
                int cycleCount = 0;
                while (cycleCount++ < 5)
                {
                    index -= ZongxiangIncr;
                    if (index < 0 || index >= length)
                    {
                        break;
                    }
                    if (indexRowNumerDict[index] + cycleCount != oriRow)
                    {
                        break;
                    }
                    if (qipanArr[index] != type)
                    {
                        break;
                    }
                    if (++matchCount == 5)
                    {
                        break;
                    }
                }
                if (matchCount < 5)
                {
                    index = oriIndex;
                    cycleCount = 0;
                    while (cycleCount++ < 5)
                    {
                        index += ZongxiangIncr;
                        if (index < 0 || index >= length)
                        {
                            break;
                        }
                        if (indexRowNumerDict[index] - cycleCount != oriRow)
                        {
                            break;
                        }
                        if (qipanArr[index] != type)
                        {
                            break;
                        }
                        if (++matchCount == 5)
                        {
                            break;
                        }
                    }
                }
                return matchCount == 5 ? true : false;
            }
            /// <summary>
            /// 检查右斜
            /// </summary>
            /// <param name="index"></param>
            /// <param name="qipanArr"></param>
            /// <returns></returns>
            static bool CheckRight(int index, int[] qipanArr)
            {
                int oriIndex = index;
                int oriRow = indexRowNumerDict[index];
                int matchCount = 1;//匹配数量
                int type = qipanArr[index];
                int cycleCount = 0;
                while (cycleCount++ < 5)
                {
                    index -= RigthIncr;
                    if (index < 0 || index >= length)
                    {
                        break;
                    }
                    if (indexRowNumerDict[index] + cycleCount != oriRow)
                    {
                        break;
                    }
                    if (qipanArr[index] != type)
                    {
                        break;
                    }
                    if (++matchCount == 5)
                    {
                        break;
                    }
                }
                if (matchCount < 5)
                {
                    index = oriIndex;
                    cycleCount = 0;
                    while (cycleCount++ < 5)
                    {
                        index += RigthIncr;
                        if (index < 0 || index >= length)
                        {
                            break;
                        }
                        if (indexRowNumerDict[index] - cycleCount != oriRow)
                        {
                            break;
                        }
                        if (qipanArr[index] != type)
                        {
                            break;
                        }
                        if (++matchCount == 5)
                        {
                            break;
                        }
                    }
                }
                return matchCount == 5 ? true : false;
            }
            /// <summary>
            /// 检查左斜
            /// </summary>
            /// <param name="index"></param>
            /// <param name="qipanArr"></param>
            /// <returns></returns>
            static bool CheckLeft(int index, int[] qipanArr)
            {
                int oriIndex = index;
                int oriRow = indexRowNumerDict[index];
                int matchCount = 1;//匹配数量
                int type = qipanArr[index];
                int cycleCount = 0;
                while (cycleCount++ < 5)
                {
                    index -= LeftIncr;
                    if (index < 0 || index >= length)
                    {
                        break;
                    }
                    if (indexRowNumerDict[index] + cycleCount != oriRow)
                    {
                        break;
                    }
                    if (qipanArr[index] != type)
                    {
                        break;
                    }
                    if (++matchCount == 5)
                    {
                        break;
                    }
                }
                if (matchCount < 5)
                {
                    index = oriIndex;
                    cycleCount = 0;
                    while (cycleCount++ < 5)
                    {
                        index += LeftIncr;
                        if (index < 0 || index >= length)
                        {
                            break;
                        }
                        if (indexRowNumerDict[index] - cycleCount != oriRow)
                        {
                            break;
                        }
                        if (qipanArr[index] != type)
                        {
                            break;
                        }
                        if (++matchCount == 5)
                        {
                            break;
                        }
                    }
                }
                return matchCount == 5 ? true : false;
            }
    

    枚举:

        public enum EType
        {
            白子 = 1,
            黑子 = 2
        }
    
  • 相关阅读:
    HDU 4289 Control [网络流拆点]
    HDU 4292 Food [网络流]
    HDU 4284 Travel [TSP]
    HDU 4291 A Short problem [矩阵]
    通过100个单词掌握英语语法(四十一)make
    通过100个单词掌握英语语法(四十二)may
    通过100个单词掌握英语语法(四十六)much
    通过100个单词掌握英语语法(四十七)my
    通过100个单词掌握英语语法(四十八)need
    通过100个单词掌握英语语法(四十五)most
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/13283600.html
Copyright © 2011-2022 走看看