-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXO_Demo.cpp
More file actions
495 lines (385 loc) · 13.2 KB
/
XO_Demo.cpp
File metadata and controls
495 lines (385 loc) · 13.2 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
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <memory>
#include <limits>
#include <cstdlib>
#include "XO_Classes.h"
#include "NumericalTicTacToe.h"
#include "BoardGame_Classes.h"
#include "SUS.h"
#include "5x5 Tic Tac Toe.h"
#include "Misere_Board.h"
#include "Infinity_XO.h"
#include "T4x4_Classes.h"
#include "Memory_XO.h"
#include "FourInRow.h"
#include "Word_Tic_Tac_Toe.h"
#include "Ultimate_TTT.h"
#include "pyramid.h"
#include "Diamond.h"
#include "Obstacle.h"
using namespace std;
enum class MatchResult {
PLAYER1_WIN,
PLAYER2_WIN,
DRAW,
UNKNOWN
};
struct TournamentScore {
string player1Name = "Player 1";
string player2Name = "Player 2";
PlayerType player1Type = PlayerType::HUMAN;
PlayerType player2Type = PlayerType::HUMAN;
int player1Score = 0;
int player2Score = 0;
int draws = 0;
int gamesPlayed = 0;
};
string player_type_to_string(PlayerType type) {
return type == PlayerType::COMPUTER ? "Computer" : "Human";
}
void clear_score(TournamentScore& score) {
score.player1Score = 0;
score.player2Score = 0;
score.draws = 0;
score.gamesPlayed = 0;
}
void pause_screen() {
cout << "\nPress Enter to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
}
int read_int_in_range(const string& prompt, int minValue, int maxValue) {
int value;
while (true) {
cout << prompt;
cin >> value;
if (!cin.fail() && value >= minValue && value <= maxValue) {
return value;
}
cout << "Invalid choice. Please try again.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
string read_name(const string& prompt) {
string name;
while (true) {
cout << prompt;
getline(cin >> ws, name);
if (!name.empty()) {
return name;
}
cout << "Name cannot be empty. Try again.\n";
}
}
PlayerType read_player_type(const string& playerLabel) {
cout << "\nChoose " << playerLabel << " type:\n";
cout << "1. Human\n";
cout << "2. Computer\n";
int choice = read_int_in_range("Choice: ", 1, 2);
return choice == 2 ? PlayerType::COMPUTER : PlayerType::HUMAN;
}
void configure_players(TournamentScore& score) {
cout << "\n====================================\n";
cout << " PLAYER SETUP\n";
cout << "====================================\n";
score.player1Name = read_name("Enter Player 1 name: ");
score.player1Type = read_player_type(score.player1Name);
cout << "\n";
score.player2Name = read_name("Enter Player 2 name: ");
score.player2Type = read_player_type(score.player2Name);
clear_score(score);
cout << "\nPlayers configured successfully.\n";
}
void show_score(const TournamentScore& score) {
cout << "\n====================================\n";
cout << " SCOREBOARD\n";
cout << "====================================\n";
cout << score.player1Name << " (" << player_type_to_string(score.player1Type) << "): "
<< score.player1Score << "\n";
cout << score.player2Name << " (" << player_type_to_string(score.player2Type) << "): "
<< score.player2Score << "\n";
cout << "Draws: " << score.draws << "\n";
cout << "Games Played: " << score.gamesPlayed << "\n";
cout << "====================================\n";
}
void reset_score(TournamentScore& score) {
clear_score(score);
cout << "\nScore has been reset successfully.\n";
}
void update_score(TournamentScore& score, MatchResult result) {
if (result == MatchResult::PLAYER1_WIN) {
score.player1Score++;
score.gamesPlayed++;
cout << "\nResult recorded automatically: " << score.player1Name << " wins this game.\n";
}
else if (result == MatchResult::PLAYER2_WIN) {
score.player2Score++;
score.gamesPlayed++;
cout << "\nResult recorded automatically: " << score.player2Name << " wins this game.\n";
}
else if (result == MatchResult::DRAW) {
score.draws++;
score.gamesPlayed++;
cout << "\nResult recorded automatically: Draw.\n";
}
else {
cout << "\nCould not detect the result automatically.\n";
cout << "Score was not changed.\n";
}
}
template <typename T>
MatchResult detect_result(Board<T>* board, Player<T>** players) {
if (board == nullptr || players == nullptr || players[0] == nullptr || players[1] == nullptr) {
return MatchResult::UNKNOWN;
}
if (board->is_win(players[0])) {
return MatchResult::PLAYER1_WIN;
}
if (board->is_win(players[1])) {
return MatchResult::PLAYER2_WIN;
}
if (board->is_lose(players[0])) {
return MatchResult::PLAYER2_WIN;
}
if (board->is_lose(players[1])) {
return MatchResult::PLAYER1_WIN;
}
if (board->is_draw(players[0]) || board->is_draw(players[1])) {
return MatchResult::DRAW;
}
return MatchResult::UNKNOWN;
}
Player<char>** create_char_players(
UI<char>* ui,
const TournamentScore& score,
char player1Symbol = 'X',
char player2Symbol = 'O'
) {
Player<char>** players = new Player<char>*[2];
string name1 = score.player1Name;
string name2 = score.player2Name;
players[0] = ui->create_player(name1, player1Symbol, score.player1Type);
players[1] = ui->create_player(name2, player2Symbol, score.player2Type);
return players;
}
Player<int>** create_int_players(
UI<int>* ui,
const TournamentScore& score,
int player1Symbol = 1,
int player2Symbol = 2
) {
Player<int>** players = new Player<int>*[2];
string name1 = score.player1Name;
string name2 = score.player2Name;
players[0] = ui->create_player(name1, player1Symbol, score.player1Type);
players[1] = ui->create_player(name2, player2Symbol, score.player2Type);
return players;
}
template <typename T>
void cleanup_game(Board<T>* board, UI<T>* ui, Player<T>** players) {
delete board;
delete ui;
if (players != nullptr) {
for (int i = 0; i < 2; ++i) {
delete players[i];
}
delete[] players;
}
}
template <typename BoardType, typename UIType>
MatchResult run_standard_char_game(const TournamentScore& score) {
UI<char>* ui = new UIType();
Board<char>* board = new BoardType();
Player<char>** players = create_char_players(ui, score, 'X', 'O');
GameManager<char> game(board, players, ui);
game.run();
MatchResult result = detect_result(board, players);
cleanup_game(board, ui, players);
return result;
}
template <typename BoardType, typename UIType>
MatchResult run_standard_int_game(const TournamentScore& score) {
UI<int>* ui = new UIType();
Board<int>* board = new BoardType();
Player<int>** players = create_int_players(ui, score, 1, 2);
GameManager<int> game(board, players, ui);
game.run();
MatchResult result = detect_result(board, players);
cleanup_game(board, ui, players);
return result;
}
MatchResult run_sus_game_with_result(const TournamentScore& score) {
UI<char>* ui = new SUS_UI();
Board<char>* board = new SUS_Board();
// SUS uses S and U, so Player 1 gets S and Player 2 gets U.
Player<char>** players = create_char_players(ui, score, 'S', 'U');
run_sus_game(board, players, ui);
MatchResult result = detect_result(board, players);
cleanup_game(board, ui, players);
return result;
}
MatchResult run_pyramid_game_with_result(const TournamentScore& score) {
PyramidBoard* board = new PyramidBoard();
PyramidUI* ui = new PyramidUI(board);
Player<char>** players = create_char_players(ui, score, 'X', 'O');
GameManager<char> game(board, players, ui);
game.run();
MatchResult result = detect_result<char>(board, players);
delete board;
delete ui;
for (int i = 0; i < 2; ++i) {
delete players[i];
}
delete[] players;
return result;
}
MatchResult run_ultimate_game_with_result(const TournamentScore& score) {
UltimateTTT_UI* ui = new UltimateTTT_UI();
UltimateTTT_Board* board = new UltimateTTT_Board();
Player<char>** players = create_char_players(ui, score, 'X', 'O');
UltimateTTT_Manager mgr(board, players, ui);
mgr.run();
MatchResult result = detect_result<char>(board, players);
delete board;
delete ui;
for (int i = 0; i < 2; ++i) {
delete players[i];
}
delete[] players;
return result;
}
void show_games_menu() {
cout << "\n====================================\n";
cout << " CHOOSE A GAME\n";
cout << "====================================\n";
cout << "1. Play X-O (Tic-Tac-Toe)\n";
cout << "2. Play Numerical Tic-Tac-Toe\n";
cout << "3. Play SUS\n";
cout << "4. Play 5x5 Tic Tac Toe\n";
cout << "5. Play Misere\n";
cout << "6. Play Four-in-a-Row\n";
cout << "7. Play 4x4 Tic-Tac-Toe\n";
cout << "8. Play Word Tic-Tac-Toe\n";
cout << "9. Play Pyramid Tic-Tac-Toe\n";
cout << "10. Play Diamond\n";
cout << "11. Play Infinity XO\n";
cout << "12. Play Ultimate XO\n";
cout << "13. Play Memory XO\n";
cout << "14. Play Obstacle\n";
cout << "0. Back to main menu\n";
cout << "====================================\n";
}
MatchResult play_selected_game(int choice, const TournamentScore& score) {
switch (choice) {
case 1:
return run_standard_char_game<X_O_Board, XO_UI>(score);
case 2:
return run_standard_int_game<NumericalTTT_Board, NumericalTTT_UI>(score);
case 3:
return run_sus_game_with_result(score);
case 4:
return run_standard_char_game<Five_TTT_Board, Five_TTT_UI>(score);
case 5:
return run_standard_char_game<MISERE_Board, MISERE_UI>(score);
case 6:
return run_standard_char_game<FourInRow_Board, FourInRow_UI>(score);
case 7:
return run_standard_char_game<T4x4_Board, T4x4_UI>(score);
case 8:
return run_standard_char_game<Word_Tic_Tac_Toe_Board, Word_Tic_Tac_Toe_UI>(score);
case 9:
return run_pyramid_game_with_result(score);
case 10:
return run_standard_char_game<DIAMOND_Board, DIAMOND_UI>(score);
case 11:
return run_standard_char_game<Infinity_XO_Board, Infinity_XO_UI>(score);
case 12:
return run_ultimate_game_with_result(score);
case 13:
return run_standard_char_game<Memory_XO_Board, Memory_XO_UI>(score);
case 14:
return run_standard_char_game<Obstacle_Board, Obstacle_UI>(score);
default:
return MatchResult::UNKNOWN;
}
}
void play_game_flow(TournamentScore& score) {
while (true) {
show_score(score);
show_games_menu();
int gameChoice = read_int_in_range("Choose game: ", 0, 14);
if (gameChoice == 0) {
return;
}
MatchResult result = play_selected_game(gameChoice, score);
update_score(score, result);
show_score(score);
cout << "\nWhat would you like to do next?\n";
cout << "1. Play another game\n";
cout << "2. Back to main menu\n";
cout << "0. Exit program\n";
int next = read_int_in_range("Choice: ", 0, 2);
if (next == 1) {
continue;
}
else if (next == 2) {
return;
}
else {
cout << "\nFinal score:\n";
show_score(score);
cout << "Goodbye!\n";
exit(0);
}
}
}
void show_main_menu() {
cout << "\n====================================\n";
cout << " BOARD GAMES ARENA\n";
cout << "====================================\n";
cout << "1. Play / Choose Game\n";
cout << "2. Show Scoreboard\n";
cout << "3. Reset Score\n";
cout << "4. Change Players / Types\n";
cout << "0. Exit\n";
cout << "====================================\n";
}
int main() {
srand(static_cast<unsigned int>(time(0)));
TournamentScore score;
cout << "====================================\n";
cout << " BOARD GAMES ARENA\n";
cout << "====================================\n";
configure_players(score);
while (true) {
show_score(score);
show_main_menu();
int mainChoice = read_int_in_range("Choose option: ", 0, 4);
if (mainChoice == 1) {
play_game_flow(score);
}
else if (mainChoice == 2) {
show_score(score);
pause_screen();
}
else if (mainChoice == 3) {
reset_score(score);
pause_screen();
}
else if (mainChoice == 4) {
configure_players(score);
pause_screen();
}
else if (mainChoice == 0) {
cout << "\nFinal score:\n";
show_score(score);
cout << "Goodbye!\n";
break;
}
}
return 0;
}