⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkforbidden.cs

📁 人工智能 五子棋的实现。AI算法比较高
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace gobang
{
    class CheckForbidden
    {
        public const int NO_FORBIDDEN = 0;
        public const int THREE_THREE_FORBIDDEN = 1;
        public const int FOUR_FOUR_FORBIDDEN = 2;
        public const int LONG_FORBIDDEN = 3;

        private const int NONE = 0;
        private const int BLACK = 1;
        private const int WHITE = 2;

        public int fobiddenCheck(int[][] chessboard, int x, int y)
        {
            int[] adjsame = new int[8];
            int[] adjempty = new int[8];
            int[] jumpsame = new int[8];
            int[] jumpempty = new int[8];
            int[] jumpjumpsame = new int[8];

            for (int i = 0; i < 8; i++)
            {
                adjsame[i] = adjempty[i] = jumpempty[i] = jumpjumpsame[i] = jumpsame[i] = 0;
            }

            chessboard[x][y] = BLACK;
            int _x, _y;

            for (_y = y - 1; _y >= 0 && chessboard[x][_y] == BLACK; _y--, adjsame[0]++) ;
            for (; _y >= 0 && chessboard[x][_y] == NONE; _y--, adjempty[0]++) ;
            for (; _y >= 0 && chessboard[x][_y] == BLACK; _y--, jumpsame[0]++) ;
            for (; _y >= 0 && chessboard[x][_y] == NONE; _y--, jumpempty[0]++) ;
            for (; _y >= 0 && chessboard[x][_y] == BLACK; _y--, jumpjumpsame[0]++) ;

            for (_x = x + 1, _y = y - 1; _x < 15 && _y >= 0 && chessboard[_x][_y] == BLACK; _x++, _y--, adjsame[1]++) ;
            for (; _x < 15 && _y >= 0 && chessboard[_x][_y] == NONE; _x++, _y--, adjempty[1]++) ;
            for (; _x < 15 && _y >= 0 && chessboard[_x][_y] == BLACK; _x++, _y--, jumpsame[1]++) ;
            for (; _x < 15 && _y >= 0 && chessboard[_x][_y] == NONE; _x++, _y--, jumpempty[1]++) ;
            for (; _x < 15 && _y >= 0 && chessboard[_x][_y] == BLACK; _x++, _y--, jumpjumpsame[1]++) ;

            for (_x = x + 1; _x < 15 && chessboard[_x][y] == BLACK; _x++, adjsame[2]++) ;
            for (; _x < 15 && chessboard[_x][y] == NONE; _x++, adjempty[2]++) ;
            for (; _x < 15 && chessboard[_x][y] == BLACK; _x++, jumpsame[2]++) ;
            for (; _x < 15 && chessboard[_x][y] == NONE; _x++, jumpempty[2]++) ;
            for (; _x < 15 && chessboard[_x][y] == BLACK; _x++, jumpjumpsame[2]++) ;

            for (_x = x + 1, _y = y + 1; _x < 15 && _y < 15 && chessboard[_x][_y] == BLACK; _x++, _y++, adjsame[3]++) ;
            for (; _x < 15 && _y < 15 && chessboard[_x][_y] == NONE; _x++, _y++, adjempty[3]++) ;
            for (; _x < 15 && _y < 15 && chessboard[_x][_y] == BLACK; _x++, _y++, jumpsame[3]++) ;
            for (; _x < 15 && _y < 15 && chessboard[_x][_y] == NONE; _x++, _y++, jumpempty[3]++) ;
            for (; _x < 15 && _y < 15 && chessboard[_x][_y] == BLACK; _x++, _y++, jumpjumpsame[3]++) ;

            for (_y = y + 1; _y < 15 && chessboard[x][_y] == BLACK; _y++, adjsame[4]++) ;
            for (; _y < 15 && chessboard[x][_y] == NONE; _y++, adjempty[4]++) ;
            for (; _y < 15 && chessboard[x][_y] == BLACK; _y++, jumpsame[4]++) ;
            for (; _y < 15 && chessboard[x][_y] == NONE; _y++, jumpempty[4]++) ;
            for (; _y < 15 && chessboard[x][_y] == BLACK; _y++, jumpjumpsame[4]++) ;

            for (_x = x - 1, _y = y + 1; _x >=0 && _y < 15 && chessboard[_x][_y] == BLACK; _x--, _y++, adjsame[5]++) ;
            for (; _x >= 0 && _y < 15 && chessboard[_x][_y] == NONE; _x--, _y++, adjempty[5]++) ;
            for (; _x >= 0 && _y < 15 && chessboard[_x][_y] == BLACK; _x--, _y++, jumpsame[5]++) ;
            for (; _x >= 0 && _y < 15 && chessboard[_x][_y] == NONE; _x--, _y++, jumpempty[5]++) ;
            for (; _x >= 0 && _y < 15 && chessboard[_x][_y] == BLACK; _x--, _y++, jumpjumpsame[5]++) ;

            for (_x = x - 1; _x >= 0 && chessboard[_x][y] == BLACK; _x--, adjsame[6]++) ;
            for (; _x >= 0 && chessboard[_x][y] == NONE; _x--, adjempty[6]++) ;
            for (; _x >= 0 && chessboard[_x][y] == BLACK; _x--, jumpsame[6]++) ;
            for (; _x >= 0 && chessboard[_x][y] == NONE; _x--, jumpempty[6]++) ;
            for (; _x >= 0 && chessboard[_x][y] == BLACK; _x--, jumpjumpsame[6]++) ;

            for (_x = x - 1, _y = y - 1; _x >= 0 && _y >= 0 && chessboard[_x][_y] == BLACK; _x--, _y--, adjsame[7]++) ;
            for (; _x >= 0 && _y >= 0 && chessboard[_x][_y] == NONE; _x--, _y--, adjempty[7]++) ;
            for (; _x >= 0 && _y >= 0 && chessboard[_x][_y] == BLACK; _x--, _y--, jumpsame[7]++) ;
            for (; _x >= 0 && _y >= 0 && chessboard[_x][_y] == NONE; _x--, _y--, jumpempty[7]++) ;
            for (; _x >= 0 && _y >= 0 && chessboard[_x][_y] == BLACK; _x--, _y--, jumpjumpsame[7]++) ;

            chessboard[x][y] = NONE;

            for (int i = 0; i < 4; i++)
            {
                if (adjsame[i] + adjsame[i + 4] == 4)
                    return NO_FORBIDDEN;
            }

            int threecount = 0, fourcount = 0;

            for (int i = 0; i < 4; i++)
            {
                if (adjsame[i] + adjsame[i + 4] >= 5)
                    return LONG_FORBIDDEN;
                else if (adjsame[i] + adjsame[i + 4] == 3)
                {
                    bool isFour = false;
                    if (adjempty[i] > 0)
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i], i) == NO_FORBIDDEN)
                            isFour = true;
                    }

                    if (adjempty[i + 4] > 0)
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i+4], i+4) == NO_FORBIDDEN)
                            isFour = true;
                    }
                    if (isFour)
                        fourcount++;
                }
                else if (adjsame[i] + adjsame[i + 4] == 2)
                {
                    if (adjempty[i] == 1 && jumpsame[i] == 1)
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i], i) == NO_FORBIDDEN)
                            fourcount++;
                    }
                    if (adjempty[i + 4] == 1 && jumpsame[i + 4] == 1)
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i + 4], i + 4) == NO_FORBIDDEN)
                            fourcount++;
                    }

                    bool isThree = false;
                    if ((adjempty[i] > 2 || adjempty[i] == 2 && jumpsame[i] == 0) && (adjempty[i + 4] > 1 || adjempty[i + 4] == 1 && jumpsame[i + 4] == 0))
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i], i) == NO_FORBIDDEN)
                            isThree = true;
                    }
                    if ((adjempty[i + 4] > 2 || adjempty[i + 4] == 2 && jumpsame[i + 4] == 0) && (adjempty[i] > 1 || adjempty[i] == 1 && jumpsame[i] == 0))
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i + 4], i + 4) == NO_FORBIDDEN)
                            isThree = true;
                    }
                    if (isThree)
                        threecount++;
                }
                else if (adjsame[i] + adjsame[i + 4] == 1)
                {
                    if (adjempty[i] == 1 && jumpsame[i] == 2)
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i], i) == NO_FORBIDDEN)
                            fourcount++;
                    }
                    if (adjempty[i + 4] == 1 && jumpsame[i + 4] == 2)
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i + 4], i + 4) == NO_FORBIDDEN)
                            fourcount++;
                    }

                    if (adjempty[i] == 1 && jumpsame[i] == 1 && (jumpempty[i] > 1 || jumpempty[i] == 1 && jumpjumpsame[i] == 0)
                        && (adjempty[i + 4] > 1 || adjempty[i + 4] == 1 && jumpsame[i + 4] == 0))
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i], i) == NO_FORBIDDEN)
                            threecount++;
                    }
                    if (adjempty[i + 4] == 1 && jumpsame[i + 4] == 1 && (jumpempty[i + 4] > 1 || jumpempty[i + 4] == 1 && jumpjumpsame[i + 4] == 0)
                        && (adjempty[i] > 1 || adjempty[i] == 1 && jumpsame[i] == 0))
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i + 4], i + 4) == NO_FORBIDDEN)
                            threecount++;
                    }
                }
                else if (adjsame[i] + adjsame[i + 4] == 0)
                {
                    if (adjempty[i] == 1 && jumpsame[i] == 3)
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i], i) == NO_FORBIDDEN)
                            fourcount++;
                    }
                    if (adjempty[i + 4] == 1 && jumpsame[i + 4] == 3)
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i + 4], i + 4) == NO_FORBIDDEN)
                            fourcount++;
                    }

                    if (adjempty[i] == 1 && jumpsame[i] == 2 && (jumpempty[i] > 1 || jumpempty[i] == 1 && jumpjumpsame[i] == 0)
                        && (adjempty[i + 4] > 1 || adjempty[i + 4] == 1 && jumpsame[i + 4] == 0))
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i], i) == NO_FORBIDDEN)
                            threecount++;
                    }
                    if (adjempty[i + 4] == 1 && jumpsame[i + 4] == 2 && (jumpempty[i + 4] > 1 || jumpempty[i + 4] == 1 && jumpjumpsame[i + 4] == 0)
                        && (adjempty[i] > 1 || adjempty[i] == 1 && jumpsame[i] == 0))
                    {
                        if (keyPointForbiddenCheck(chessboard, x, y, adjsame[i + 4], i + 4) == NO_FORBIDDEN)
                            threecount++;
                    }
                }
            }
            if (fourcount > 1)
                return FOUR_FOUR_FORBIDDEN;
            if (threecount > 1)
                return THREE_THREE_FORBIDDEN;
            return NO_FORBIDDEN;
        }

        private int keyPointForbiddenCheck(int[][] chessboard, int x, int y, int adjsame, int direction)
        {
            int i, j;
            adjsame++;
            if (direction >= 4)
                adjsame = -adjsame;

            switch (direction % 4)
            {
                case 0:
                    i = x;
                    j = y - adjsame;
                    break;
                case 1:
                    i = x + adjsame;
                    j = y - adjsame;
                    break;
                case 2:
                    i = x + adjsame;
                    j = y;
                    break;
                default:
                    i = x + adjsame;
                    j = y + adjsame;
                    break;
            }

            chessboard[x][y] = BLACK;
            chessboard[i][j] = BLACK;
            int result = fobiddenCheck(chessboard, i, j);
            chessboard[x][y] = NONE;
            chessboard[i][j] = NONE;

            return result;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -