游戏实现
首先搭建游戏菜单界面:
GUIStyle fontStyle = new GUIStyle()
{
fontSize = 25
};
fontStyle.normal.textColor = new Color(255, 255, 255);
GUIStyle fontStyle1 = new GUIStyle()
{
fontSize = 30
};
fontStyle1.normal.textColor = new Color(255, 255, 255);
GUI.Label(new Rect(413, 50, 100, 50), "井字游戏", fontStyle1);
if(gamestate == GameState.end)
{
if (GUI.Button(new Rect(400, 200, 140, 50), "Player vs Player"))
{
gamestate = GameState.mode1;
isWin = false;
}
if (GUI.Button(new Rect(400, 280, 140, 50), "Player vs Computer"))
{
gamestate = GameState.mode2;
isWin = false;
}
}
if(gamestate == GameState.mode1)
{
FixedUI(fontStyle);
bool full = true;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i, j] == 1)
{
GUI.Button(new Rect(350 + i * 80, 100 + j * 80, 80, 80), "X");
}
else if(board[i, j] == 2)
{
GUI.Button(new Rect(350 + i * 80, 100 + j * 80, 80, 80), "O");
}
else
{
full = false;
}
//如果空白格子被点击
if (GUI.Button(new Rect(350 + i * 80, 100 + j * 80, 80, 80), ""))
{
if(!isWin)
{
if (click == 1) //X的回合
{
board[i, j] = 1; //棋盘下X
}
if (click == -1) //O的回合
{
board[i, j] = 2; //棋盘下O
}
click = -click;
}
}
}
}
bool CheckGo(int pos_x,int pos_y,int mod) //检查AI和玩家是否有获胜机会
{
int piecesNum = 0;
for (int i = 0; i < 3; i++)
{
piecesNum = 0;
bool empty = false;
for (int j = 0; j < 3; j++)
{
if (board[i, j] == mod)
{
piecesNum++;
}
else if (board[i, j] == 0)
{
pos_x = i;
pos_y = j;
empty = true;
}
}
if (piecesNum == 2 && empty && board[pos_x,pos_y] == 0)
{
board[pos_x, pos_y] = 1;
click = -click;
return true;
}
}
for (int i = 0; i < 3; i++)
{
piecesNum = 0;
bool empty = false;
for (int j = 0; j < 3; j++)
{
if (board[j, i] == mod)
{
piecesNum++;
}
else if (board[j, i] == 0)
{
pos_x = j;
pos_y = i;
empty = true;
}
}
if (piecesNum == 2 && empty && board[pos_x, pos_y] == 0)
{
board[pos_x, pos_y] = 1;
click = -click;
return true;
}
}
piecesNum = 0;
bool empty1 = false;
for (int i = 0; i < 3; i++)
{
if (board[i, i] == mod)
{
piecesNum++;
}
else if (board[i, i] == 0)
{
pos_x = i;
pos_y = i;
empty1 = true;
}
}
if (piecesNum == 2 &&empty1 && board[pos_x, pos_y] == 0)
{
board[pos_x, pos_y] = 1;
click = -click;
return true;
}
piecesNum = 0;
empty1 = false;
for (int i = 0; i < 3; i++)
{
int j = 2 - i;
if (board[i, j] == mod)
{
piecesNum++;
}
else if (board[i, j] == 0)
{
pos_x = i;
pos_y = j;
empty1 = true;
}
}
if (piecesNum == 2 && empty1 && board[pos_x, pos_y] == 0)
{
board[pos_x, pos_y] = 1;
click = -click;
return true;
}
return false;
}
if (click == 1 && !isWin) //AI的回合
{
int a = 1, b = 2, c = 0; //1代表检测X是否有取胜机会,2代表检测O是否有取胜机会
if (!CheckGo(c, c, a))
{
if (!CheckGo(c, c, b))
{
int pos_x, pos_y;
System.Random ran = new System.Random();
pos_x = ran.Next(0, 2);
pos_y = ran.Next(0, 2);
int count = 0;
while (board[pos_x, pos_y] != 0 && Check() == 0)
{
pos_x = ran.Next(0, 2);
pos_y = ran.Next(0, 2);
count++;
if(count == 5)
{
FindEmpty();
break;
}
}
if(count != 5)
{
board[pos_x, pos_y] = 1;
click = -click;
}
}
}
}
void FixedUI(GUIStyle fontStyle) //固定不变的UI
{
int result = Check();
if (result == 1)
{
GUI.Label(new Rect(430, 350, 100, 50), "X wins!", fontStyle);
isWin = true;
}
else if (result == 2)
{
GUI.Label(new Rect(430, 350, 100, 50), "O wins!", fontStyle);
isWin = true;
}
if (GUI.Button(new Rect(420, 390, 100, 50), "Reset"))
{
Reset();
isWin = false;
}
if (GUI.Button(new Rect(420, 450, 100, 50), "Return"))
{
Reset();
gamestate = GameState.end;
isWin = false;
}
if (!isWin && click == 1)
{
GUI.Label(new Rect(430, 350, 100, 50), "X turn", fontStyle);
}
if (!isWin && click == -1)
{
GUI.Label(new Rect(430, 350, 100, 50), "O turn", fontStyle);
}
}
void Reset()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
board[i, j] = 0; //每一个小格子都没有棋子
}
最后实现结果如图
完整代码见github地址:井字棋