/
TicTacToeControl.cs
142 lines (101 loc) · 3.34 KB
/
TicTacToeControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using UnityEngine;
using System.Collections;
public class TicTacToeControl : MonoBehaviour {
public SquareState[] board = new SquareState[9];
public bool xTurn = true;
public GameState gameState = GameState.Opening;
public SquareState winner = SquareState.Clear;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnGUI() {
switch (gameState) {
case GameState.Opening:
DrawOpening();
break;
case GameState.MultiPlayer:
DrawGameBoard();
break;
case GameState.GameOver:
DrawGameOver();
break;
}
}
public void DrawOpening() {
Rect titleRect = new Rect(0,0,300,75);
GUI.Label(titleRect, "Tic-Tac-Toe");
Rect multiRect = new Rect(titleRect.x, titleRect.y + titleRect.height, titleRect.width, 75);
if (GUI.Button(multiRect, "New Game")) {
NewGame();
gameState = GameState.MultiPlayer;
}
}
public void DrawGameBoard()
{
float width = 75;
float height = 75;
for (int y=0; y<3; y++) {
for (int x=0; x<3; x++) {
int boardIndex = (y*3) + x;
Rect square = new Rect(x*width, y*height, width, height);
string owner = board[boardIndex] == SquareState.XControl ? "X" : board[boardIndex] == SquareState.OControl ? "O" : "";
if (board[boardIndex] == SquareState.Clear) {
if (GUI.Button(square,owner)) SetControl(boardIndex);
}
else GUI.Label(square, owner);
Rect turnRect = new Rect(300,0,100,100);
string turnTitle = xTurn ? "X's Turn!" : "O's Turn!";
GUI.Label(turnRect, turnTitle);
}
}
}
public void DrawGameOver() {
Rect winnerRect = new Rect(0,0,300,75);
string winnerTitle = winner == SquareState.XControl ? "X Wins!" : winner == SquareState.OControl ? "O Wins!" : "It's a Tie!";
GUI.Label(winnerRect, winnerTitle);
winnerRect.y += winnerRect.height;
if (GUI.Button(winnerRect, "Main Menu")) gameState = GameState.Opening;
}
public void NewGame() {
xTurn = true;
board = new SquareState[9];
}
public void SetControl(int boardIndex) {
if (boardIndex < 0 || boardIndex >= board.Length) return;
board[boardIndex] = xTurn ? SquareState.XControl : SquareState.OControl;
xTurn = !xTurn;
}
public void SetWinner(SquareState toWin) {
winner = toWin;
gameState = GameState.GameOver;
}
public void LateUpdate() {
if (gameState != GameState.MultiPlayer) return;
for(int i=0;i<3;i++) {
if(board[i] != SquareState.Clear && board[i] == board[i + 3] && board[i] == board[i + 6]) {
SetWinner(board[i]);
return;
}
else if(board[i * 3] != SquareState.Clear && board[i * 3] == board[(i * 3) + 1] && board[i * 3] == board[(i * 3) + 2]) {
SetWinner(board[i * 3]);
return;
}
}
if(board[0] != SquareState.Clear && board[0] == board[4] && board[0] == board[8]) {
SetWinner(board[0]);
return;
}
else if(board[2] != SquareState.Clear && board[2] == board[4] && board[2] == board[6]) {
SetWinner(board[2]);
return;
}
for(int i=0;i<board.Length;i++) {
if(board[i] == SquareState.Clear)
return;
}
SetWinner(SquareState.Clear);
}
}