-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeGame.java
More file actions
416 lines (303 loc) · 9.9 KB
/
TicTacToeGame.java
File metadata and controls
416 lines (303 loc) · 9.9 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
public class TicTacToeGame {
private static final char PLAYERX = 'X'; // Helper constant for X player
private static final char PLAYERO = 'O'; // Helper constant for O player
private static final char SPACE = ' '; // Helper constant for spaces
private static int Count=10;
private boolean hasWon;
private boolean check;
private String lastPlayer;
private boolean TIE;
public char WINNER;
public void setLastPlayer(String lastPlayer)
{
this.lastPlayer=lastPlayer;
}
public String getLastPlayer() {
return lastPlayer;
}
private char[] tableDisplay;
/*
Sample TicTacToe Board
0 | 1 | 2
-----------
3 | 4 | 5
-----------
6 | 7 | 8
*/
// TODO 4: Implement necessary methods to manage the games of Tic Tac Toe
public TicTacToeGame(String otherPlayer)
{
tableDisplay=new char[9];
for(int i=0;i<9;i++)
{
tableDisplay[i]=SPACE;
}
}
public synchronized void updateTable(int place)
{
if(place>8||place<0)
{
return;
}
else
{
//returnBoo();
if(tableDisplay[place]!='O'&&tableDisplay[place]!='X')
{
if(check)
{
tableDisplay[place]='O';
check=false;
}
else
{
tableDisplay[place]='X';
check=true;
}
}
char player = checkWinner();
if(player==PLAYERX){
WINNER=PLAYERX;
returnWinner();
}
else if(player==PLAYERO){
WINNER=PLAYERO;
returnWinner();
}
else if(player==SPACE&&Count==0){
WINNER=SPACE;
}
}
}
public char returnWinner()
{
return WINNER;
}
public String displayTable()
{
return tableDisplay[0] + " | " + tableDisplay[1] + " | " + tableDisplay[2] + "\n" + tableDisplay[3] + " | " + tableDisplay[4] + " | " + tableDisplay[5] + "\n" +
tableDisplay[6] + " | " + tableDisplay[7] + " | " + tableDisplay[8]+"\n";
}
public char checkWinner()
{
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line =Character.toString(tableDisplay[0]) + tableDisplay[1] + tableDisplay[2];
break;
case 1:
line = Character.toString(tableDisplay[3]) + tableDisplay[4] + tableDisplay[5];
break;
case 2:
line = Character.toString(tableDisplay[6]) + tableDisplay[7] + tableDisplay[8];
break;
case 3:
line =Character.toString (tableDisplay[0]) + tableDisplay[3] + tableDisplay[6];
break;
case 4:
line = Character.toString (tableDisplay[1]) + tableDisplay[4] + tableDisplay[7];
break;
case 5:
line = Character.toString (tableDisplay[2]) + tableDisplay[5] + tableDisplay[8];
break;
case 6:
line = Character.toString (tableDisplay[0]) + tableDisplay[4] + tableDisplay[8];
break;
case 7:
line =Character.toString (tableDisplay[2]) + tableDisplay[4] + tableDisplay[6];
break;
}
int count=0;
for(int i=0;i<9;i++)
{
if(tableDisplay[i]==' ')
{
count++;
}
}
if(count==0)
{
TIE=true;
}
if (line.equals("XXX")) {
return PLAYERX;
} else if (line.equals("OOO")) {
return PLAYERO;
}
}
return SPACE;
}
public int takeTurn(int index)
{
return 0;
}
public char getWinner()
{
return ' ';
}
public int isTied()
{
return 0;
}
public char getSpace(int index)
{
return ' ';
}
public String toString()
{
return "";
}
public int returnCount()
{
return Count++;
}
public synchronized boolean returnBoo()
{
if(!check)
{
check=true;
return true;
}
else
{
check = false;
return false;
}
}
}
//import java.util.InputMismatchException;
//public class TicTacToeGame {
// private static final char PLAYERX = 'X'; // Helper constant for X player
// private static final char PLAYERO = 'O'; // Helper constant for O player
// private static final char SPACE = ' '; // Helper constant for spaces
// /*
// Sample TicTacToe Board
// 0 | 1 | 2
// -----------
// 3 | 4 | 5
// -----------
// 6 | 7 | 8
// */
// TicTacToeGame(String otherPlayer , boolean isX){
//
// }
// public int takeTurn(){
// return -1;
// }
// // TODO 4: Implement necessary methods to manage the games of Tic Tac Toe
//
// static char[] board=new char[9];
// static char turn=PLAYERX;
//
// // public static void main(String[] args) {
//
// // int n=Integer.parseInt(args[0]);
//
// // board = new char[9];
// // turn = PLAYERX;
// static char winner = SPACE;
// // populateEmptyBoard();
// static int numInput;
//
//// System.out.println("Welcome to 2 Player Tic Tac Toe.");
// // System.out.println("--------------------------------");
// // printBoard();
// // System.out.println("X's will play first. Enter a slot number to place X in:");
//
// // while (winner == null) {
//
// // int numInput=Integer.parseInt(args[0]);
// // System.out.println();
// public void trying() {
// try {
//// System.out.println("kk");
//// numInput = in.nextInt();
//// in.nextLine();
// while (numInput >= 0 && numInput < 9) {
// System.out.println("Invalid input; re-enter slot number:");
// // continue;
// }
// } catch (InputMismatchException e) {
// System.out.println("Invalid input; re-enter slot number:");
// //continue;
// }
//
// if (board[numInput] == (char) (numInput + 48)) {
// board[numInput] = turn;
// if (turn == PLAYERX) {
// turn = PLAYERO;
// } else {
// turn = PLAYERX;
// }
// //printBoard();
// winner = getWinner();
// } else {
// System.out.println("Slot already taken; re-enter slot number:");
// //continue;
// }
// if (winner == PLAYERX || winner == PLAYERO) {
// System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");
// }
// }
// public static int isTied(){
// return -1;
// }
// public static char getSpace(int index){
// return SPACE;
// }
// @Override
// public String toString() {
// return super.toString();
// }
// static char getWinner() {
// for (int a = 0; a < 8; a++) {
// String line = null;
// switch (a) {
// case 0:
// line =Character.toString(board[0]) + board[1] + board[2];
// break;
// case 1:
// line = Character.toString(board[3]) + board[4] + board[5];
// break;
// case 2:
// line = Character.toString(board[6]) + board[7] + board[8];
// break;
// case 3:
// line =Character.toString (board[0]) + board[3] + board[6];
// break;
// case 4:
// line = Character.toString (board[1]) + board[4] + board[7];
// break;
// case 5:
// line = Character.toString (board[2]) + board[5] + board[8];
// break;
// case 6:
// line = Character.toString (board[0]) + board[4] + board[8];
// break;
// case 7:
// line =Character.toString (board[2]) + board[4] + board[6];
// break;
// }
// if (line.equals("XXX")) {
// return PLAYERX;
// } else if (line.equals("OOO")) {
// return PLAYERO;
// }
// }
// System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:");
// return SPACE;
// }
//// static void printBoard() {
//// System.out.println( board[0] + " | " + board[1] + " | " + board[2] );
//// System.out.println("----------");
//// System.out.println( board[3] + " | " + board[4] + " | " + board[5]);
//// System.out.println("----------");
//// System.out.println( board[3] + " | " + board[4] + " | " + board[5]);
//// }
//// static void populateEmptyBoard() {
//// for (int a = 0; a < 9; a++) {
//// board[a] = (char)(a+48);
//// }
//// }
//}