当前位置:网站首页>Unity Gobang Game Design and Simple AI (2)
Unity Gobang Game Design and Simple AI (2)
2022-08-09 06:01:00 【makise2333】
Above we have realized the basic functions of basic gobang playing, this time we will judge the winning and losing conditions of gobang.The logic is as follows: after each chess game, check the chess piece in 8 directions. If there are chess pieces of the same type, continue to judge along the chess pieces until there are 5 pieces.After introducing the basic idea, let's talk about how to implement it.
1 first checks a line of code: set an offset to offset, and two loops detect the positive and negative pawns of each store respectively.When num=5, stop the game and declare the game over.In the loop, pay attention to set, x, y respectively pos plus offset offset.The loop condition is that x and y are both less than 15 and greater than 0.Add an offset each time through the loop.(offset = move one space in a certain direction)
public void CheckOneLine(int[]pos, int[] offset){intNum = 1;for (int x = pos[0] + offset[0], y = pos[1] + offset[1]; (x<15&&x>0&&y<15&&y>0); x += offset[0], y +=offset[1]){if (grid[x, y] == (int)turn){Num += 1;}else break;}for (int x = pos[0] - offset[0], y = pos[1] - offset[1]; (x < 15 && x > 0 && y < 15 && y > 0); x -= offset[0], y -= offset[1]){if (grid[x, y] == (int)turn){Num += 1;}else break;}if (Num == 5){GameStart = false;Debug.Log("Game over");}}
2 The following code is for the detection of 8 directions of a chess piece. Since each detection already includes positive and negative 2 directions, then only four directions (left, up, diagonally upper right, diagonally upper left) need to be detected.That's it.
public void CheckWinner(int[] pos){CheckOneLine(pos, new int[2] { 0, 1 });CheckOneLine(pos, new int[2] { 1, 0 });CheckOneLine(pos, new int[2] { 1, 1 });CheckOneLine(pos, new int[2] { 1, -1 });}
3 Put the checkWinner method in the player's method of playing chess, pay attention to the position, and check the pieces after the grid is assigned a value.
public void Play(int[] pos){if (grid[pos[0], pos[1]]!=0||GameStart==false)return;if (turn == ChessType.Black){Instantiate(prefabs[0], new Vector3(pos[0], pos[1], 0), Quaternion.identity);grid[pos[0], pos[1]] = (int)ChessType.Black;CheckWinner(pos);turn = ChessType.White;}else if (turn == ChessType.White){Instantiate(prefabs[1], new Vector3(pos[0], pos[1], 0), Quaternion.identity);grid[pos[0], pos[1]] = (int)ChessType.White;CheckWinner(pos);turn = ChessType.Black;}
4 As shown in the figure below, we have completed the basic logic of Unity Gobang victory judgment.
5 The idea of repentance: We can create a new stack to save the position of each chess game. When we want to regret a chess move, we can directly pop the last chess piece from the stack and destroy it.The code is not posted here, and interested readers can try to complete the function of repentance by themselves.
If you have other ideas, you are welcome to discuss and exchange. There are not many things used in this article, it is very simple.At the end I will publish the source code of the complete project for readers' reference.
Gobang originated in China, one of the competitive events, is a pure strategy chess game for two players.The two sides use black and white chess pieces respectively, and place them on the intersection of the straight line and the horizontal line of the chessboard.
Link above: Unity Gobang function and simple AI implementation (1)
https://blog.csdn.net/makise2333/article/details/126069263
边栏推荐
猜你喜欢
shell正则表达式
一天学习一家上市公司:索菲亚
qt发送邮件程序
SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)
pytorch implements GAN entry case
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
Harbor Enterprise Mirror Warehouse Construction
5年测试开发工程师感悟——写给还在迷茫中的朋友
软件测试工程师简历要怎么写,才能让HR看到
磁性核壳四氧化三铁颗粒负载金纳米星|磁性Fe3O4-POSS-COOH|超顺磁四氧化三铁聚多巴胺核壳结构纳米粒子
随机推荐
剑指offer专项突击版第24天
shell正则表达式
LDO和DC-DC的区别
JMeter test - JMeter 】 【 upload multiple images/batch CSV file upload pictures interface parametric method
[mysql database] the use of mysql database
Domain name batch query online tool
X86汇编语言从实模式到保护模式练习题
一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
C语言如何给函数传二维数组?
MOS管的选型
打开数字时代的门槛,元宇宙NFT商城开发解决方案
找两个单身狗
【R语言】把文件夹下的所有文件提取到特定文件夹
Three Musketeers Advanced
1,C#的基本结构
【JMeter】jmeter测试 - 上传多个图片/批量上传图片接口 CSV文件参数化方法
牛客每日刷题之链表
Chapter7、基于图像的光照
5年测试开发工程师感悟——写给还在迷茫中的朋友
王爽 汇编语言个人疑问汇总第九篇