当前位置:网站首页>Unity五子棋游戏设计 和简单AI实现(1)
Unity五子棋游戏设计 和简单AI实现(1)
2022-08-09 05:56:00 【makise2333】
1 准备工作:
vs unity ,棋子,一张15*15的棋局.
我们先把准备好的图直接拖上来,将左下方的点设为(0,0),因此将中心点设为(7,7),由于图中的中心和图片pivot不完全吻合,所以会往下拖一点,但不影响。

2 代码部分
1 创建一个Player脚本和player的空物体,将脚本挂到player上 ,以下代码能够点击并且反馈点击在棋盘中的位置。
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 防止点击棋盘外并且为了玩家更好的体验(玩家可能点击到2个点的中间位置)对代码进行修改,pos需要各加0.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);
}
}3之后创建脚本gametable,创建int类型数组记录每次下棋的点。play函数负责创建棋子.turn负责判断黑子还是白子。
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在play脚本中判断下是否该自己下棋。
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;
}
}
}
6最后设置两个空物体player 将chesstype分别设置为white black就可以依次下棋了。

7判断5子胜负以及AI的实现放到下一篇继续。
总结:五子棋实现的问题主要在于如何分辨黑白子,记录每个棋子的位置,防止重复下棋,或者超出界外,控制每一步下棋的时间,防止秒下,以及一些bug的出现,优化下棋体验。
边栏推荐
- Xray-强大的漏洞扫描工具
- 著名数字藏品专家、数藏大会创始人高泽龙接受中国企业家杂志采访
- LDO和DC-DC的区别
- command,shell,raw,script模块的作用和区别,file、copy、fetch、synchronize的应用
- Three Musketeers Advanced
- Transaction rolled back because it has been marked as rollback-only
- kubernetes 安全
- 弄潮 Web3 欧易OKX全球「抢人」
- tidb 宕机测试
- The 24th day of the special assault version of the sword offer
猜你喜欢

RT201 国产PA射频功率放大器 兼容RFX2401C

The request was rejected because the URL contained a potentially malicious String “//“

牛客每日刷题之链表

deploy上传到私服配置注意事项(踩坑经验)

超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品

5G对物流行业的积极影响

第三章搜索与图论(一)

牛客每日刷题之链表

JVM:(六)运行时数据区之本地方法栈

Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
随机推荐
【mysql数据库】mysql数据库的使用
多字段关联校验
51 serial communication (on)
Lock wait timeout exceeded; try restarting transaction 更新数据量范围太大,导致锁表惨案
具有CT造影功能的硫化铋纳米棒|硫化铋-锌原卟啉复合材料(PAMAM/Bi2S3复合纳米粒子)
NFT协议OMNI因重入攻击损失1300ETH
#define
excel表格如何不需鼠标往下拖动而自动往下填
RT201 国产PA射频功率放大器 兼容RFX2401C
MySQL LIMIT + order by limit n,m 和 limit n的小坑
【Wwise】ArgumentException: The specified path is not of a legal form (empty).关于WwiseGlobal中的路径读取错误问题
多行字符串排序在线工具
什么是SIP请求和SIP响应?
RNN-T
2022-08-08 第四小组 修身课 学习笔记(every day)
Distributed timing task framework xxl-job source code analysis
二硫化钼/二氧化铪的复合纳米材料(MoS2/HfO2)|钽掺杂二氧化铪纳米颗粒(齐岳bio)
2022/08/08 学习笔记 (day25)File类
SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
一天学习一家上市公司:索菲亚