-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepictoe.cpp
493 lines (464 loc) · 15.3 KB
/
epictoe.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
// Epic TicTacToe.cpp
// Written by David Whittaker
#include <iostream>
#include <cassert>
// Found color codes here https://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c
#define YELLOW "\x1B[33m"
#define BLUE "\x1B[94m"
#define RED "\x1B[31m"
#define GREEN "\x1B[32m"
#define BRIGHT_RED "\x1B[91m"
#define CYAN "\x1B[36m"
#define END_COLOR "\033[0m"
#define MAX_PLAYERS 2
#define MINE_HIDDEN 1
#define MINE_FOUND 2
using namespace std;
class Board {
public:
/**
* Sets up main board
* params:
* m - number of rows
* n - number of cols
* k - number of spaces to win
* mines - number of mines
**/
// TODO - think of ways to split up this class
Board(int m, int n, int k, int mines) {
board = new int*[m];
mine_locations = new int*[m];
for (int i = 0; i < m; i++) {
board[i] = new int[n];
mine_locations[i] = new int[n];
}
this->m = m;
this->n = n;
this->mines = mines;
this->win = k;
init_board();
}
~Board() {
// free memory
for (int i = 0; i < m; i++) {
delete[] board[i];
delete[] mine_locations[i];
}
delete[] board;
delete[] mine_locations;
}
/**
* Initializes all spaces to 0, sets up mines
**/
void init_board() {
int mine_count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = 0;
mine_locations[i][j] = 0;
}
}
for (int i = 0; i < mines; i++) {
while (true) {
int row = rand() % m;
int col = rand() % n;
if (!mine_locations[row][col]) {
mine_locations[row][col] = MINE_HIDDEN;
// cout << "setting mine at location (" << row << ", " << col << ")\n";
break;
}
}
}
}
/**
* Prints the current board with M for found mines
**/
void print_board() {
print_top();
for (int i = 0; i < m; i++) {
cout << BLUE << char('A' + i) << YELLOW << " |" << END_COLOR;
for (int j = 0; j < n; j++) {
if (mine_locations[i][j] == MINE_FOUND) {
cout << RED << " M" << END_COLOR;
}
else {
switch (board[i][j]) {
case 0:
cout << " ";
break;
case 1:
cout << BRIGHT_RED << " X" << END_COLOR;
break;
case 2:
cout << GREEN << " O" << END_COLOR;
break;
}
}
if (j < n) cout << YELLOW << " |" << END_COLOR;
}
cout << YELLOW << "\n +" << END_COLOR;
if (i < m - 1) {
for (int i = 0; i < n - 1; i++) {
cout << YELLOW << "---+" << END_COLOR;
}
cout << YELLOW << "---+\n" << END_COLOR;
}
}
print_line();
}
/**
* Sets a mark on the board
* params:
* m - row number (0-based)
* n - column number (0-based)
* player - the player number (1 or 2)
**/
void set_mark(int m, int n, int player) {
board[m][n] = player;
}
/**
* Checks whether a player has won the game
* params:
* player - the player number (1 or 2)
**/
bool check_win(int player) {
//check cols
int count;
for (int i = 0; i < m; i++) {
count = 0;
for (int j = 0; j < n; j++) {
if (board[i][j] == player) {
count++;
if (count == win) return true;
}
else {
count = 0;
}
}
}
//check rows
for (int j = 0; j < n; j++) {
count = 0;
for (int i = 0; i < m; i++) {
if (board[i][j] == player) {
count++;
if (count == win) return true;
}
else {
count = 0;
}
}
}
//check forward diags:
for (int i = 0; i <= m - win; i++) {
count = 0;
for (int j = 0; j <= n - win; j++) {
if (board[i][j] == player) {
// check next win diagonals
for (int k = 0; k < win; k++) {
if (board_val(i + k, j + k) == player) {
count++;
if (count == win) return true;
}
else {
count = 0;
break;
}
}
}
else {
count = 0;
}
}
}
//check reverse diags:
for (int i = 0; i <= m - win; i++) {
count = 0;
for (int j = n; j >= win - 1; j--) {
if (board[i][j] == player) {
// check next win diagonals
for (int k = 0; k < win; k++) {
if (board_val(i + k, j - k) == player) {
count++;
if (count == win) return true;
}
else {
count = 0;
break;
}
}
}
else {
count = 0;
}
}
}
return false;
}
/**
* Determines whether there are any spaces remaining on the board
**/
bool no_more_moves() {
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] > 0 || mine_locations[i][j] == MINE_FOUND) {
count++;
}
}
}
return count == m * n;
}
/**
* Gets the number of rows
**/
int get_num_rows() {
return this->m;
}
/**
* Gets the number of columns
**/
int get_num_cols() {
return this->n;
}
/**
* Determines if a mine is visible to the user at a location
* params:
* row - row number (0-based)
* col - column number (0-based)
**/
bool is_exploded_mine_at(int row, int col) {
return mine_locations[row][col] == MINE_FOUND;
}
/**
* Determines if there is a not-yet-visible mine at a location
* params:
* row - row number (0-based)
* col - column number (0-based)
**/
bool is_unexploded_mine_at(int row, int col) {
return mine_locations[row][col] == MINE_HIDDEN;
}
/**
* Sets the mine at a location to visible
* params:
* row - row number (0-based)
* col - column number (0-based)
**/
void explode_mine_at(int row, int col) {
mine_locations[row][col] = MINE_FOUND;
}
/**
* Gets the mark (if any) of a player at a location
* params:
* row - row number (0-based)
* col - column number (0-based)
**/
int get_mark_at(int row, int col) {
return board[row][col];
}
private:
int** board;
int** mine_locations;
int m, n, win, mines = 0;
// prints full divider line
void print_line() {
for (int j = 0; j < n - 1; j++) {
cout << YELLOW << "---+" << END_COLOR;
}
cout << YELLOW << "---+\n" << END_COLOR;
}
// prints top of grid
void print_top() {
cout << " ";
for (int j = 0; j < n; j++) {
cout << BLUE << " " << char('a' + j) << " " << END_COLOR;
}
cout << YELLOW << "\n +" << END_COLOR;
print_line();
}
// provides a safe way to get the value of the board at a position without worrying about buffer overflow
int board_val(int i, int j) {
if (i >= m || j >= n) {
return 0;
}
return board[i][j];
}
};
// test cases
void test_game() {
auto game = new Board(8, 8, 4, 0);
//column win
game->set_mark(1, 4, 1);
game->set_mark(2, 4, 1);
game->set_mark(3, 4, 1);
game->set_mark(4, 4, 1);
game->print_board();
assert(game->check_win(1) == true);
game->init_board();
//row win
game->set_mark(7, 0, 2);
game->set_mark(7, 1, 2);
game->set_mark(7, 2, 2);
game->set_mark(7, 3, 2);
game->print_board();
assert(game->check_win(2) == true);
//forward diag win
game->init_board();
game->set_mark(4, 4, 2);
game->set_mark(5, 5, 2);
game->set_mark(6, 6, 2);
game->set_mark(7, 7, 2);
game->print_board();
assert(game->check_win(2) == true);
//reverse diag win
game->init_board();
game->set_mark(4, 7, 2);
game->set_mark(5, 6, 2);
game->set_mark(6, 5, 2);
game->set_mark(7, 4, 2);
game->print_board();
assert(game->check_win(2) == true);
}
/**
* Gets the initial board dimensions and parameters from the user and sanitizes
**/
Board* setup_board() {
int rows = 0, cols = 0, win = 0, mines = 0;
bool valid_input = false;
while (!valid_input) {
cout << "Enter the number of rows, columns, spaces in a row to win, and mines (separated by spaces, e.g, 3 3 3 0): " << END_COLOR;
if (cin >> rows >> cols >> win >> mines) {
if (rows < 3 || rows > 26) {
cout << RED << "Invalid input. The number of rows must be at least 3 but not more than 26.\n" << END_COLOR;
}
else if (cols < 3 || cols > 26) {
cout << RED << "Invalid input. The number of columns must be at least 3 but not more than 26.\n" << END_COLOR;
}
else if (win < 1) {
cout << RED << "Invalid input. The number of spaces to win must be at least 1.\n" << END_COLOR;
}
else if (win > rows || win > cols) {
cout << RED << "Invalid input. The number of spaces to win cannot be larger than the number of rows or columns.\n" << END_COLOR;
}
else if (mines > rows * cols) {
cout << RED << "Invalid input. The number of mines must be less than the number of spaces on the board.\n" << END_COLOR;
}
else {
valid_input = true;
}
}
else
{
cout << RED << "Invalid input. Please enter 4 integers separated by spaces, e.g., 3 3 3 0.\n" << END_COLOR;
cin.clear();
cin.ignore();
while ((getchar()) != '\n');
}
}
return new Board(rows, cols, win, mines);
}
/**
* Takes one turn - explodes mine if necessary
**/
void take_turn(Board* board, int player) {
string space;
bool valid_input = false;
int row = 0, col = 0;
while (!valid_input) {
cin.clear();
cin.ignore();
cout << "Player " << player << " (" << (player == 1 ? "X" : "O") << ") - it's your turn! Choose a space (e.g., Aa): ";
if (cin >> space) {
if (space.length() != 2) {
cout << RED << "Invalid input. Please enter a valid space (e.g., Aa).\n\n" << END_COLOR;
}
else if (space.at(0) < 'A' || space.at(0) > 'Z') {
cout << RED << "Invalid input. The first character should be a capital letter (e.g., A).\n\n" << END_COLOR;
}
else if (space.at(1) < 'a' || space.at(1) > 'z') {
cout << RED << "Invalid input. The second character should be a lowercase letter (e.g., a).\n\n" << END_COLOR;
}
else if (space.at(0) - 'A' >= board->get_num_rows()) {
cout << RED << "Invalid input. The rows only go from A to " << char(board->get_num_rows() - 1 + 'A') << ".\n\n" << END_COLOR;
}
else if (space.at(1) - 'a' >= board->get_num_cols()) {
cout << RED << "Invalid input. The columns only go from a to " << char(board->get_num_cols() - 1 + 'a') << ".\n\n" << END_COLOR;
}
else {
row = space.at(0) - 'A';
col = space.at(1) - 'a';
int player_at_mark = board->get_mark_at(row, col);
if (board->is_exploded_mine_at(row, col)) {
cout << RED << "Invalid space. There's already a mine at this location!\n\n" << END_COLOR;
}
else if (player_at_mark > 0) {
cout << RED << "Invalid space. Player " << player_at_mark << " has already chosen this location!\n\n" << END_COLOR;
}
else {
valid_input = true;
}
}
}
else {
cout << RED << "Invalid input. Please enter a valid space (e.g, Aa).\n\n" << END_COLOR;
while ((getchar()) != '\n');
}
}
if (board->is_unexploded_mine_at(row, col)) {
cout << RED << "**BOOM!!** There's a mine at this location!\n" << END_COLOR;
board->explode_mine_at(row, col);
}
else {
board->set_mark(row, col, player);
}
}
/**
* Main entry point for the game
**/
void run_game() {
auto board = setup_board();
bool player_has_won = false;
int player = 1;
while (!player_has_won) {
board->print_board();
take_turn(board, player);
if (board->check_win(player)) {
board->print_board();
cout << CYAN << "*** PLAYER " << player << " WINS! ***" << END_COLOR << "\n";
break;
}
if (board->no_more_moves()) {
board->print_board();
cout << CYAN << "*** CAT'S EYE - no more moves ***" << END_COLOR << "\n";
break;
}
player++;
if (player > MAX_PLAYERS) {
player = 1;
}
}
delete board;
}
int main()
{
cout << BLUE << "Welcome to Epic Tic-Tac-Toe!\n" << END_COLOR;
cout << BRIGHT_RED << "Epic Tic-Tac-Toe" << END_COLOR << " is a thrilling and explosive extension of the classic game we all know and love!";
cout << "In this exciting twist on the traditional game, you'll face off against your opponents on a board of up to 26 rows and columns, ";
cout << "providing a challenging and dynamic gameplay experience.But that's not all - hidden throughout the board are dangerous mines ";
cout << "that can detonate at any moment!If you attempt to claim a space with a mine, it will explode, causing you to lose your turn and ";
cout << "rendering that space unclaimable for the rest of the game.This adds an element of danger and excitement to the game, as you must ";
cout << "strategically navigate the board and avoid the hidden dangers while trying to outmaneuver your opponent.\n";
cout << "Get ready to experience the ultimate test of skill and strategy with " << BRIGHT_RED << "Epic Tic-Tac-Toe!\n\n" << END_COLOR;
//test_game();
while (true) {
run_game();
string play_again = "Y";
cout << "\nWould you like to play again? (Y/n)? ";
cin >> play_again;
if (play_again == "n" || play_again == "N") {
cout << "\nGoodbye!";
return 0;
}
}
}