当前位置:网站首页>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
边栏推荐
- MOS管的选型
- The request was rejected because the URL contained a potentially malicious String “//“
- How to automatically fill down an excel table without dragging the mouse down
- Unity 五子棋游戏设计和简单AI(3)
- harbor企业级镜像仓库搭建
- 金仓数据库能否设置事务自动提交
- 2022-08-08 顾宇佳 学习笔记
- phpstudy install flarum forum
- 【JMeter】jmeter测试 - 上传多个图片/批量上传图片接口 CSV文件参数化方法
- 【Wwise】ArgumentException: The specified path is not of a legal form (empty).关于WwiseGlobal中的路径读取错误问题
猜你喜欢
随机推荐
qt send mail program
[GO]、数组与切片
pytorch implements GAN entry case
【Word】Add subscripts to the text of Word at the same time
Lock wait timeout exceeded; try restarting transaction 更新数据量范围太大,导致锁表惨案
list 字符串的输出方法 print(*a) print(““.join(str(c) for c in a) )
【Wwise】ArgumentException: The specified path is not of a legal form (empty).关于WwiseGlobal中的路径读取错误问题
Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
deploy上传到私服配置注意事项(踩坑经验)
Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
shell正则表达式
JVM:(六)运行时数据区之本地方法栈
分布式定时任务框架 xxl-job 源码解析
RNN-T
tidb crash test
Online tool for sorting multi-line strings
王爽 汇编语言个人疑问汇总第九篇
RT201 Domestic PA RF Power Amplifier Compatible with RFX2401C
LDO和DC-DC的区别
契约测试(上):什么是契约测试