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

📄 computer.cs

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

namespace gobang
{
    class Computer
    {
        /**/
        /// <summary>
        /// 正东正西检测与m,n点棋子相同的棋子个数
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <param name="arrchessboard"></param>
        /// <returns>如果返回负值则表示改方向在无子可下</returns>
        /// 

        private const int NONE = 0;
        private const int BLACK = 1;
        private const int WHITE = 2;
        public const int win = 1;
        public const int liveFour = 2;
        public const int deadFour = 3;
        public const int liveThree = 4;
        public const int deadThree = 5;
        public const int liveTwo = 6;
        public const int deadTwo = 7;
        public const int liveOne = 8;
        public const int deadOne = 9;
        public bool checkWin(int x, int y, int[][] arrchessboard, chess_color color)
        {
            int[] adjsame = new int[8];
            for (int i = 0; i < 8; i++)
            {
                adjsame[i] = 0;
            }
            int _x, _y;

            for (_y = y - 1; _y >= 0 && arrchessboard[x][_y] == (int)color; _y--, adjsame[0]++) ;

            for (_x = x + 1, _y = y - 1; _x < 15 && _y >= 0 && arrchessboard[_x][_y] == (int)color; _x++, _y--, adjsame[1]++) ;

            for (_x = x + 1; _x < 15 && arrchessboard[_x][y] == (int)color; _x++, adjsame[2]++) ;

            for (_x = x + 1, _y = y + 1; _x < 15 && _y < 15 && arrchessboard[_x][_y] == (int)color; _x++, _y++, adjsame[3]++) ;

            for (_y = y + 1; _y < 15 && arrchessboard[x][_y] == (int)color; _y++, adjsame[4]++) ;

            for (_x = x - 1, _y = y + 1; _x >= 0 && _y < 15 && arrchessboard[_x][_y] == (int)color; _x--, _y++, adjsame[5]++) ;

            for (_x = x - 1; _x >= 0 && arrchessboard[_x][y] == (int)color; _x--, adjsame[6]++) ;

            for (_x = x - 1, _y = y - 1; _x >= 0 && _y >= 0 && arrchessboard[_x][_y] == (int)color; _x--, _y--, adjsame[7]++) ;
            for (int i = 0; i < 4; i++)
            {
                if (adjsame[i] + adjsame[i + 4] >= 4)
                    return true;
            }
            return false;
        }
        private int[] fenXi(int x, int y, int[][] arrchessboard, chess_color color)
        {
            int[] adjsame = new int[8];
            int[] adjempty = new int[8];
            int[] jumpsame = new int[8];
            int[] jumpempty = new int[8];
            int[] res = new int[4];

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

            int _x, _y;

            for (_y = y - 1; _y >= 0 && arrchessboard[x][_y] == (int)color; _y--, adjsame[0]++) ;
            for (; _y >= 0 && arrchessboard[x][_y] == NONE; _y--, adjempty[0]++) ;
            for (; _y >= 0 && arrchessboard[x][_y] == (int)color; _y--, jumpsame[0]++) ;
            for (; _y >= 0 && arrchessboard[x][_y] == NONE; _y--, jumpempty[0]++) ;

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

            for (_x = x + 1; _x < 15 && arrchessboard[_x][y] == (int)color; _x++, adjsame[2]++) ;
            for (; _x < 15 && arrchessboard[_x][y] == NONE; _x++, adjempty[2]++) ;
            for (; _x < 15 && arrchessboard[_x][y] == (int)color; _x++, jumpsame[2]++) ;
            for (; _x < 15 && arrchessboard[_x][y] == NONE; _x++, jumpempty[2]++) ;

            for (_x = x + 1, _y = y + 1; _x < 15 && _y < 15 && arrchessboard[_x][_y] == (int)color; _x++, _y++, adjsame[3]++) ;
            for (; _x < 15 && _y < 15 && arrchessboard[_x][_y] == NONE; _x++, _y++, adjempty[3]++) ;
            for (; _x < 15 && _y < 15 && arrchessboard[_x][_y] == (int)color; _x++, _y++, jumpsame[3]++) ;
            for (; _x < 15 && _y < 15 && arrchessboard[_x][_y] == NONE; _x++, _y++, jumpempty[3]++) ;

            for (_y = y + 1; _y < 15 && arrchessboard[x][_y] == (int)color; _y++, adjsame[4]++) ;
            for (; _y < 15 && arrchessboard[x][_y] == NONE; _y++, adjempty[4]++) ;
            for (; _y < 15 && arrchessboard[x][_y] == (int)color; _y++, jumpsame[4]++) ;
            for (; _y < 15 && arrchessboard[x][_y] == NONE; _y++, jumpempty[4]++) ;

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

            for (_x = x - 1; _x >= 0 && arrchessboard[_x][y] == (int)color; _x--, adjsame[6]++) ;
            for (; _x >= 0 && arrchessboard[_x][y] == NONE; _x--, adjempty[6]++) ;
            for (; _x >= 0 && arrchessboard[_x][y] == (int)color; _x--, jumpsame[6]++) ;
            for (; _x >= 0 && arrchessboard[_x][y] == NONE; _x--, jumpempty[6]++) ;

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

            for (int i = 0; i < 4; i++)
            {
                if (adjsame[i] + adjsame[i + 4] >= 4)
                {
                    res[i] = win;
                }
                else if (adjsame[i] + adjsame[i + 4] == 3)
                {
                    if (adjempty[i] != 0 && adjempty[i + 4] != 0)
                        res[i] = liveFour;
                    else if (adjempty[i] != 0 || adjempty[i + 4] != 0)
                        res[i] = deadFour;
                }
                else if ((adjsame[i] + adjsame[i + 4] + jumpsame[i] > 2 && adjempty[i] == 1) || (adjsame[i + 4] + adjsame[i] + jumpsame[i + 4] > 2 && adjempty[i + 4] == 1))
                {
                    res[i] = deadFour;
                }
                else if (adjsame[i] + adjsame[i + 4] == 2)
                {
                    if (adjempty[i] != 0 && adjempty[i + 4] != 0 && (adjempty[i] + adjempty[i + 4] > 2))
                        res[i] = liveThree;
                    else if ((adjempty[i] != 0 || adjempty[i + 4] != 0) && (adjempty[i] + adjempty[i + 4] >= 2))
                        res[i] = deadThree;
                }
                else if ((adjsame[i] + adjsame[i + 4] + jumpsame[i] > 1 && adjempty[i] == 1 && ((jumpempty[i] != 0 && adjsame[i + 4] != 0)))
                    || (adjsame[i + 4] + adjsame[i] + jumpsame[i + 4] > 1 && adjempty[i + 4] == 1 && ((jumpempty[i + 4] != 0 || adjsame[i] != 0))))
                {
                    res[i] = liveThree;
                }
                else if ((adjsame[i] + adjsame[i + 4] + jumpsame[i] > 1 && adjempty[i] == 1 && (!(jumpempty[i] == 0 || adjsame[i + 4] == 0)))
                    || (adjsame[i + 4] + adjsame[i] + jumpsame[i + 4] > 1 && adjempty[i + 4] == 1 && (!(jumpempty[i + 4] == 0 || adjsame[i] == 0))))
                {
                    res[i] = deadThree;
                }
                else if (adjsame[i] + adjsame[i + 4] == 1)
                {
                    if (adjempty[i] != 0 && adjempty[i + 4] != 0 && (adjempty[i] + adjempty[i + 4] > 3))
                        res[i] = liveTwo;
                    else if ((adjempty[i] != 0 || adjempty[i + 4] != 0) && (adjempty[i] + adjempty[i + 4] >= 3))
                        res[i] = deadTwo;
                }
                else if (adjsame[i] + adjsame[i + 4] == 0)
                {
                    if (adjempty[i] != 0 && adjempty[i + 4] != 0 && (adjempty[i] + adjempty[i + 4] > 4))
                        res[i] = liveOne;
                    else if ((adjempty[i] != 0 || adjempty[i + 4] != 0) && (adjempty[i] + adjempty[i + 4] >= 4))
                        res[i] = deadOne;
                }
            }

            return res;
        }
        public int evaluatePre(int x, int y, int[][] arrchessboard, chess_color color)
        {
            int[] my = new int[4];
            int[] mm = new int[12];
            int res = 0;
            my = fenXi(x, y, arrchessboard, color);
            for (int i = 0; i < 4; i++)
            {
                mm[i] = 0;
            }
            for (int i = 0; i < 4; i++)
            {
                mm[my[i]]++;
            }
            while (mm[win] > 0)
            {
                res = int.MaxValue - 10;
                return res;
            }
            if (mm[liveFour] > 0)
            {
                res += 15656250;
            }
            else if (mm[deadFour] > 1)
            {
                res += 9765625;
            }
            else if (mm[deadFour] > 0 && mm[liveThree] > 0)
            {
                res += 9765625;
            }
            else if (mm[liveThree] > 1)
            {
                res += 1953125;
            }
            else if (mm[deadThree] > 0 && mm[liveThree] > 0)
            {
                res += 390625;
            }
            else if (mm[liveThree] > 0)
            {
                res += 78125;
            }
            else if (mm[deadFour] > 0)
            {
                res += 15625;
            }
            else if (mm[liveTwo] > 1)
            {
                res += 3125;
            }
            else if (mm[deadThree] > 0)
            {
                res += 625;
            }
            else if (mm[liveTwo] > 0)
            {
                res += 125;
                mm[liveTwo]--;
            }
            else if (mm[deadTwo] > 0)
            {
                res += 25;
            }
            else if (mm[liveOne] > 0)
            {
                res += 5;
                mm[liveOne]--;
            }
            else if (mm[deadOne] > 0)
            {
                res += 3;
                mm[deadOne]--;
            }
            return res;
        }
        public int evaluate(int x, int y, int[][] arrchessboard, chess_color color)
        {
            int temp;
            int res = 0; 
            arrchessboard[x][y] = (int)color;
            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    if (arrchessboard[i][j] != 0)
                    {
                        if (arrchessboard[i][j] == (int)color)
                        {
                            temp = evaluatePre(i, j, arrchessboard, color);
                            if (temp == int.MaxValue - 10)
                                return temp;
                            else res += temp;
                        }
                        else
                        {
                            temp = evaluatePre(i, j, arrchessboard, (color == chess_color.Chess_Clr_Black) ? chess_color.Chess_Clr_White : chess_color.Chess_Clr_Black);
                            if (temp == int.MaxValue - 10)
                                return int.MinValue + 10;
                            else res -= 3 * temp;
                        }
                    }
                }
            }
            arrchessboard[x][y] = 0;
            return res;
        }
    }
}

⌨️ 快捷键说明

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