-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
455 lines (428 loc) · 14.6 KB
/
TicTacToe.java
File metadata and controls
455 lines (428 loc) · 14.6 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import java.util.*;
import java.util.Random;
public class TicTacToe {
public static void main(String[] args) {
board grid = new board(); //making a grid object of board class
grid.initialise(); //initilising the grid
System.out.println("Choose the version");
System.out.println("Enter 1 for Player Vs Player, Enter 2 for Player Vs Computer");
Scanner scn = new Scanner(System.in);
int y = scn.nextInt();
int x;
grid.instructionBoard();
if (y == 2) {
System.out.println("Welcome to TicTacToe. Player is 'X' and Computer is 'O'");
while (board.temp != 0) {
if (board.xx == 1) {
System.out.println("Player, Enter your desired location[1-9]");
scn = new Scanner(System.in);
x = scn.nextInt();
} else {
if (grid.computerMoves() != 0) {
x = grid.computerMoves();
} else {
Random rand = new Random();
x = rand.nextInt(9) + 1;
}
System.out.print("Computer entered ");
System.out.println(x);
}
grid.modifyBoard(x, y);
grid.printBoard();
}
} else if (y == 1) {
System.out.println("Welcome to TicTacToe. Player1 is 'X' and Player2 is 'O'");
while (board.temp != 0) {
if (board.xx == 1) {
System.out.println("Player1, Enter your desired location[1-9]");
scn = new Scanner(System.in);
x = scn.nextInt();
} else {
System.out.println("Player2, Enter your desired location[1-9]");
scn = new Scanner(System.in);
x = scn.nextInt();
}
grid.modifyBoard(x, y);
grid.printBoard();
}
} else {
System.out.println("Wrong choosen option");
}
}
}
class board {
private int[][] arr = new int[3][3]; //properties of class taken by object
static int temp = -1; //variable of class board used in subsequent method
static int xx = 1; //variable of class board used in subsequent method
public void initialise() {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
arr[i][j] = -1;
}
}
}
public void instructionBoard() {
System.out.println("");
System.out.println("This is a new game. Board numbers are as follows:");
System.out.print("---------------");
System.out.println("");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
System.out.print(" |");
System.out.print(3 * i + j + 1);
System.out.print("| ");
}
System.out.println("");
System.out.print("---------------");
System.out.println("");
}
System.out.println("");
}
public void printBoard() {
System.out.print("---------------");
System.out.println("");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (arr[i][j] == 1) {
System.out.print(" |");
System.out.print("X");
System.out.print("| ");
}
if (arr[i][j] == 0) {
System.out.print(" |");
System.out.print("0");
System.out.print("| ");
}
if (arr[i][j] == -1) {
System.out.print(" |");
System.out.print(" ");
System.out.print("| ");
}
}
System.out.println("");
System.out.print("---------------");
System.out.println("");
}
}
public void modifyBoard(int x, int y) {
int check = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if ((3 * i + j + 1) == x) {
if (arr[i][j] == -1) {
check = 1;
break;
}
}
}
}
if (x >= 10 || x <= 0) {
System.out.println("Invalid choosen location,Please choose b/w [1-9]");
} else if (check == 1) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if ((3 * i + j + 1) == x) {
this.arr[i][j] = xx;
break;
}
}
}
if (x == 1) {
if (this.arr[0][0] == xx && this.arr[0][1] == xx && this.arr[0][2] == xx) {
temp = 0;
} else if (this.arr[0][0] == xx && this.arr[1][0] == xx && this.arr[2][0] == xx) {
temp = 0;
} else if (this.arr[0][0] == xx && this.arr[1][1] == xx && this.arr[2][2] == xx) {
temp = 0;
}
} else if (x == 2) {
if (this.arr[0][0] == xx && this.arr[0][1] == xx && this.arr[0][2] == xx) {
temp = 0;
} else if (this.arr[0][1] == xx && this.arr[1][1] == xx && this.arr[2][1] == xx) {
temp = 0;
}
} else if (x == 3) {
if (this.arr[0][0] == xx && this.arr[0][1] == xx && this.arr[0][2] == xx) {
temp = 0;
} else if (this.arr[0][2] == xx && this.arr[1][2] == xx && this.arr[2][2] == xx) {
temp = 0;
} else if (this.arr[0][2] == xx && this.arr[1][1] == xx && this.arr[2][0] == xx) {
temp = 0;
}
} else if (x == 4) {
if (this.arr[0][0] == xx && this.arr[1][0] == xx && this.arr[2][0] == xx) {
temp = 0;
} else if (this.arr[1][0] == xx && this.arr[1][1] == xx && this.arr[1][2] == xx) {
temp = 0;
}
} else if (x == 5) {
if (this.arr[0][0] == xx && this.arr[1][1] == xx && this.arr[2][2] == xx) {
temp = 0;
} else if (this.arr[0][2] == xx && this.arr[1][1] == xx && this.arr[2][0] == xx) {
temp = 0;
} else if (this.arr[0][1] == xx && this.arr[1][1] == xx && this.arr[2][1] == xx) {
temp = 0;
} else if (this.arr[1][0] == xx && this.arr[1][1] == xx && this.arr[1][2] == xx) {
temp = 0;
}
} else if (x == 6) {
if (this.arr[0][2] == xx && this.arr[1][2] == xx && this.arr[2][2] == xx) {
temp = 0;
} else if (this.arr[1][0] == xx && this.arr[1][1] == xx && this.arr[1][2] == xx) {
temp = 0;
}
} else if (x == 7) {
if (this.arr[0][0] == xx && this.arr[1][0] == xx && this.arr[2][0] == xx) {
temp = 0;
} else if (this.arr[2][0] == xx && this.arr[2][1] == xx && this.arr[2][2] == xx) {
temp = 0;
} else if (this.arr[2][0] == xx && this.arr[1][1] == xx && this.arr[0][2] == xx) {
temp = 0;
}
} else if (x == 8) {
if (this.arr[0][1] == xx && this.arr[1][1] == xx && this.arr[2][1] == xx) {
temp = 0;
} else if (this.arr[2][0] == xx && this.arr[2][1] == xx && this.arr[2][2] == xx) {
temp = 0;
}
} else if (x == 9) {
if (this.arr[0][0] == xx && this.arr[1][1] == xx && this.arr[2][2] == xx) {
temp = 0;
} else if (this.arr[2][0] == xx && this.arr[2][1] == xx && this.arr[2][2] == xx) {
temp = 0;
} else if (this.arr[2][2] == xx && this.arr[1][2] == xx && this.arr[0][2] == xx) {
temp = 0;
}
}
//Checking if someone wins or not
if (temp == 0) {
if (y == 1) {
System.out.print("Player");
if (xx == 1) {
System.out.print(xx);
} else {
System.out.print(2);
}
System.out.println(" Win!!");
}
if (y == 2) {
if (xx == 1) {
System.out.println("You win!!");
} else {
System.out.println("Computer win!!");
}
}
}
xx = 1 - xx;
} else {
System.out.println("Already filled location choosen");
}
//Checking for a Tie game
if (isFull() == false) {
System.out.println("The Game is a Tie!!");
temp = 0;
}
}
public boolean isFull() {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (arr[i][j] == -1) return true;
}
}
return false;
}
//Checking if moves done by computer is optimal or not
public boolean checkMoves() {
if (arr[0][0] == arr[0][1] && arr[0][1] == arr[0][2]&&arr[0][0]!=-1) {
return true;
}
if (arr[0][0] == arr[1][0] && arr[1][0] == arr[2][0]&&arr[0][0]!=-1) {
return true;
}
if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2]&&arr[0][0]!=-1) {
return true;
}
if (arr[0][1] == arr[1][1] && arr[1][1] == arr[2][1]&&arr[0][1]!=-1) {
return true;
}
if (arr[0][2] == arr[1][2] && arr[1][2] == arr[2][2]&&arr[0][2]!=-1) {
return true;
}
if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0]&&arr[0][2]!=-1) {
return true;
}
if (arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2]&&arr[1][0]!=-1) {
return true;
}
if (arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2]&&arr[2][0]!=-1) {
return true;
}
return false;
}
//Checking for if computer has a winning moves or if computer can block opponents winning position
public int computerMoves() {
if (arr[0][0] == -1) {
arr[0][0] = 0;
if (checkMoves() == true) {
arr[0][0] = -1;
return 1;
} else {
arr[0][0] = -1;
}
}
if (arr[0][1] == -1) {
arr[0][1] = 0;
if (checkMoves() == true) {
arr[0][1] = -1;
return 2;
} else {
arr[0][1] = -1;
}
}
if (arr[0][2] == -1) {
arr[0][2] = 0;
if (checkMoves() == true) {
arr[0][2] = -1;
return 3;
} else {
arr[0][2] = -1;
}
}
if (arr[1][0] == -1) {
arr[1][0] = 0;
if (checkMoves() == true) {
arr[1][0] = -1;
return 4;
} else {
arr[1][0] = -1;
}
}
if (arr[1][1] == -1) {
arr[1][1] = 0;
if (checkMoves() == true) {
arr[1][1] = -1;
return 5;
} else {
arr[1][1] = -1;
}
}
if (arr[1][2] == -1) {
arr[1][2] = 0;
if (checkMoves() == true) {
arr[1][2] = -1;
return 6;
} else {
arr[1][2] = -1;
}
}
if (arr[2][0] == -1) {
arr[2][0] = 0;
if (checkMoves() == true) {
arr[2][0] = -1;
return 7;
} else {
arr[2][0] = -1;
}
}
if (arr[2][1] == -1) {
arr[2][1] = 0;
if (checkMoves() == true) {
arr[2][1] = -1;
return 8;
} else {
arr[2][1] = -1;
}
}
if (arr[2][2] == -1) {
arr[2][2] = 0;
if (checkMoves() == true) {
arr[2][2] = -1;
return 9;
} else {
arr[2][2] = -1;
}
}
if (arr[0][0] == -1) {
arr[0][0] = 1;
if (checkMoves() == true) {
arr[0][0] = -1;
return 1;
} else {
arr[0][0] = -1;
}
}
if (arr[0][1] == -1) {
arr[0][1] = 1;
if (checkMoves() == true) {
arr[0][1] = -1;
return 2;
} else {
arr[0][1] = -1;
}
}
if (arr[0][2] == -1) {
arr[0][2] = 1;
if (checkMoves() == true) {
arr[0][2] = -1;
return 3;
} else {
arr[0][2] = -1;
}
}
if (arr[1][0] == -1) {
arr[1][0] = 1;
if (checkMoves() == true) {
arr[1][0] = -1;
return 4;
} else {
arr[1][0] = -1;
}
}
if (arr[1][1] == -1) {
arr[1][1] = 1;
if (checkMoves() == true) {
arr[1][1] = -1;
return 5;
} else {
arr[1][1] = -1;
}
}
if (arr[1][2] == -1) {
arr[1][2] = 1;
if (checkMoves() == true) {
arr[1][2] = -1;
return 6;
} else {
arr[1][2] = -1;
}
}
if (arr[2][0] == -1) {
arr[2][0] = 1;
if (checkMoves() == true) {
arr[2][0] = -1;
return 7;
} else {
arr[2][0] = -1;
}
}
if (arr[2][1] == -1) {
arr[2][1] = 1;
if (checkMoves() == true) {
arr[2][1] = -1;
return 8;
} else {
arr[2][1] = -1;
}
}
if (arr[2][2] == -1) {
arr[2][2] = 1;
if (checkMoves() == true) {
arr[2][2] = -1;
return 9;
} else {
arr[2][2] = -1;
}
}
return 0;
}
}