-
Notifications
You must be signed in to change notification settings - Fork 1
/
TicTacToeGame.java
349 lines (333 loc) · 11.1 KB
/
TicTacToeGame.java
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* A game of TicTacToe to be played between two human players.
*
* The board is a 2D array of Strings.
* One player is the String literal "X".
* The second player is the String literal "O" (the letter O, not the numeral zero).
*
* The game should continue to be played as long as either:
* 1. No player has won the game.
* 2. There is at least one open square left in the game.
*
* This project adapted from code by I. Frame.
*
* @yourName: Hershel Thomas
*/
public class TicTacToeGame
{
/*
* These fields are used to build the GUI.
* You won't need to modiy them.
* They are declared as final because their value is final; it won't ever change.
* The convention for final constants is all uppercase. Remember Math.PI?
*/
private final int WINDOW_SIZE;
private final int LINE_BUFFER;
private final int TILE_SIZE;
// These are the fields you'll be working with. Added new fiels for Strings and ints for collum and row testing
private boolean isXsTurn;
private String[][] board;
public String pResult = "";
public String r1Check = "";
public int oneX;
public int oneO;
public TicTacToeGame()
{
WINDOW_SIZE = 600;
LINE_BUFFER = 25;
TILE_SIZE = WINDOW_SIZE / 3;
isXsTurn = true;
}
private boolean isCellClaimed(int r, int c)
{
// to be implemented by you
//checks if board piece is taken by checking if it is still the default null, returns true if not null
if (board[r][c] != null) {
return true;
} else {
return false;
}
}
private boolean hasDiagonalsWon()
{
//to be implemented by you.
//turns each diagonal into a readble String. Uses .charAt() to find how many of X's or O's there are
//and Then returns true if there are 3 of the same kind cant figure out how to make it more efficient with
//nested for loops like I did for collums, so one for loop also works.
String lCheck = "";
int lX = 0;
int lO = 0;
String rCheck = "";
int rX = 0;
int rO = 0;
for (int i = 0; i < board[0].length; i++) {
lCheck += board[i][i];
rCheck += board[i][(board[0].length - 1) - i];
if (lCheck.charAt(i) == 'X') {
lX++;
} else if (lCheck.charAt(i) == 'O') {
lO++;
}
if (rCheck.charAt(i) == 'X') {
rX++;
} else if (rCheck.charAt(i) == 'O') {
rO++;
}
}
if (lX == 3 || rX == 3 || lO == 3 || rO == 3) {
System.out.println("Game Over! " + board[1][1] + " Diagonal Win");
return true;
}
return false;
}
private boolean hasRowsWon()
{
//to be implemented by you.
//uses a nested for loop to create each row as a string, and then use a counter to test if there are 3 of the same
//charcters in the String and then if there are return true and tell who won the game.
for (int i = 0; i < board.length; i++) {
oneX = 0;
oneO = 0;
r1Check = "";
for (int j = 0; j < board[0].length; j++) {
r1Check += board[i][j];
if (r1Check.charAt(j) == 'X') {
oneX++;
} else if (r1Check.charAt(j) == 'O') {
oneO++;
}
if (oneX == 3 || oneO == 3) {
System.out.println("Game Over! " + r1Check.charAt(0) + " Row Win");
return true;
}
}
}
return false;
}
private boolean hasColsWon()
{
//to be implemented by you.
//checks collums using same was as rows by nested for loop
for (int i = 0; i < board.length; i++) {
oneX = 0;
oneO = 0;
r1Check = "";
for (int j = 0; j < board[0].length; j++) {
r1Check += board[j][i];
if (r1Check.charAt(j) == 'X') {
oneX++;
} else if (r1Check.charAt(j) == 'O') {
oneO++;
}
if (oneX == 3 || oneO == 3) {
System.out.println("Game Over! " + r1Check.charAt(0) + " Column Win");
return true;
}
}
}
return false;
}
private boolean isGameOver()
{
//to be implemented by you.
//isGameOver() should call other methods in the class to determine if the game is over.
if (hasColsWon() || hasRowsWon() || hasDiagonalsWon() || isBoardFull()) {return true;}
return false;
}
//This will require looping, so you may want to save this for later in Module 6.
private boolean isBoardFull()
{
//to be implemented by you.
//runs a for nested for loop and uses a counter to check if all 9 spots are claimed
int counter = 0;
for (int i = 0; i < board.length; i++ ) {
for (int j = 0; j < board[0].length; j++) {
if (isCellClaimed(i, j)) {
counter++;
}
}
}
if (counter == 9) {
System.out.println("Tie!");
return true;
} else {
return false;
}
}
public void printBoard() {
int spot = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
System.out.println("" + spot + ": " + board[i][j]);
spot++;
}
}
}
//////////////////////DO NOT MODIFY ANYTHING BELOW THIS COMMENT!!!!////////////////////////
//////////////////////SERIOUSLY, DO NOT CHANGE ANYTHING AFTER THIS COMMENT.////////////////
public void playAGame()
{
drawBoard();
while (!isGameOver())
{
if (mouseClicked())
{
double x = StdDraw.mouseX();
double y = StdDraw.mouseY();
int square = determineSquare(x, y);
int[] coords = getSquareRowColumn(square);
if (!isCellClaimed(coords[0], coords[1]))
{
if(isXsTurn)
drawShape("X", square);
else
drawShape("O", square);
isXsTurn = !isXsTurn;
}
}
}
}
private void drawBoard()
{
StdDraw.setCanvasSize(WINDOW_SIZE, WINDOW_SIZE);
StdDraw.setScale(0, WINDOW_SIZE);
StdDraw.enableDoubleBuffering();
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(0.005);
StdDraw.line(WINDOW_SIZE / 3, LINE_BUFFER, WINDOW_SIZE / 3, WINDOW_SIZE - LINE_BUFFER);
StdDraw.line(2*WINDOW_SIZE / 3, LINE_BUFFER, 2*WINDOW_SIZE / 3, WINDOW_SIZE - LINE_BUFFER);
StdDraw.line(LINE_BUFFER, WINDOW_SIZE / 3, WINDOW_SIZE - LINE_BUFFER, WINDOW_SIZE / 3);
StdDraw.line(LINE_BUFFER, 2*WINDOW_SIZE / 3, WINDOW_SIZE - LINE_BUFFER, 2*WINDOW_SIZE / 3);
StdDraw.show();
board = new String[3][3];
}
private int determineSquare(double x, double y)
{
if (y > 2*WINDOW_SIZE / 3)
{
if (x < WINDOW_SIZE / 3)
return 1;
else if (x < 2*WINDOW_SIZE / 3)
return 2;
else
return 3;
}
else if (y > WINDOW_SIZE / 3)
{
if (x < WINDOW_SIZE / 3)
return 4;
else if (x < 2*WINDOW_SIZE / 3)
return 5;
else
return 6;
}
else
{
if (x < WINDOW_SIZE / 3)
return 7;
else if (x < 2*WINDOW_SIZE / 3)
return 8;
else
return 9;
}
}
private int[] getSquareRowColumn(int square)
{
int[] coords = new int[2];
switch (square){
case 1:
coords[0] = 0; coords[1] = 0; break;
case 2:
coords[0] = 0; coords[1] = 1; break;
case 3:
coords[0] = 0; coords[1] = 2; break;
case 4:
coords[0] = 1; coords[1] = 0; break;
case 5:
coords[0] = 1; coords[1] = 1; break;
case 6:
coords[0] = 1; coords[1] = 2; break;
case 7:
coords[0] = 2; coords[1] = 0; break;
case 8:
coords[0] = 2; coords[1] = 1; break;
case 9:
coords[0] = 2; coords[1] = 2; break;
default:
break;
}
return coords;
}
private void drawShape(String letter, int square)
{
double x, y;
if (square == 1 || square == 4 || square == 7)
x = WINDOW_SIZE / 2 - TILE_SIZE;
else if (square == 2 || square == 5 || square == 8)
x = WINDOW_SIZE / 2;
else
x = WINDOW_SIZE / 2 + TILE_SIZE;
if (square <= 3)
y = WINDOW_SIZE - TILE_SIZE / 2;
else if (square <= 6)
y = WINDOW_SIZE / 2;
else
y = TILE_SIZE / 2;
if (letter.equalsIgnoreCase("X"))
{
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.line(x - TILE_SIZE / 2 + LINE_BUFFER, y - TILE_SIZE / 2 + LINE_BUFFER, x + TILE_SIZE / 2 - LINE_BUFFER, y + TILE_SIZE / 2 - LINE_BUFFER);
StdDraw.line(x - TILE_SIZE / 2 + LINE_BUFFER, y + TILE_SIZE / 2 - LINE_BUFFER, x + TILE_SIZE / 2 - LINE_BUFFER, y - TILE_SIZE / 2 + LINE_BUFFER);
addSymbolToBoard("X", square);
}
else
{
StdDraw.setPenColor(StdDraw.RED);
StdDraw.filledCircle(x, y, TILE_SIZE / 2 - LINE_BUFFER);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledCircle(x, y, TILE_SIZE / 2 - 1.2*LINE_BUFFER);
addSymbolToBoard("O", square);
}
StdDraw.show();
}
private void addSymbolToBoard(String letter, int square)
{
switch (square){
case 1:
board[0][0] = letter; break;
case 2:
board[0][1] = letter; break;
case 3:
board[0][2] = letter; break;
case 4:
board[1][0] = letter; break;
case 5:
board[1][1] = letter; break;
case 6:
board[1][2] = letter; break;
case 7:
board[2][0] = letter; break;
case 8:
board[2][1] = letter; break;
case 9:
board[2][2] = letter; break;
default:
break;
}
}
private void waitForStart()
{
while (!mouseClicked())
{}
}
private boolean mouseClicked()
{
if (StdDraw.mousePressed())
{
while (StdDraw.mousePressed())
{}
return true;
}
return false;
}
}