-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
304 lines (213 loc) · 8.14 KB
/
game.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
#include <iostream>
#include <vector>
using namespace std;
class Game {
public:
vector<vector<char>> table = {{' ', ' ', ' '},{' ', ' ', ' '},{' ', ' ', ' '}};
char turn = 'x';
bool change(int x, int y) {
if (x < 1 || x > 3 || y < 1 || y > 3 || table[y - 1][x - 1] != ' ') return false;
table[y - 1][x - 1] = turn;
return true;
}
void changeTurn() {
switch (turn) {
case 'x':
turn = 'o';
break;
case 'o':
turn = 'x';
break;
}
}
bool checkFinish() {
if (checkWin() || checkDraw()) return true;
return false;
}
bool checkWin() {
for (int x = 0; x < 3; x++)
if ((table[x][0] != ' ' && table[x][0] == table[x][1] && table[x][1] == table[x][2]) || (table[0][x] != ' ' && table[0][x] == table[1][x] && table[1][x] == table[2][x])) return true;
if (table[1][1] != ' ' && ((table[0][0] == table[1][1] && table[1][1] == table[2][2]) || (table[0][2] == table[1][1] && table[1][1] == table[2][0]))) return true;
return false;
}
bool checkDraw() {
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
if (table[y][x] == ' ') return false;
return true;
}
};
class InputOutput {
public:
string initPrint() {
cout << "Benvenuto su TRIS!\n\nInserendo 1) Player vs Computer\nInserendo 2) Player1 vs Player2" << endl;
string mod;
int attempts = 0;
do {
if (attempts > 0) cout << "Codice modalità non valido, scegline un altro..." << endl;
attempts++;
cout << "\nInserisci la modalità: "; cin >> mod;
} while (mod != "1" && mod != "2");
return mod;
}
void printTable(vector<vector<char>> table) {
cout << "\n";
cout << " " << table[0][0] << " | " << table[0][1] << " | " << table[0][2] << " " << endl;
cout << "---|---|---" << endl;
cout << " " << table[1][0] << " | " << table[1][1] << " | " << table[1][2] << " " << endl;
cout << "---|---|---" << endl;
cout << " " << table[2][0] << " | " << table[2][1] << " | " << table[2][2] << " " << endl;
}
void makeMove(Game& game) {
string x, y;
int attempts = 0;
do {
if (attempts > 0) cout << "Riga o colonna non selezionabile, scegline un altra..." << endl;
attempts++;
cout << "\nScegli la colonna: "; cin >> x;
cout << "Scegli la riga: "; cin >> y;
} while ((!isdigit(x[0]) || !isdigit(y[0])) || !game.change(stoi(x), stoi(y)));
}
void playerNum(char turn) {
if (turn == 'x') cout << "Turno Player 2 (x)" << endl;
else cout << "Turno Player 1 (o)" << endl;
}
void clear() {
cout << "\033c";
}
bool askToPlayAgain() {
string res;
cout << "Vuoi giocare ancora? (si/no) "; cin >> res;
if (res == "si") return true;
else return false;
}
void awardsVictory(char turn) {
switch (turn) {
case 'x':
cout << "\nVince x (bot)!\n" << endl;
break;
case 'o':
cout << "\nVince o (player)!\n" << endl;
break;
case 'd':
cout << "\nPatta!\n" << endl;
break;
}
}
int lastPrint() {
clear();
cout << "Alla prossima!" << endl;
return 0;
}
};
class Bot {
public:
void makeMove(Game& game) {
vector<int> move = minimaxRoot(game.table);
game.change(++move[1], ++move[0]);
}
private:
vector<int> minimaxRoot(vector<vector<char>> table, bool isMaximisingPlayer = true) {
int depth = 0;
vector<int> bestMove;
int bestValue = -9999;
const vector<vector<int>> freeSpaces = findFreeSpaces(table);
for (int i = 0; i < freeSpaces.size(); i++) {
const vector<vector<char>> tableBackup = table;
table[freeSpaces[i][0]][freeSpaces[i][1]] = 'x';
const int attualValue = minimax(table, depth + 1, -10000, 10000, !isMaximisingPlayer);
table = tableBackup;
if (attualValue >= bestValue) {
bestValue = attualValue;
bestMove = freeSpaces[i];
}
}
return bestMove;
}
int minimax(vector<vector<char>> table, int depth, int alpha, int beta, bool isMaximisingPlayer){
if (checkDraw(table)) {
return (0 - depth);
} else if (checkWin(table)) {
if (!isMaximisingPlayer) return (100 - depth);
else return (-100 - depth);
}
const vector<vector<int>> freeSpaces = findFreeSpaces(table);
if (isMaximisingPlayer) {
int bestValue = -9999;
for (int i = 0; i < freeSpaces.size(); i++) {
const vector<vector<char>> tableBackup = table;
table[freeSpaces[i][0]][freeSpaces[i][1]] = 'x';
bestValue = max(bestValue, minimax(table, depth + 1, alpha, beta, !isMaximisingPlayer));
table = tableBackup;
alpha = max(alpha, bestValue);
if (beta <= alpha) {
return bestValue;
}
}
return bestValue;
} else {
int bestValue = 9999;
for (int i = 0; i < freeSpaces.size(); i++) {
vector<vector<char>> tableBackup = table;
table[freeSpaces[i][0]][freeSpaces[i][1]] = 'o';
bestValue = min(bestValue, minimax(table, depth + 1, alpha, beta, !isMaximisingPlayer));
table = tableBackup;
beta = min(beta, bestValue);
if (beta <= alpha) {
return bestValue;
}
}
return bestValue;
}
}
vector<vector<int>> findFreeSpaces(vector<vector<char>> table) {
vector<vector<int>> freeSpaces;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
if (table[y][x] == ' ')
freeSpaces.push_back({y, x});
return freeSpaces;
}
bool checkWin(vector<vector<char>> table) {
for (int x = 0; x < 3; x++)
if ((table[x][0] != ' ' && table[x][0] == table[x][1] && table[x][1] == table[x][2]) || (table[0][x] != ' ' && table[0][x] == table[1][x] && table[1][x] == table[2][x])) return true;
if (table[1][1] != ' ' && ((table[0][0] == table[1][1] && table[1][1] == table[2][2]) || (table[0][2] == table[1][1] && table[1][1] == table[2][0]))) return true;
return false;
}
bool checkDraw(vector<vector<char>> table) {
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
if (table[y][x] == ' ') return false;
return true;
}
};
int main() {
InputOutput inOut;
Bot bot;
do {
Game game;
inOut.clear();
string mod = inOut.initPrint();
do {
//player1 turn
inOut.clear();
game.changeTurn();
if (mod == "2") inOut.playerNum(game.turn);
inOut.printTable(game.table);
inOut.makeMove(game);
if (game.checkFinish()) break;
//player2 or bot turn
inOut.clear();
game.changeTurn();
if (mod == "2") inOut.playerNum(game.turn);
inOut.printTable(game.table);
if (mod == "1") bot.makeMove(game);
else inOut.makeMove(game);
} while (!game.checkFinish());
inOut.clear();
inOut.printTable(game.table);
if (game.checkDraw()) inOut.awardsVictory('d');
else inOut.awardsVictory(game.turn);
} while (inOut.askToPlayAgain());
return inOut.lastPrint();
}