-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
292 lines (235 loc) · 8.08 KB
/
TicTacToe.java
File metadata and controls
292 lines (235 loc) · 8.08 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package tictactoe;
import java.util.Arrays;
public class TicTacToe {
private boolean gameOver;
private boolean xTurn;
private int emptySpaces;
private char[][] gameboard;
private int inARowToWin;
private String result;
public TicTacToe() {
}
public void createInitializedBoard(char[] input) {
gameboard = new char[3][3];
this.inARowToWin = 3;
emptySpaces = 0;
for (int i = 0; i < input.length; i++) {
char ch = input[i];
if (ch == '_' || ch == ' ') {
emptySpaces++;
}
gameboard[i / 3][i % 3] = ch;
}
xTurn = (gameboard.length * gameboard.length - emptySpaces) % 2 == 0 ? true : false;
boolean winner = checkAllWinConditions();
gameOver = winner;
if (!winner && emptySpaces == 0) {
gameOver = true;
result = "Draw";
}
}
/*
* Create the board matrix
*/
public void createBoard() {
createBoard(3);
}
/*
* Create a connect three board with a custom size and three connected to win
*/
public void createBoard(int customSize) {
createBoard(customSize, 3);
}
/*
* Create a custom size board with a custom defined amount to connect to win
*/
public void createBoard(int customSize, int inARowToWin) {
this.gameOver = false;
this.xTurn = true;
if (customSize > 0 && inARowToWin > 0) {
this.inARowToWin = inARowToWin;
gameboard = new char[customSize][customSize];
emptySpaces = customSize * customSize;
for (char[] chars : gameboard) {
Arrays.fill(chars, '_');
}
printGameboard();
} else {
createBoard(3, 3);
}
}
/*
* Handles the code while the game is in progress
*/
public boolean move(int inputX, int inputY) {
if (!gameOver) {
int coordX;
int coordY;
/* Verify that the input is within the game's boundaries */
if (inputX > 0 && inputX <= gameboard.length && inputY > 0 && inputY <= gameboard.length) {
coordX = inputX - 1;
coordY = gameboard.length - inputY;
/* Commit change to board if spot is empty */
if (gameboard[coordY][coordX] == '_' || gameboard[coordY][coordX] == ' ') {
gameboard[coordY][coordX] = xTurn ? 'X' : 'O';
emptySpaces--;
/* Check for a win based on the previously played spot */
if (isWinCondition(coordY, coordX)) {
result = xTurn ? "X wins" : "O wins";
gameOver = true;
} else if (emptySpaces == 0) {
result = "Draw";
gameOver = true;
}
xTurn = !xTurn;
return true;
} else {
System.out.println("This cell is occupied! Choose another one!");
}
} else {
System.out.println(String.format("Coordinates should be from 1 to %s!", gameboard.length));
}
}
return false;
}
private boolean checkAllWinConditions() {
boolean winCondition = false;
for (int i = 0; i < gameboard.length; i++) {
winCondition = winCondition || isWinCondition(i, i);
if (winCondition) {
result = gameboard[i][i] == 'X' ? "X wins" : "O wins";
break;
}
}
return winCondition;
}
private boolean isWinCondition(int y, int x) {
boolean winCondition;
winCondition = checkRow(y, x) || checkCol(y, x);
if (gameboard[y][x] != '_' && (inARowToWin < gameboard.length || y - x == 0 || gameboard.length - 1 - y - x == 0)) {
winCondition = checkDiagonal(y, x) || winCondition;
}
return winCondition;
}
/*
* This method checks the played horizontal row for a win condition
*/
private boolean checkRow(int y, int x) {
int count = 0;
if (gameboard[y][x] != '_') {
for (int i = 0; i < gameboard.length; i++) {
if (x - i >= 0 && gameboard[y][x] == gameboard[y][x - i]) {
count++;
} else {
break;
}
}
for (int i = 0; i < gameboard.length; i++) {
if (x + i < gameboard.length && gameboard[y][x] == gameboard[y][x + i]) {
count++;
} else {
break;
}
}
}
return count > inARowToWin;
}
/*
* This method checks the played vertical column for a win condition
*/
private boolean checkCol(int y, int x) {
int count = 0;
if (gameboard[y][x] != '_') {
for (int i = 0; i < gameboard.length; i++) {
if (y - i >= 0 && gameboard[y][x] == gameboard[y - i][x]) {
count++;
} else {
break;
}
}
for (int i = 0; i < gameboard.length; i++) {
if (y + i < gameboard.length && gameboard[y][x] == gameboard[y + i][x]) {
count++;
} else {
break;
}
}
}
return count > inARowToWin;
}
/*
* Checks diagonal for a win condition
*/
private boolean checkDiagonal(int y, int x) {
boolean desWin = false;
boolean ascWin = false;
int count;
if (inARowToWin < gameboard.length || y - x == 0) {
count = 1;
for (int i = 1; i < gameboard.length; i++) {
if (y - i >= 0 && x - i >= 0 && gameboard[y][x] == gameboard[y - i][x - i]) {
count++;
} else {
break;
}
}
for (int i = 1; i < gameboard.length; i++) {
if (y + i < gameboard.length && x + i < gameboard.length
&& gameboard[y][x] == gameboard[y + i][x + i]) {
count++;
} else {
break;
}
}
desWin = count >= inARowToWin;
}
/* check bottom left to top right diagonal win */
if (inARowToWin < gameboard.length || gameboard.length - 1 - y - x == 0) {
count = 1;
for (int i = 1; i < gameboard.length; i++) {
if (y + i < gameboard.length && x - i >= 0 && gameboard[y][x] == gameboard[y + i][x - i]) {
count++;
} else {
break;
}
}
for (int i = 1; i < gameboard.length; i++) {
if (y - i >= 0 && x + i < gameboard.length && gameboard[y][x] == gameboard[y - i][x + i]) {
count++;
} else {
break;
}
}
ascWin = count >= inARowToWin;
}
return desWin || ascWin;
}
/*
* Prints game board
*/
public void printGameboard() {
/* Determines the size of the header and footer dynamically with board size */
StringBuilder header = new StringBuilder("---");
header.append("--".repeat(gameboard.length));
System.out.println(header);
for (char[] chars : gameboard) { // print out the gameboard
System.out.print("| ");
for (char ch : chars) {
System.out.print(ch + " ");
}
System.out.println("|");
}
System.out.println(header);
}
public char[][] getGameboard() {
return gameboard;
}
public boolean isGameOver() {
return gameOver;
}
public void setGameOver(boolean gameOver) { this.gameOver = gameOver; }
public boolean isXTurn() { return xTurn; }
public String getResult() {
return result;
}
}