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的出现,优化下棋体验。