当前位置:网站首页>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.
边栏推荐
猜你喜欢
RNN-T
How to automatically fill down an excel table without dragging the mouse down
牛客每日刷题之链表
[MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
第三章搜索与图论(一)
[email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂"/>
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
excel表格如何不需鼠标往下拖动而自动往下填
手把手教你用C语言制作七夕流星雨---优雅永不过时(详细教程)
磁性核壳四氧化三铁颗粒负载金纳米星|磁性Fe3O4-POSS-COOH|超顺磁四氧化三铁聚多巴胺核壳结构纳米粒子
随机推荐
Domain name batch query online tool
harbor企业级镜像仓库搭建
直播源码开发,点击扫描跳转到扫码页面
直播平台怎么搭建,设置状态栏颜色、沉浸式状态栏
The difference between LDO and DC-DC
Chapter 3 Search and Graph Theory (1)
Online tool for sorting multi-line strings
MOS管的选型
牛客每日刷题之链表
Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
LDO和DC-DC的区别
直播电商平台开发,点击查看更多显示所有内容
untiy 倒计时
Spark源码-任务提交流程-8-DAGScheduler任务切分
S7-200SMART PLC Modbus TCP communication
SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
条件编译
【R语言】交互作用 测试数据
shell函数、数组