当前位置:网站首页>Unity backgammon game design and simple AI implementation (1)
Unity backgammon game design and simple AI implementation (1)
2022-08-09 06:01:00 【makise2333】
1 准备工作:
vs unity ,棋子,一张15*15的棋局.
Let's drag the prepared picture directly up,Set the lower left point to (0,0),So set the center point to (7,7),Because of the center in the figure and the picturepivot不完全吻合,So drag it down a bit,但不影响.
2 代码部分
1 创建一个Player脚本和player的空物体,将脚本挂到player上 ,The following code can click and give feedback on the position of the click on the chessboard.
void Update()
{
PlayChess();
}
void PlayChess()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log((int)pos.x+" "+(int)pos.y);
}
}
2 Prevents clicking outside the board and for a better player experience(Players may click to2the middle of a point)对代码进行修改,posneed to add0.5.
void PlayChess()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (pos.x > 14 || pos.x < 0 || pos.y > 14 || pos.y < 0) return;
pos.x += (float) 0.5;pos.y += (float)(0.5);
Debug.Log((int)pos.x+" "+(int)pos.y);
}
}
3Create the script afterwardsgametable,创建intA typed array records the points of each chess game.playThe function is responsible for creating the pieces.turnResponsible for judging black or white.
public class gametable : MonoBehaviour
{
public int[,] grid;
public ChessType turn;
public GameObject[] prefabs;
private static gametable instance;
public static gametable Instance
{
get
{
return instance;
}
}
private void Awake()
{
grid = new int[15,15];
if (instance == null)
{
instance = this;
}
}
private void Update()
{
}
public void Play(int[] pos)
{
grid = new int[15, 15];
if (grid[pos[0], pos[1]]!=0)return;
if (turn == ChessType.Black)
{
Instantiate(prefabs[0], new Vector3(pos[0], pos[1], 0), Quaternion.identity);
turn = ChessType.White;
}
else if (turn == ChessType.White)
{
Instantiate(prefabs[1], new Vector3(pos[0], pos[1], 0), Quaternion.identity);
turn = ChessType.Black;
}
}
public enum ChessType
{
Watch,
White,
Black
}
}
4设置timer时间控制器,以及在grid中记录位置.
public class gametable : MonoBehaviour
{
public int[,] grid;
public ChessType turn;
public GameObject[] prefabs;
private static gametable instance;
public float timer=0;
public static gametable Instance
{
get
{
return instance;
}
}
private void Awake()
{
grid = new int[15,15];
if (instance == null)
{
instance = this;
}
}
private void Update()
{
timer += Time.deltaTime;
}
public void Play(int[] pos)
{
if (grid[pos[0], pos[1]]!=0)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;
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;
turn = ChessType.Black;
}
}
public enum ChessType
{
Watch,
White,
Black
}
}
5在playThe script determines whether it is time to play chess by yourself.
public class Player : MonoBehaviour
{
public gametable.ChessType chessType;
void Update()
{
if(gametable.Instance.timer>0.3f)
PlayChess();
}
void PlayChess()
{
if (Input.GetMouseButtonDown(0))
{
Vector2 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (pos.x > 14 || pos.x < 0 || pos.y > 14 ||
pos.y<0||chessType!=gametable.Instance.turn) return;
pos.x += (float) 0.5;pos.y += (float)(0.5);
Debug.Log((int)pos.x+" "+(int)pos.y);
gametable.Instance.Play(new int[2] { (int)pos.x, (int)pos.y });
gametable.Instance.timer = 0;
}
}
}
6Finally set two empty objectsplayer 将chesstype分别设置为white blackYou can play chess in turn.
7判断5Sub wins and losses as wellAIThe implementation will be continued in the next article.
总结:The main problem of Gobang implementation lies in how to distinguish black and white pieces,Record the position of each piece,Prevent repeated chess play,Or out of bounds,Control the timing of each move,Prevent seconds,以及一些bug的出现,Optimize the experience of playing chess.
边栏推荐
- 【Wwise】ArgumentException: The specified path is not of a legal form (empty).关于WwiseGlobal中的路径读取错误问题
- S7-200SMART PLC Modbus TCP communication
- 深度学习-神经网络原理2
- 数据中台项目前期总结
- Kubernetes apparmor profile
- VScode安装了ESlint以后不管用、不生效的解决办法
- 聚酰胺-胺(PAMAM)树形聚合物-硫化铋复合纳米粒子|硫化铋修饰Gd‑DTPA‑OA配体|科研实验用
- 分布式定时任务框架 xxl-job 源码解析
- Transaction rolled back because it has been marked as rollback-only
- 【深度学习】聊一聊什么是卷积神经网络,卷积是什么意思?
猜你喜欢
[email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂"/>
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
5G对物流行业的积极影响
带头双向循环链表的增删查改(C语言实现)
二硫化钼/二氧化铪的复合纳米材料(MoS2/HfO2)|钽掺杂二氧化铪纳米颗粒(齐岳bio)
2022-08-08:给定一个数组arr,表示从早到晚,依次会出现的导弹的高度。 大炮打导弹的时候,如果一旦大炮定了某个高度去打,那么这个大炮每次打的高度都必须下降一点。 1) 如果只有一个大炮,返回
JVM:(六)运行时数据区之本地方法栈
mysql查看表的创建时间
JMeter test - JMeter 】 【 upload multiple images/batch CSV file upload pictures interface parametric method
Xray - powerful vulnerability scanning tools
随机推荐
Open the threshold of the digital age, Metaverse NFT mall develops solutions
cglib获取不到接口注解
超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
feof它可不简单。
Transaction rolled back because it has been marked as rollback-only
记一个 nest.js 路由匹配后面所有路径问题
Getting Started with MATLAB Image Processing
kubernetes 安全
弄潮 Web3 欧易OKX全球「抢人」
正则表达式-判断字符串是否匹配“AABB”模式
筑牢安全“防火墙”河南通许县冯庄乡开展消防培训
Regular Expression - Determine if a string matches the "AABB" pattern
phpstudy 安装 flarum论坛
牛客每日刷题之链表
声母-字母查询工具-词语缩写查询在线工具
5G对物流行业的积极影响
shell函数、数组
JMeter test - JMeter 】 【 upload multiple images/batch CSV file upload pictures interface parametric method
Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
[GO]、数组与切片