-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
514 lines (420 loc) · 13.9 KB
/
main.cpp
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// A C++ Program to play tic-tac-toe
#include<bits/stdc++.h>
using namespace std;
#define COMPUTER 1
#define HUMAN 2
#define SIDE 3 // Length of the board
// Computer will move with 'O'
// and human with 'X'
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'
struct Move {
int row, col;
};
int evaluate(char b[3][3]) {
// Checking for Rows for X or O victory.
for (int row = 0; row < 3; row++) {
if (b[row][0] == b[row][1] &&
b[row][1] == b[row][2]) {
if (b[row][0] == COMPUTERMOVE)
return +10;
else if (b[row][0] == HUMANMOVE)
return -10;
}
}
// Checking for Columns for X or O victory.
for (int col = 0; col < 3; col++) {
if (b[0][col] == b[1][col] &&
b[1][col] == b[2][col]) {
if (b[0][col] == COMPUTERMOVE)
return +10;
else if (b[0][col] == HUMANMOVE)
return -10;
}
}
// Checking for Diagonals for X or O victory.
if (b[0][0] == b[1][1] && b[1][1] == b[2][2]) {
if (b[0][0] == COMPUTERMOVE)
return +10;
else if (b[0][0] == HUMANMOVE)
return -10;
}
if (b[0][2] == b[1][1] && b[1][1] == b[2][0]) {
if (b[0][2] == COMPUTERMOVE)
return +10;
else if (b[0][2] == HUMANMOVE)
return -10;
}
// Else if none of them have won then return 0
return 0;
}
// This function returns true if there are moves
// remaining on the board. It returns false if
// there are no moves left to play.
bool isMovesLeft(char board[3][3]) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (board[i][j] == ' ')
return true;
return false;
}
// A function that considers all the possible ways the game can go and returns the value of the board
int minimax(char board[3][3], int depth, bool isMax) {
int score = evaluate(board);
// If Maximizer has won the game return his score
if (score == 10)
return score;
// If Minimizer has won the game return his score
if (score == -10)
return score;
// If there are no more moves and no winner then it is a tie
if (isMovesLeft(board) == false)
return 0;
// If this maximizer's move
if (isMax) {
int best = -1000;
// Traverse all cells
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Check if cell is empty
if (board[i][j] == ' ') {
// Make the move
board[i][j] = COMPUTERMOVE;
// Call minimax recursively and choose the maximum value
best = max(best, minimax(board, depth + 1, !isMax));
// Undo the move
board[i][j] = ' ';
}
}
}
return best;
}
// If this minimizer's move
else {
int best = 1000;
// Traverse all cells
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Check if cell is empty
if (board[i][j] == ' ') {
// Make the move
board[i][j] = HUMANMOVE;
// Call minimax recursively and choose the minimum value
best = min(best, minimax(board, depth + 1, !isMax));
// Undo the move
board[i][j] = ' ';
}
}
}
return best;
}
}
// This will return the best possible move for the player
Move findBestMove(char board[3][3]) {
int bestVal = -1000;
Move bestMove;
bestMove.row = -1;
bestMove.col = -1;
// Traverse all cells, evaluate minimax function for
// all empty cells. And return the cell with optimal
// value.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Check if cell is empty
if (board[i][j] == ' ') {
// Make the move
board[i][j] = COMPUTERMOVE;
// compute evaluation function for this
// move.
int moveVal = minimax(board, 0, false);
// Undo the move
board[i][j] = ' ';
// If the value of the current move is
// more than the best value, then update
// best/
if (moveVal > bestVal) {
bestMove.row = i;
bestMove.col = j;
bestVal = moveVal;
}
}
}
}
printf("The value of the best Move is : %d\n\n",
bestVal);
return bestMove;
}
// A function to show the current board status
void showBoard(char board[][SIDE]) {
printf("\n\n");
printf("\t\t\t %c | %c | %c \n", board[0][0],
board[0][1], board[0][2]);
printf("\t\t\t--------------\n");
printf("\t\t\t %c | %c | %c \n", board[1][0],
board[1][1], board[1][2]);
printf("\t\t\t--------------\n");
printf("\t\t\t %c | %c | %c \n\n", board[2][0],
board[2][1], board[2][2]);
printf("\n\n");
}
// A function to show the instructions
void showInstructions() {
printf("\t\t\t Tic-Tac-Toe\n\n");
printf("Choose a cell numbered from 1 to 9 as below"
" and play\n\n");
printf("\t\t\t ROW=0,COL=0 | ROW=0,COL=1 | ROW=0,COL=2 \n");
printf("\t\t\t-------------------------------------------\n");
printf("\t\t\t ROW=1,COL=0 | ROW=1,COL=1 | ROW=1,COL=2 \n");
printf("\t\t\t-------------------------------------------\n");
printf("\t\t\t ROW=2,COL=0 | ROW=2,COL=1 | ROW=2,COL=2 \n\n");
printf("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n");
return;
}
// A function to initialise the game
void initialise(char board[][SIDE], int moves[]) {
// Initiate the random number generator so that
// the same configuration doesn't arises
srand(time(NULL));
// Fill the moves with numbers
for (int i = 0; i < SIDE * SIDE; i++)
moves[i] = i;
// randomise the moves
random_shuffle(moves, moves + SIDE * SIDE);
return;
}
// A function to declare the winner of the game
void declareWinner(int whoseTurn) {
if (whoseTurn == COMPUTER)
printf("COMPUTER has won\n");
else
printf("HUMAN has won\n");
return;
}
void declareWinnerTwo(int whoseTurn) {
if (whoseTurn == COMPUTER)
printf("PLAYER 1 has won\n");
else
printf("PLAYER 2 has won\n");
return;
}
// A function that returns true if any of the row
// is crossed with the same player's move
bool rowCrossed(char board[][SIDE]) {
for (int i = 0; i < SIDE; i++) {
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != ' ')
return (true);
}
return (false);
}
// A function that returns true if any of the column
// is crossed with the same player's move
bool columnCrossed(char board[][SIDE]) {
for (int i = 0; i < SIDE; i++) {
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != ' ')
return (true);
}
return (false);
}
// A function that returns true if any of the diagonal
// is crossed with the same player's move
bool diagonalCrossed(char board[][SIDE]) {
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != ' ')
return (true);
if (board[0][2] == board[1][1] &&
board[1][1] == board[2][0] &&
board[0][2] != ' ')
return (true);
return (false);
}
// A function that returns true if the game is over
// else it returns a false
bool gameOver(char board[][SIDE]) {
return (rowCrossed(board) || columnCrossed(board)
|| diagonalCrossed(board));
}
// A function to play Tic-Tac-Toe easy
void playTicTacToe_easy(int whoseTurn, char board[SIDE][SIDE]) {
int moves[SIDE * SIDE];
// Initialise the game
initialise(board, moves);
int moveIndex = 0, x, y;
// Keep playing till the game is over or it is a draw
while (gameOver(board) == false &&
moveIndex != SIDE * SIDE) {
if (whoseTurn == COMPUTER) {
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
while (board[x][y] != ' ') {
moveIndex++;
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
}
board[x][y] = COMPUTERMOVE;
printf("COMPUTER has put a %c in ROW %d COL %d\n",
COMPUTERMOVE, x, y);
showBoard(board);
moveIndex++;
whoseTurn = HUMAN;
}
else if (whoseTurn == HUMAN) {
do {
printf("ROW=");
scanf("%d", &x);
printf("COLUMN=");
scanf("%d", &y);
} while (board[x][y] != ' ');
board[x][y] = HUMANMOVE;
printf("HUMAN has put a %c in ROW %d COL %d\n",
HUMANMOVE, x, y);
showBoard(board);
whoseTurn = COMPUTER;
}
}
// If the game has drawn
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw\n");
else {
// Toggling the user to declare the actual
// winner
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
// Declare the winner
declareWinner(whoseTurn);
}
return;
}
// A function to play Tic-Tac-Toe with two players
void playTicTacToe_Two(int whoseTurn, char board[SIDE][SIDE]) {
int x, y;
// Keep playing till the game is over or it is a draw
while (gameOver(board) == false && isMovesLeft(board) == true) {
if (whoseTurn == COMPUTER) {
do {
printf("ROW=");
scanf("%d", &x);
printf("COLUMN=");
scanf("%d", &y);
} while (board[x][y] != ' ');
board[x][y] = COMPUTERMOVE;
printf("PLAYER 1 has put a %c in ROW %d COL %d\n",
COMPUTERMOVE, x, y);
showBoard(board);
whoseTurn = HUMAN;
}
else if (whoseTurn == HUMAN) {
do {
printf("ROW=");
scanf("%d", &x);
printf("COLUMN=");
scanf("%d", &y);
} while (board[x][y] != ' ');
board[x][y] = HUMANMOVE;
printf("PLAYER 2 has put a %c in ROW %d COL %d\n",
HUMANMOVE, x, y);
showBoard(board);
whoseTurn = COMPUTER;
}
}
// If the game has drawn
if (gameOver(board) == false)
printf("It's a draw\n");
else {
// Toggling the user to declare the actual
// winner
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
// Declare the winner
declareWinnerTwo(whoseTurn);
}
return;
}
// A function to play Tic-Tac-Toe hard
void playTicTacToeminmax(int whoseTurn, char board[SIDE][SIDE]) {
int x, y;
// Keep playing till the game is over or it is a draw
while (gameOver(board) == false && isMovesLeft(board) == true) {
if (whoseTurn == COMPUTER) {
Move bestMove = findBestMove(board);
x = bestMove.row;
y = bestMove.col;
board[x][y] = COMPUTERMOVE;
printf("COMPUTER has put a %c in ROW %d COL %d\n", COMPUTERMOVE, x, y);
showBoard(board);
whoseTurn = HUMAN;
}
else if (whoseTurn == HUMAN) {
do {
printf("ROW=");
scanf("%d", &x);
printf("COLUMN=");
scanf("%d", &y);
} while (board[x][y] != ' ');
board[x][y] = HUMANMOVE;
printf("HUMAN has put a %c in ROW %d COL %d\n", HUMANMOVE, x, y);
showBoard(board);
whoseTurn = COMPUTER;
}
}
// If the game has drawn
if (gameOver(board) == false)
printf("It's a draw\n");
else {
// Toggling the user to declare the actual
// winner
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
// Declare the winner
declareWinner(whoseTurn);
}
return;
}
// Driver program
int main() {
// A 3*3 Tic-Tac-Toe board for playing
char board[SIDE][SIDE];
int choice, choice_computer, n;
// Initially the board is empty
for (int i = 0; i < SIDE; i++) {
for (int j = 0; j < SIDE; j++)
board[i][j] = ' ';
}
printf("WELCOME TO THE TIC-TAC-TOE GAME\n ARE YOU READY !\n SO LET'S START\n");
printf("if you want to play with a local player press 1 \nif you want to play with the computer press 2\n");
scanf("%d", &choice);
if (choice == 2) {
printf("EASY_MODE :press 1 \t\t HARD_MODE:press 2\n");
scanf("%d", &choice_computer);
if (choice_computer == 1) {
// Show the instructions before playing
showInstructions();
// Let us play the game with COMPUTER starting first playTicTacToe_easy(COMPUTER);
printf("press 2 to play first else press 1 ");
scanf("%d", &n);
playTicTacToe_easy(n, board);
} else {
// Show the instructions before playing
showInstructions();
printf("press 2 to play first else press 1 ");
scanf("%d", &n);
playTicTacToeminmax(n, board);
}
} else {
// Show the instructions before playing
showInstructions();
playTicTacToe_Two(1, board);
}
return (0);
}