当前位置:网站首页>Unity Gobang Game Design and Simple AI(3)

Unity Gobang Game Design and Simple AI(3)

2022-08-09 06:02:00 makise2333

After completing the basic victory logic of Gobang, we will start the AI ​​design this time.The main idea is to use the greedy algorithm to evaluate the function to design a Gobang AI.

AI design idea: traverse each position on the chessboard, and calculate the score of each position to obtain the "optimal solution" on the surface. For example, a position can achieve both a live three and a block.The opposite pawn, then its score is higher than the score of a live three alone.We can refer to the previous code for detecting the five sons for the way of calculating the score.

1 Code: The "*" in the code refers to the piece _ means that the position is empty.Check whether there are the same type around the pieces in turn, and then pick the one with the highest score as the return value.

 public int CheckOneLine(int[] pos, int[] offest , int chessColor ){string temp = "*";for (int x = pos[0] + offest[0], y = pos[1] + offest[1], rem=0; (x < 15 && x > 0) && (y > 0 && y < 15)&&rem<=5; x=x+ offest[0], y += offest[1],rem++){if (ChessBoard.Instance.grid[x, y] == (int)chessColor){temp += "*";}else if (ChessBoard.Instance.grid[x, y] == 0){temp += "_";}else break;}for (int x = pos[0] - offest[0], y = pos[1] - offest[1],rem=0; (x < 15 && x > 0) && (y > 0 && y < 15)&&rem<=5; x -= offest[0], y -= offest[1],rem++){if (ChessBoard.Instance.grid[x, y] == (int)chessColor){temp = "*" + temp;}else if (ChessBoard.Instance.grid[x, y] == 0){temp = "_" + temp;break;}else break;}int MaxScore = 0;foreach (var item in toScore)//returns a maximum value{if (temp.Contains((item.Key)) && toScore[item.Key] > MaxScore){MaxScore = toScore[item.Key];}}return MaxScore;}

2 Here we use the dictionary as the storage of scores and chess records. At the beginning of initialization, readers of scores and chess records can adjust and add them by themselves.

Dictionary Score = new Dictionary();private void Start(){Score.Add("_*_", 20);Score.Add("_**_", 120);Score.Add("_**", 50);Score.Add("**_", 50);Score.Add("_***_", 720);Score.Add("_***", 600);Score.Add("***_", 600);Score.Add("****_", 720);Score.Add("**_**_", 720);Score.Add("**_*_", 720);Score.Add("_*_**", 720);Score.Add("_****_", 5000);Score.Add("*****", 100000);}

3 Calculate the total score, pay attention to not only calculate the score of your own pieces, but also calculate the score of the opponent in order to defend.

 int SetScore(int[] pos)//Calculate the total score{int score = 0;score += CheckOneLine(pos, new int[] { 1, 0 },1);score += CheckOneLine(pos, new int[] { 0, 1 },1);score += CheckOneLine(pos, new int[] { 1, 1 },1);score += CheckOneLine(pos, new int[] { 1, -1 },1);//Calculate the total score of Baiziscore += CheckOneLine(pos, new int[] { 1, 0 },2);score += CheckOneLine(pos, new int[] { 0, 1 },2);score += CheckOneLine(pos, new int[] { 1, 1 },2);score += CheckOneLine(pos, new int[] { 1, -1 },2);//Calculate the total score of sunspotsreturn score;}

4 Finally, traverse the board to get the point with the highest score, pay attention to some details, and our Gobang AI is complete!

 public override void PlayChess(){if (chessType != gametable.Instance.turn) return;int MaxX=7,MaxY=7, MaxScore = 80;if (gametable .Instance.grid[MaxX, MaxY] != 0) MaxScore = 0;for (int x = 0; x <= 14; x++){for (int y = 0; y <= 14; y++){if (gametable.Instance.grid[x, y] != 0) continue;int score = SetScore(new int[] { x, y });if (score > MaxScore){MaxX = x;MaxY = y;MaxScore = score;}}}gametable.Instance.Play(new int[] { MaxX, MaxY });}

原网站

版权声明
本文为[makise2333]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090556395032.html