-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
210 lines (207 loc) · 7.51 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
#include <iostream>
#include "tictactoe.h"
#include "easylevel.h"
#include "hardlevel.h"
#include "getch.h"
using namespace std;
int two_players(string player1, string player2, int first_move)
{
tictactoe new_game;
for (int i = first_move;i < 9 + first_move;i++)
if (i & 1) {
new_game.print_grid();
cout << "Move of " << player2 << " (\033[1m\033[31mX\033[0m): ";
if (i == 8 + first_move) {
cout << "\n";
new_game.do_last_move('X');
sleepcp(250);
}
else {
char c = new_game.input('X');
if (c == 'b' || c == 'B')
return -1;
}
if (new_game.if_won()) {
new_game.print_grid();
cout << player2 << " wins \U0001F389\n";
return 1;
}
}
else {
new_game.print_grid();
cout << "Move of " << player1 << " (\033[1m\033[34mO\033[0m): ";
if (i == 8 + first_move) {
cout << "\n";
new_game.do_last_move('O');
sleepcp(250);
}
else {
char c = new_game.input('O');
if (c == 'b' || c == 'B')
return -1;
}
if (new_game.if_won()) {
new_game.print_grid();
cout << player1 << " wins \U0001F389\n";
return 0;
}
}
new_game.print_grid();
cout << "Match tied.\n";
return (1 - first_move);
}
template <class T>
int one_player(bool first_move)
{
T new_game;
for (int i = first_move;;i++) {
if (i & 1) {
new_game.print_grid();
// cout << i << "\n";
cout << "Computer's move (\033[1m\033[31mX\033[0m):\n";
new_game.make_move();
sleepcp(250);
if (new_game.if_won()) {
new_game.print_grid();
// cout << i << "\n";
cout << "You lost \U00002639\n\n";
while (1) {
cout << "\033[1APlay again (Y/n): ";
char again = getch();
if (again == 'y' || again == 'Y')
return 1;
else if ((again == 'z' || again == 'Z') && new_game.st_done.size() > 1) {// if undoing is possible
new_game.undo();
new_game.undo();
i -= 2;
break;
}
else if (again == 'n' || again == 'N' || again == 'b' || again == 'B')
return -1;
}
}
}
else {
new_game.print_grid();
// cout << i << "\n";
cout << "Your move" << " (\033[1m\033[34mO\033[0m): ";
if (i == 8 + first_move) {
cout << "\n";
new_game.do_last_move('O');
sleepcp(250);
}
else {
char c = new_game.input('O');
if (c == 'b' || c == 'B')
return -1;
else if (c == 'z' || c == 'Z') {// if undoing is possible
if (new_game.st_done.size() > 1) {
new_game.undo();
new_game.undo();
i -= 3;
}
else
i -= 1;
}
else if (c == 'r' || c == 'r') {// if redoing is possible
if (new_game.st_undone.size() > 1) {
new_game.redo();
new_game.redo();
i += 1;
}
else
i -= 1;
}
}
if (new_game.if_won()) {
new_game.print_grid();
// cout << i << "\n";
cout << "You won \U0001F389\n\n";
do {
cout << "\033[1APlay again (Y/n): ";
char again = getch();
if (again == 'y' || again == 'Y')
return 0;
else if (again == 'n' || again == 'N' || again == 'b' || again == 'B')
return -1;
} while (1);
}
}
if (i == 8 + first_move) {
new_game.print_grid();
// cout << i << "\n";
cout << "Match tied.\n\n";
while (1) {
cout << "\033[1APlay again (Y/n): ";
char again = getch();
if (again == 'y' || again == 'Y')
return 1 - first_move;
else if (again == 'z' || again == 'Z') {// if undoing is possible
if (first_move == 0 && new_game.st_done.size() > 2) {
new_game.undo();
//new_game.st_undone.clear();
new_game.undo();
new_game.undo();
i -= 3;
break;
}
else if (first_move == 1 && new_game.st_done.size() > 1) {// if undoing is possible
new_game.undo();
new_game.undo();
i -= 2;
break;
}
}
else if (again == 'n' || again == 'N' || again == 'b' || again == 'B')
return -1;
}
}
}
}
int main()
{
char option;
do {
system("clear");
cout << "1 ~ One Player\n2 ~ Two Players\n\n\n\n\n\n\n\nz ~ undo\nr ~ redo\nb ~ back\nq ~ quit\033[10A\033[8DType your choice: ";
option = getch();
if (option == '1') {
char option2;
do {
system("clear");
cout << "1 ~ Easy level\n2 ~ Hard level\n\n\n\n\n\n\n\nz ~ undo\nr ~ redo\nb ~ back\nq ~ quit\033[10A\033[8DType your choice: ";
option2 = getch();
if (option2 == '1') {
int first_move = 0;
while (first_move == 0 || first_move == 1) {
first_move = one_player<easylevel> (first_move);
}
}
else if (option2 == '2') {
int first_move = 0;
while (first_move == 0 || first_move == 1) {
first_move = one_player<hardlevel> (first_move);
}
}
} while (option2 != 'b' && option2 != 'B');
}
else if (option == '2') {
system("clear");
string player1, player2;
cout << "Enter Player 1 name: ";
getline(cin, player1);
cout << "Enter Player 2 name: ";
getline(cin, player2);
int first_move = 0;
char again = 'y';
while (again == 'y' || again == 'Y') {
first_move = two_players(player1, player2, first_move);
if (first_move == -1)
break;
cout << "Play again (Y/n): ";
again = getch();
}
}
} while (1); // option != 'b' && option != 'B' // make back a exit prompt in the main menu
return 0;
}