-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
691 lines (568 loc) · 17.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#include <iostream>
#include <fstream>
#include <stdio.h>
#include "constants.h"
#include "components.h"
using namespace std;
/* Global Variables */
Player players[2];
string board[3][3];
/* Functions Declarations */
void welcome_scene();
void stage_scene();
void history_scene();
void handle_choices(int);
/* General Functions */
// This function clears the entire screen.
inline void clear () {
// Use "CLS" instead of "clear" in Windows.
system("clear");
}
// This function gets an integer value from the user.
// Inputs:
// message : the message that will be shown before getting the value (default: >>)
// Outputs:
// int : the value passed by user
inline int input_int (string message = ">>") {
cout << message << " ";
int n;
cin >> n;
return n;
}
// This function capitalize all the letters inside a string.
// Inputs:
// content : the initial string
// Outputs:
// string : the capitalized string
inline string toupper (string content) {
for (int i = 0; i < content.length(); i++) {
content[i] = toupper(content[i]);
}
return content;
}
// This function converts the ordinal numbers to words
// Inputs:
// times : the ordinal number
// Outputs:
// string : the ordinal number in words
string timize (int times) {
if (times == 1) {
return "once";
} else if (times == 2) {
return "twice";
} else {
return to_string(times) + " times";
}
}
/* Operating Functions */
// This function decides that who must start the game.
// Inputs:
// n1 : the name of player one
// n2 : the name of player two
// Outputs:
// string : the name of player who must start the game
int discover_starter (string n1, string n2) {
ifstream fin(HISTORY_PATH);
if (!fin.is_open()) {
return -1;
}
int winner = -1;
while (!fin.eof()) {
string p1, p2;
int w1, w2;
fin >> p1 >> p2;
fin >> w1 >> w2;
if (p1 == n1 && p2 == n2) {
if (w1 > w2) {
winner = 0;
} else if (w2 > w1) {
winner = 1;
}
}
if (p1 == n2 && p2 == n1) {
if (w1 > w2) {
winner = 1;
} else if (w2 > w1) {
winner = 0;
}
}
}
fin.close();
return winner;
}
// This function updates the history file of wins.
// Inputs:
// n1 : the name of player one
// n2 : the name of player two
// winner : the index of winner
void update_history (string n1, string n2, int winner) {
int nw1 = 0, nw2 = 0;
if (winner == 0) {
nw1 = 1;
} else if (winner == 1) {
nw2 = 1;
}
ofstream fout(HISTORY_BUFFER_PATH);
ifstream fin(HISTORY_PATH);
if (!fin.is_open()) {
fout << n1 << " " << n2 << " " << nw1 << " " << nw2;
} else {
bool does_exist = false;
while (!fin.eof()) {
string p1 = "", p2 = "";
int w1, w2;
fin >> p1 >> p2;
fin >> w1 >> w2;
if (n1 == p1 && n2 == p2) {
w1 += nw1;
w2 += nw2;
does_exist = true;
} else if (n1 == p2 && n2 == p1) {
w1 += nw2;
w2 += nw1;
does_exist = true;
}
if (p1 != "" && p2 != "") {
fout << p1 << " " << p2 << " " << w1 << " " << w2 << endl;
}
}
if (!does_exist) {
fout << n1 << " " << n2 << " " << nw1 << " " << nw2 << endl;
}
}
fin.close();
fout.close();
remove(HISTORY_PATH.c_str());
rename(HISTORY_BUFFER_PATH.c_str(), HISTORY_PATH.c_str());
}
// This function initializes all cells of the board with empty string.
void initialize_board () {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = "";
}
}
}
// This function initializes the default details of players.
void initialize_players () {
players[0].symbol = 'B';
players[1].symbol = 'R';
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
players[i].pieces[j] = 2;
}
}
}
// This function clears the entire screen and displays the current situation of the board.
void display_board () {
clear();
cout << " ";
for (int i = 0; i < 3; i++) {
cout << " 0" << i + 1 << " ";
}
cout << endl;
cout << " " << string(3 * 4 + 4, '-') << endl;
for (int i = 0; i < 3; i++) {
cout << "0" << i + 1 << " | ";
for (int j = 0; j < 3; j++) {
string pieces = board[i][j];
if (pieces.length() >= 2) {
cout << pieces.substr(pieces.length() - 2, 2);
} else {
cout << " ";
}
cout << " | ";
}
cout << endl;
cout << " ";
if (i != 2) {
for (int i = 0; i < 3 * 4 + 4; i++) {
if (i % 5 == 0) {
cout << "|";
} else {
cout << "-";
}
}
cout << endl;
}
}
cout << string(3 * 4 + 4, '-') << endl;
}
// This function displays the menu and gets the choice of the user.
void handle_menu () {
cout << "Available Commands:" << endl;
cout << "(1) Start a New Game" << endl;
cout << "(2) See the History" << endl;
cout << "(3) Exit the Game" << endl;
int choice = input_int("Please choose a command:");
switch (choice) {
case 1:
stage_scene();
break;
case 2:
history_scene();
break;
case 3:
exit(0);
break;
default:
cout << "Error: Invalid command!" << endl;
cin.clear();
cin.ignore(1000, '\n');
handle_menu();
break;
}
}
// This function checks whether the game must be finished or not.
// Outputs:
// string : the symbol of the winner if someone won, or ' ' if nothing happened
char check_board () {
string top_board[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j].length() < 2) {
top_board[i][j] = " ";
} else {
top_board[i][j] = board[i][j].substr(board[i][j].length() - 2, 2);
}
}
}
for (int i = 0; i < 3; i++) {
if (top_board[i][0][0] == top_board[i][1][0] && top_board[i][1][0] == top_board[i][2][0]) {
if (top_board[i][0][0] != ' ') {
return top_board[i][0][0];
}
}
}
for (int j = 0; j < 3; j++) {
if (top_board[0][j][0] == top_board[1][j][0] && top_board[1][j][0] == top_board[2][j][0]) {
if (top_board[0][j][0] != ' ') {
return top_board[0][j][0];
}
}
}
if (top_board[0][0][0] == top_board[1][1][0] && top_board[1][1][0] == top_board[2][2][0]) {
if (top_board[0][0][0] != ' ') {
return top_board[0][0][0];
}
}
if (top_board[0][2][0] == top_board[1][1][0] && top_board[1][1][0] == top_board[2][0][0]) {
if (top_board[0][2][0] != ' ') {
return top_board[0][2][0];
}
}
return ' ';
}
// This function clears the entire screen and gets the details of the players.
void handle_players () {
clear();
cin.ignore();
while (true) {
cout << "Please enter details of players: " << endl << endl;
cout << "[1] First Player" << endl;
cout << " Symbol: " << players[0].symbol << endl;
cout << " Name: ";
cin >> players[0].name;
cout << endl;
cout << "[2] Second Player" << endl;
cout << " Symbol: " << players[1].symbol << endl;
cout << " Name: ";
cin >> players[1].name;
cout << endl;
if (players[0].name == players[1].name) {
cout << endl << "Error: Names can't be the same!" << endl;
continue;
}
break;
}
}
// This function ends the game by displaying the winner and then the menu to handle further actions.
// Inputs:
// winner : the player who won the game
void end_game (char winner) {
display_board();
cout << endl;
Player player;
for (int i = 0; i < 2; i++) {
if (players[i].symbol == winner) {
player = players[i];
update_history(players[0].name, players[1].name, i);
break;
}
}
cout << "Congratulations to Player '" << player.symbol << "'!" << endl;
cout << player.name << " won the game." << endl;
cout << endl;
handle_menu();
}
// This function handles placing a piece on the board by a specific player.
// Inputs:
// player : the id of the player who wants to place a piece (default: 0)
void handle_placing (int player = 0) {
int row = -1;
int column = -1;
string piece = "";
int size_of_choice = 0;
while (true) {
cin.clear();
cin.ignore(1000, '\n');
row = input_int("Please enter the row:") - 1;
column = input_int("Please enter the column:") - 1;
if (row < 0 || row > 2 || column < 0 || column > 2) {
cout << "Error: Row or column is out of range! Please try another." << endl;
continue;
}
string pieces = board[row][column];
int size_of_prepiece = 0;
if (pieces.length() >= 2) {
size_of_prepiece = pieces[pieces.length() - 1] - '0';
}
if (size_of_prepiece > 2) {
cout << "Error: You can't place any piece on this cell." << endl;
} else {
break;
}
cin.ignore();
}
while (true) {
cin.ignore();
cout << "Please enter the piece: ";
cin >> piece;
piece = toupper(piece);
if (piece.length() != 2) {
cout << "Error: Invalid choice of piece!" << endl;
continue;
}
size_of_choice = piece[1] - '0';
if (piece[0] != players[player].symbol || size_of_choice < 1 || size_of_choice > 3) {
cout << "Error: Invalid choice of piece!" << endl;
continue;
}
if (players[player].pieces[size_of_choice - 1] < 1) {
cout << "Error: You don't have this piece" << endl;
continue;
}
string pieces = board[row][column];
int size_of_prepiece = 0;
if (pieces.length() >= 2) {
size_of_prepiece = pieces[pieces.length() - 1] - '0';
}
if (size_of_choice <= size_of_prepiece) {
cout << "Error: You can place only a larger piece on a smaller piece!" << endl;
continue;
}
break;
}
board[row][column] += piece;
players[player].pieces[size_of_choice - 1]--;
display_board();
if (player == 0) {
handle_choices(1);
} else {
handle_choices(0);
}
}
// This function handles moving a piece across the board by a specific player.
// Inputs:
// player : the id of the player who wants to move a piece (default: 0)
void handle_moving (int player = 0) {
int source_row = -1;
int source_column = -1;
int target_row = -1;
int target_column = -1;
int size_of_source = 0;
while (true) {
cin.clear();
cin.ignore(1000, '\n');
source_row = input_int("Source Row:") - 1;
source_column = input_int("Source Column:") - 1;
if (source_row < 0 || source_row > 2 || source_column < 0 || source_column > 2) {
cout << "Error: Source row or column is out of range!" << endl;
continue;
}
string pieces = board[source_row][source_column];
if (pieces.length() < 2) {
cout << "Error: There is no piece to move!" << endl;
continue;
}
size_of_source = pieces[pieces.length() - 1] - '0';
if (pieces[pieces.length() - 2] != players[player].symbol) {
cout << "Error: You can't move the piece that is not yours!" << endl;
continue;
}
bool applicable = false;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int size = 0;
if (board[i][j].length() >= 2) {
size = board[i][j][board[i][j].length() - 1] - '0';
}
if (size < size_of_source) {
applicable = true;
break;
}
}
if (applicable) break;
}
if (!applicable) {
cout << "Error: You can't move this piece to any cell!" << endl;
continue;
}
break;
}
while (true) {
cin.clear();
cin.ignore(1000, '\n');
target_row = input_int("Target Row:") - 1;
target_column = input_int("Target Column:") - 1;
if (target_row < 0 || target_row > 2 || target_column < 0 || target_column > 2) {
cout << "Error: Target row or column is out of range!" << endl;
continue;
}
string pieces = board[target_row][target_column];
int size_of_target = 0;
if (pieces.length() >= 2) {
size_of_target = pieces[pieces.length() - 1] - '0';
}
if (size_of_source <= size_of_target) {
cout << "Error: You can move only a larger piece onto a smaller piece!" << endl;
continue;
}
break;
}
string source_pieces = board[source_row][source_column];
string piece = source_pieces.substr(source_pieces.length() - 2, 2);
board[source_row][source_column] = source_pieces.substr(0, source_pieces.length() - 2);
string target_pieces = board[target_row][target_column];
board[target_row][target_column] = target_pieces.substr(0, target_pieces.length() - 2);
board[target_row][target_column] += piece;
display_board();
if (player == 0) {
handle_choices(1);
} else {
handle_choices(0);
}
}
// This function handles choices of each player on each turn.
// It will lead calling "handle_placing" or "handle_moving".
// Inputs:
// player : the id of the player who must start the game (default: -1)
// -1 means that the starter will be chosen based on the wins history.
void handle_choices (int player = -1) {
if (player == -1) {
player = discover_starter(players[0].name, players[1].name);
if (player == -1) {
player = 0;
}
}
char result = check_board();
if (result != ' ') {
end_game(result);
return;
}
cout << players[player].name << ", it is your turn." << endl;
cout << "Your symbol is '" << players[player].symbol << "'";
int npieces = 0;
for (int i = 0; i < 3; i++) {
npieces += players[player].pieces[i];
}
if (npieces != 0) {
cout << ", and you have these pieces:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < players[player].pieces[i]; j++) {
cout << players[player].symbol << i + 1 << " ";
}
}
cout << endl;
bool accessible = false;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] != "") {
if (board[i][j][board[i][j].length() - 2] == players[player].symbol) {
accessible = true;
break;
}
}
}
if (accessible) break;
}
if (accessible) {
cout << "What do you want to do?" << endl;
cout << "(1) Place a New Piece" << endl;
cout << "(2) Move an Existing Piece across the Board" << endl;
while (true) {
cin.clear();
cin.ignore(1000, '\n');
int choice = input_int("Please enter your choice:");
if (choice == 1) {
handle_placing(player);
break;
} else if (choice == 2) {
handle_moving(player);
break;
} else {
cout << "Invalid choice!" << endl;
}
}
} else {
handle_placing(player);
}
} else {
cout << ", and you don't have any pieces!" << endl;
cout << "So you can only move the pieces." << endl;
handle_moving(player);
}
}
/* Scenes */
// This function displays the welcome scene (including menu).
void welcome_scene () {
cout << "Enhanced Tic Tac Toe 1.0" << endl;
cout << "Developed by: Kazem Forghani" << endl;
cout << "Updated at: 2022 Jan 20 22:40" << endl;
handle_menu();
}
// This function displays all the necessary scenes to start the game sequentially.
void stage_scene () {
initialize_players();
handle_players();
initialize_board();
display_board();
handle_choices();
}
// This function displays the scene of the wins history.
void history_scene () {
clear();
cout << "History" << endl;
ifstream fin(HISTORY_PATH);
if (!fin.is_open()) {
cout << "There is no history!" << endl;
return;
}
int counter = 0;
while (!fin.eof()) {
string p1, p2;
int w1, w2;
fin >> p1 >> p2;
fin >> w1 >> w2;
if (p1 != "" and p2 != "") {
counter++;
string all_times = timize(w1 + w2);
string w1_times = timize(w1);
string w2_times = timize(w2);
cout << counter << ". " << p1 << " and " << p2 << " have played " << all_times << "." << endl;
cout << " " << p1 << " won " << w1_times << " and " << p2 << " won " << w2_times << "." << endl;
}
}
if (counter == 0) {
cout << "There is no history!" << endl;
}
cout << endl;
fin.close();
handle_menu();
}
/* Main */
// This is the main function.
int main () {
welcome_scene();
return 0;
}