五子棋算法(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
    }
posted @ 2020-07-11 14:59  .Neterr  阅读(531)  评论(0编辑  收藏  举报