-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_4_game.cpp
381 lines (263 loc) · 10.5 KB
/
connect_4_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
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
#include<bits/stdc++.h>
#include <chrono>
#include <windows.h>
using namespace std;
// by jvpcms
class connect_4 {
private:
int n, m, w;
char** board;
char piece_1 = 'X';
char piece_2 = 'O';
char space = char(250);
char divider_horiz = ' ';
char player = piece_1;
int made_moves = 0;
bool strategic_opponent = false;
int MIN_INF = -2, MAX_INF = 2;
const int MAX_DEPTH = 10;
vector<int> check_order;
public:
connect_4() {
cout << "Enter the number of lines: "; cin >> this->n;
cout << "Enter the number of columns: "; cin >> this->m;
cout << "Enter the number of pieces in a row to win: "; cin >> this->w;
this->board = new char*[n];
for (int i = 0; i < n; i++) this->board[i] = new char[m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
this->board[i][j] = this->space;
}
}
int p = this->m / 2;
for (int i = 1; i <= this->m; i++) {
check_order.push_back(p);
p += (i % 2 == 0) ? i : -i;
}
}
connect_4(int n, int m, int w=4) {
this->n = n;
this->m = m;
this->w = w;
this->board = new char*[n];
for (int i = 0; i < n; i++) this->board[i] = new char[m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
this->board[i][j] = this->space;
}
}
int p = this->m / 2;
for (int i = 1; i <= this->m; i++) {
check_order.push_back(p);
p += (i % 2 == 0) ? i : -i;
}
}
void display_board() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < this->n; i++) {
for (int j = 0; j < this->m; j++) {
if (this->board[i][j] == this->piece_1) SetConsoleTextAttribute(hConsole, 12);
else if (this->board[i][j] == this->piece_2) SetConsoleTextAttribute(hConsole, 9);
cout << this->board[i][j] << this->divider_horiz;
SetConsoleTextAttribute(hConsole, 15);
}
cout << '\n';
}
}
void reset_board() {
for (int i = 0; i < this->n; i++) {
for (int j = 0; j < this->m; j++) {
this->board[i][j] = this->space;
}
}
this->made_moves = 0;
}
void next_player() {
this->player = (this->player == this->piece_1) ? this->piece_2 : this->piece_1;
}
bool is_valid_move(int col) {
return this->board[0][col] == this->space and col >= 0 and col < this->m;
}
void place_piece(int col) {
// col need to be validated first
for (int line = this->n - 1; line >= 0; line--) {
if (this->board[line][col] == this->space) {
this->board[line][col] = this->player;
this->made_moves++;
break;
}
}
}
void remove_piece(int col) {
for (int line = 0; line < this->n; line++) {
if (this->board[line][col] != this->space) {
this->board[line][col] = this->space;
this->made_moves--;
break;
}
}
}
bool check_win() {
bool win = false;
for (int line = 0; line < this->n; line++) {
for (int col = 0; col < this->m; col++) {
char current = this->board[line][col];
if (current == this->space) continue;
// check right
for (int i = 1; i < this->w; i++) {
if (col + i >= this->m or this->board[line][col + i] != current) break;
if (i == this->w - 1) win = true;
}
// check down
for (int i = 1; i < this->w; i++) {
if (line + i >= this->n or this->board[line + i][col] != current) break;
if (i == this->w - 1) win = true;
}
// check diagonal (right down)
for (int i = 1; i < this->w; i++) {
if (line + i >= this->n or col + i >= this->m or this->board[line + i][col + i] != current) break;
if (i == this->w - 1) win = true;
}
// check diagonal (right up)
for (int i = 1; i < this->w; i++) {
if (line - i < 0 or col + i >= this->m or this->board[line - i][col + i] != current) break;
if (i == this->w - 1) win = true;
}
}
}
return win;
}
bool board_is_full() {
return this->made_moves >= this->n * this->m;
}
void player_move() {
bool valid_move = false;
while (!valid_move) {
cout << '\n' << "Player " << this->player << " choose a column: ";
int col; cin >> col; col--;
if (is_valid_move(col)) {
valid_move = true;
place_piece(col);
}
if (!valid_move) {
cout << "Invalid move, try again." << '\n';
}
cout << '\n';
}
}
void bot_random_move() {
int col = rand() % this->m;
while (!is_valid_move(col)) col = rand() % this->m;
place_piece(col);
}
int minimax(char **board, int depth, int alpha, int beta, bool isMaximizing) {
if (check_win()) return (isMaximizing) ? -1 : 1;
if (board_is_full() or depth == MAX_DEPTH) return 0;
if (isMaximizing) {
int bestScore = MIN_INF;
for (int col : this->check_order) {
if (!is_valid_move(col)) continue;
place_piece(col);
next_player();
int score = minimax(this->board, depth + 1, alpha, beta, false);
remove_piece(col);
next_player();
bestScore = max(bestScore, score);
alpha = max(alpha, score);
if (beta <= alpha) break;
}
return bestScore;
}
else {
int bestScore = MAX_INF;
for (int col : this->check_order) {
if (!is_valid_move(col)) continue;
place_piece(col);
next_player();
int score = minimax(this->board, depth + 1, alpha, beta, true);
remove_piece(col);
next_player();
bestScore = min(bestScore, score);
beta = min(beta, score);
if (beta <= alpha) break;
}
return bestScore;
}
}
void bot_strategic_move() {
int bestScore = MIN_INF, bestCol = 0;
for (int col : this->check_order) {
if (!is_valid_move(col)) continue;
place_piece(col);
next_player();
int score = minimax(this->board, 0, MIN_INF, MAX_INF, false);
remove_piece(col);
next_player();
if (score > bestScore) {
bestScore = score;
bestCol = col;
}
}
place_piece(bestCol);
}
void play() {
cout << "Who do you want to play against? (s - Strategic / r - Random): ";
char choice; cin >> choice; cout << '\n';
if (choice == 's') this->strategic_opponent = true;
reset_board();
while (!board_is_full()) {
(this->strategic_opponent) ? bot_strategic_move() : bot_random_move();
display_board();
if (check_win()) {
cout << '\n' << "Player " << this->player << " wins!";
return;
}
next_player();
if (board_is_full()) break;
player_move();
if (check_win()) {
display_board();
cout << '\n' << "Player " << this->player << " wins!";
return;
}
next_player();
}
cout << '\n' << "It's a draw!";
return;
}
int play_bot_vs_bot(int tests=1) {
// Expected:
// strategic vs random: mostly wins, some draws
// random vs random: wins and defeats close to 50%, some draws
// strategic vs strategic: mostly draws
// random vs strategic: mostly defeats, some draws, almost no victories
int victory = 0, defeat = 0, draw = 0;
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < tests; i++) {
reset_board();
while (!board_is_full()) {
bot_strategic_move();
if (check_win()) {victory++; break;}
next_player();
if (board_is_full()) break;
bot_random_move();
if (check_win()) {defeat++; break;}
next_player();
}
if (board_is_full()) draw++;
}
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << '\n' << "Victories: " << victory;
cout << '\n' << "Defeats: " << defeat;
cout << '\n' << "Draws: " << draw;
cout << '\n' << "Time: " << duration.count() << " ms";
return 0;
}
};
int main() {
srand(time(NULL));
connect_4 game(6, 7);
game.play();
return 0;
}