当前位置:网站首页>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 });}
边栏推荐
猜你喜欢
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
5年测试开发工程师感悟——写给还在迷茫中的朋友
GNNExplainer applied to node classification task
条件编译
第三章搜索与图论(一)
关于如何查找NXP S32K1xx系列单片机的封装信息和引脚定义
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
How to automatically fill down an excel table without dragging the mouse down
磁性核壳四氧化三铁颗粒负载金纳米星|磁性Fe3O4-POSS-COOH|超顺磁四氧化三铁聚多巴胺核壳结构纳米粒子
【微信小程序】5分钟实现微信小程序海报
随机推荐
Unity backgammon game design and simple AI implementation (1)
域名批量查询在线工具
command,shell,raw,script模块的作用和区别,file、copy、fetch、synchronize的应用
How to automatically fill down an excel table without dragging the mouse down
【mysql数据库】mysql数据库的使用
Initials-Letter Query Tool-Word Abbreviation Query Online Tool
LDO和DC-DC的区别
Regular Expression - Determine if a string matches the "AABB" pattern
[R language] Extract all files under a folder to a specific folder
直播平台怎么搭建,设置状态栏颜色、沉浸式状态栏
5G对物流行业的积极影响
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
shell regular expressions
[MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
Xray-强大的漏洞扫描工具
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
phpstudy 安装 flarum论坛
Unity 五子棋游戏设计和简单AI(3)
RT201 国产PA射频功率放大器 兼容RFX2401C