-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathttt.cpp
More file actions
596 lines (536 loc) · 12.9 KB
/
ttt.cpp
File metadata and controls
596 lines (536 loc) · 12.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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include <iostream>
#include<stdio.h>
#include <vector>
#include <string>
// Arrays of chars for printing out the board all fancy-like
const char GLYPHS[3][3][3] =
{
{
{ ' ', ' ', ' ' },
{ ' ', ' ', ' ' },
{ ' ', ' ', ' ' }
},
{
{ ' ', ' ', ' ' },
{ ' ', 'X', ' ' },
{ ' ', ' ', ' ' }
},
{
{ ' ', ' ', ' ' },
{ ' ', 'O', ' ' },
{ ' ', ' ', ' ' }
}
};
const int TIE_VAL = -1;
// Indexes for board values
const int NO_VAL = 0;
const int X_VAL = 1;
const int O_VAL = 2;
// Game board class
struct AiMove
{
AiMove() {};
AiMove(int Score) : score(Score) {}
int x;
int y;
int score;
};
class Board
{
public:
// Initializes the board
void init(int size);
// Clears the board
void clear();
// Prints the board to standard output
void print() const;
// If no player won, returns NO_VAL. Otherwise returns X_VAL, O_VAL or TIE_VAL
int checkVictory() const;
// Sets value at x,y spot
void setVal(int x, int y, int val)
{
_board[y * _size + x] = val;
}
// Gets value at x,y spot
int getVal(int x, int y) const
{
return _board[y * _size + x];
}
// Getters
int getSize() const { return _size; }
private:
// Adds a horizontal line to string for printing
void addHorizontalLine(std::string& s) const;
// Adds a horizontal axis guide line to string for printing
void addGuide(std::string& s) const;
std::vector<int> _board;
int _size;
};
class AI
{
public:
// Initializes the AI player
void init(int aiPlayer);
// Performs the AI move
void performMove(Board& board);
private:
AiMove getBestMove(Board& board, int player);
int _aiPlayer; ///< Index of the AI
int _humanPlayer; ///< Index of the player
};
enum class GameState { PLAYING, EXIT };
class MainGame
{
public:
// Runs the main loop
void run();
private:
// Initializes the game
void init();
// Performs a human controlled move
void playerMove();
// Performs an AI move
void aiMove();
// Changes players
void changePlayer();
// Ends a game and prompts for quit or re-try
void endGame(bool wasTie);
// Member Variables
Board _board; ///< The tic-tac-toe boarde
int _currentPlayer; ///< The index of the current player
int _aiPlayer; ///< Index of the AI player
GameState _gameState; ///< The state of the game
AI _ai; ///< The AI player
bool _isMultiplayer;
};
void Board::init(int size)
{
// Just cap it at 9 since we have limited console space
if (size > 9)
size = 9;
_size = size;
// Set the board size
_board.resize(size * size);
// Clear it
clear();
}
void Board::clear()
{
for (int i = 0; i < _board.size(); i++)
{
_board[i] = NO_VAL;
}
}
void Board::print() const
{
// Instead of calling printf multiple times, we build a single
// string and call printf once, since its faster.
std::string text = "";
// Just reserve a bunch of memory, this should be enough
text.reserve(_size * _size * 4);
// Make top guide
addGuide(text);
// Loop through columns
for (int y = 0; y < _size; y++) {
// Add line
addHorizontalLine(text);
// Loop through glyph y
for (int gy = 0; gy < 3; gy++)
{
// Add number or space
if (gy == 1)
{
std::string text =text+std::to_string(_size - y);
} else {
text += " ";
}
// Loop through row cells
for (int x = 0; x < _size; x++)
{
// Loop through glyph x
text += "|";
for (int gx = 0; gx < 3; gx++)
{
text += GLYPHS[getVal(x, _size - y - 1)][gy][gx];
}
}
text += "|";
// Add number or newline
if (gy == 1)
{
std::string text =text+std::to_string(_size - y) + "\n";
} else {
text += "\n";
}
}
}
// Add bottom line
addHorizontalLine(text);\
// Make bottom guide
addGuide(text);
// printf is faster than cout
// Print the string
printf("%s\n", text.c_str());
}
int Board::checkVictory() const
{
bool victory;
int c;
// Check the rows
for (int y = 0; y < _size; y++)
{
c = getVal(0, y);
if (c != NO_VAL)
{
victory = true;
for (int x = 0; x < _size; x++)
{
if (getVal(x, y) != c)
{
victory = false;
break;
}
}
if (victory) return c;
}
}
// Check the columns
for (int x = 0; x < _size; x++)
{
c = getVal(x, 0);
if (c != NO_VAL) {
victory = true;
for (int y = 0; y < _size; y++)
{
if (getVal(x, y) != c)
{
victory = false;
break;
}
}
if (victory) return c;
}
}
// Check top left diagonal
c = getVal(0, 0);
if (c != NO_VAL) {
victory = true;
for (int xy = 0; xy < _size; xy++)
{
if (getVal(xy, xy) != c)
{
victory = false;
break;
}
}
if (victory) return c;
}
// Check top right diagonal
c = getVal(_size - 1, 0);
if (c != NO_VAL)
{
victory = true;
for (int xy = 0; xy < _size; xy++)
{
if (getVal(_size - xy - 1, xy) != c)
{
victory = false;
break;
}
}
if (victory) return c;
}
// Check for tie game
for (int i = 0; i < _board.size(); i++)
{
if (_board[i] == NO_VAL) return NO_VAL;
}
// If we get here, every spot was filled, so return tie
return TIE_VAL;
}
void Board::addHorizontalLine(std::string& s) const
{
s += "-";
for (int x = 0; x < _size; x++)
{
s += "----";
}
s += "--\n";
}
void Board::addGuide(std::string& s) const
{
s += " ";
for (int i = 1; i <= _size; i++)
{
std::string s =s+ "| " + std::to_string(i) + " ";
}
s += "|\n";
}
void AI::init(int aiPlayer)
{
_aiPlayer = aiPlayer;
if (_aiPlayer == X_VAL)
{
_humanPlayer = O_VAL;
} else {
_humanPlayer = X_VAL;
}
}
void AI::performMove(Board& board)
{
AiMove bestMove = getBestMove(board, _aiPlayer);
board.setVal(bestMove.x, bestMove.y, _aiPlayer);
}
AiMove AI::getBestMove(Board& board, int player)
{
// Base case, check for end state
int rv = board.checkVictory();
if (rv == _aiPlayer)
{
return AiMove(10);
} else if (rv == _humanPlayer)
{
return AiMove(-10);
} else if (rv == TIE_VAL)
{
return AiMove(0);
}
std::vector<AiMove> moves;
// Do the recursive function calls and construct the moves vector
for (int y = 0; y < board.getSize(); y++)
{
for (int x = 0; x < board.getSize(); x++)
{
if (board.getVal(x, y) == NO_VAL)
{
AiMove move;
move.x = x;
move.y = y;
board.setVal(x, y, player);
if (player == _aiPlayer)
{
move.score = getBestMove(board, _humanPlayer).score;
} else
{
move.score = getBestMove(board, _aiPlayer).score;
}
moves.push_back(move);
board.setVal(x, y, NO_VAL);
}
}
}
// Pick the best move for the current player
int bestMove = 0;
if (player == _aiPlayer)
{
int bestScore = -1000000;
for (int i = 0; i < moves.size(); i++)
{
if (moves[i].score > bestScore)
{
bestMove = i;
bestScore = moves[i].score;
}
}
} else
{
int bestScore = 1000000;
for (int i = 0; i < moves.size(); i++)
{
if (moves[i].score < bestScore)
{
bestMove = i;
bestScore = moves[i].score;
}
}
}
// Return the best move
return moves[bestMove];
}
void MainGame::run()
{
init();
// Game loop
while (_gameState != GameState::EXIT)
{
// Print out the board
_board.print();
// Say which player is playing
if (_currentPlayer == X_VAL)
{
printf(" Player X's turn!\n\n");
} else
{
printf(" Player O's turn!\n\n");
}
// Perform a move
if (!_isMultiplayer && _currentPlayer == _aiPlayer)
{
aiMove();
} else {
playerMove();
}
// Check victory condition
int rv = _board.checkVictory();
if (rv != NO_VAL)
{
endGame(rv == TIE_VAL);
} else {
changePlayer();
// printf is faster than cout
// Add a bunch of blank space to "clear" the command prompt
for (int i = 0; i < 3; i++)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
}
}
}
void MainGame::init()
{
_gameState = GameState::PLAYING;
_board.init(3);
_currentPlayer = X_VAL;
printf("\n\n****** Welcome to Tic-Tac-Toe ******\n\n");
// Check for multiplayer
printf("\nMultiplayer? (y/n):");
char input = ' ';
bool isValid;
do {
isValid = true;
if (!(std::cin >> input))
{
std::cout << "Invalid input!";
std::cin.clear();
std::cin.ignore(1000, '\n');
isValid = false;
} else if (input == 'y' || input == 'Y')
{
_isMultiplayer = true;
} else
{
_isMultiplayer = false;
}
} while (isValid == false);
if (!_isMultiplayer)
{
// Get player
printf("\nWould you like to be X or O:");
do {
isValid = true;
if (!(std::cin >> input))
{
std::cout << "Invalid input!";
std::cin.clear();
std::cin.ignore(1000, '\n');
isValid = false;
} else if (input == 'x' || input == 'X')
{
_aiPlayer = O_VAL;
} else if (input == 'o' || input == 'O')
{
_aiPlayer = X_VAL;
} else {
std::cout << "Must enter X or O!";
isValid = false;
}
} while (isValid == false);
// Init the AI player
_ai.init(_aiPlayer);
}
printf("\n\n");
}
void MainGame::playerMove()
{
// Get input
bool wasValid = false;
int x, y;
do {
printf("Enter the X: ");
while (!(std::cin >> x))
{
std::cin.clear();
std::cin.ignore(1000, '\n');
printf("ERROR: Invalid input!");
}
printf("Enter the Y: ");
while (!(std::cin >> y))
{
std::cin.clear();
std::cin.ignore(1000, '\n');
printf("ERROR: Invalid input!");
}
if (x < 1 || y < 1 || x > _board.getSize() || y > _board.getSize())
{
printf("ERROR: Invalid X or Y!\n");
} else if (_board.getVal(x - 1, y - 1) != 0)
{
printf("ERROR: That spot is taken!\n");
} else
{
wasValid = true;
}
} while (!wasValid);
// Now place the token
_board.setVal(x - 1, y - 1, _currentPlayer);
}
void MainGame::aiMove()
{
_ai.performMove(_board);
}
void MainGame::changePlayer()
{
if (_currentPlayer == X_VAL)
{
_currentPlayer = O_VAL;
} else
{
_currentPlayer = X_VAL;
}
}
void MainGame::endGame(bool wasTie)
{
_board.print();
if (wasTie)
{
printf("\n\n Tie game! Enter any key to play again, or Z to exit: ");
} else
{
if (_currentPlayer == X_VAL)
{
printf("\n\n Player X wins! Enter any key to play again, or Z to exit: ");
} else
{
printf("\n\n Player O wins! Enter any key to play again, or Z to exit: ");
}
}
char input;
std::cin >> input;
if (input == 'Z' || input == 'z')
{
_gameState = GameState::EXIT;
} else
{
// Re init the game
init();
}
}
int main()
{
try
{
// Start the game
MainGame mainGame;
mainGame.run();
}
catch (std::exception const &exc)
{
std::cerr << "Exception caught " << exc.what() << "\n";
}
catch (...)
{
std::cerr << "Unknown exception caught\n";
}
return 0;
}
// This marks the End