-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
453 lines (416 loc) · 13.7 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Field.h"
#include "EdgeBot_v1_0.h"
#include "EdgeBot_v1_1.h"
using namespace std;
char getSide(Cell cell) {
switch (cell) {
case Cell::Cross:
return 'X';
case Cell::Null:
return 'O';
default:
return '_';
}
return 0;
}
size_t getRandomTopMove(std::vector <EvalMove>& evals, double margin) {
size_t count = 1;
double topEval = evals[0].eval;
for (size_t i = 1; i < evals.size(); i++) {
if (std::abs(topEval - evals[i].eval) <= margin) {
count++;
}
else {
break;
}
}
return rand() % count;
}
void playerVsPlayer(Field& field, bool analyze, bool reverts) {
EdgeBot_v1_1 edgeBot;
size_t by, bx, y, x;
bool success;
while (true) {
if (analyze) {
vector <EvalMove> evals = edgeBot.eval(field, 6);
cout << "\nEdgeBot: here's my top " << min(evals.size(), (size_t)9) << " of possible moves:\n";
for (size_t i = 0; i < min(evals.size(), (size_t)9); i++) {
cout << (i + 1) << ". " << evals[i].move << ": " << evals[i].eval << "\n";
}
}
success = false;
if (field.getNextBoard().y == -1) {
cout << field << getSide(field.getTurn()) << " turn on ANY board!\n";
}
else {
cout << field << getSide(field.getTurn()) << " turn on (" << (field.getNextBoard().x) + 1 << ", " << (field.getNextBoard().y) + 1 << ") board!\n";
}
do {
cout << "Input: bx by x y\n\n";
cin >> bx >> by >> x >> y;
cout << "\n";
if (bx == -1 && reverts) {
field.revert();
cout << "\n1 MOVE WAS CANCELLED.\nIt's " << getSide(field.getTurn()) << " to move now!\n" << field;
continue;
}
success = field.isLegalMove(--by, --bx, --y, --x);
} while (!success);
field.move(by, bx, y, x);
switch (field.getWinner()) {
case Cell::Cross:
cout << field << "CROSS won this game. GG!\n";
return;
case Cell::Null:
cout << field << "NULLS won this game. GG!\n";
return;
case Cell::Any:
cout << field << "Round draw. Incredible!\n";
return;
default:
break;
}
}
}
void playerVsEdge(Field& field, size_t depth, double margin, bool pcross, bool analyze, bool reverts) {
EdgeBot_v1_1 edgeBot;
size_t by, bx, y, x;
bool success;
while (true) {
success = false;
if (field.getNextBoard().y == -1) {
cout << field << getSide(field.getTurn()) << " turn on ANY board!\n";
}
else {
cout << field << getSide(field.getTurn()) << " turn on (" << (field.getNextBoard().x) + 1 << ", " << (field.getNextBoard().y) + 1 << ") board!\n";
}
if (pcross) {
do {
cout << "Input: bx by x y\n\n";
cin >> bx >> by >> x >> y;
cout << "\n";
if (bx == -1 && reverts) {
field.revert();
field.revert();
cout << "\n2 MOVES WERE CANCELLED.\n" << field;
continue;
}
success = field.isLegalMove(--by, --bx, --y, --x);
} while (!success);
}
else {
vector <EvalMove> evals = edgeBot.eval(field, depth);
if (analyze) {
cout << "\nEdgeBot: here's my top " << min(evals.size(), (size_t)9) << " of possible moves:\n";
for (size_t i = 0; i < min(evals.size(), (size_t)9); i++) {
cout << (i + 1) << ". " << evals[i].move << ": " << evals[i].eval << "\n";
}
}
size_t m = getRandomTopMove(evals, margin);
cout << "\nEdgeBot: I chose to make move on " << evals[m].move << ".\n\n";
by = evals[m].move.by;
bx = evals[m].move.bx;
y = evals[m].move.y;
x = evals[m].move.x;
}
field.move(by, bx, y, x);
switch (field.getWinner()) {
case Cell::Cross:
cout << field << "CROSS won this game. GG!\n";
return;
case Cell::Null:
cout << field << "NULLS won this game. GG!\n";
return;
case Cell::Any:
cout << field << "Round draw. Incredible!\n";
return;
default:
break;
}
pcross = !pcross;
}
}
void playerVsOldEdge(Field& field, size_t depth, double margin, bool pcross, bool analyze, bool reverts) {
EdgeBot_v1_0 edgeBot;
size_t by, bx, y, x;
bool success;
while (true) {
success = false;
if (field.getNextBoard().y == -1) {
cout << field << getSide(field.getTurn()) << " turn on ANY board!\n";
}
else {
cout << field << getSide(field.getTurn()) << " turn on (" << (field.getNextBoard().x) + 1 << ", " << (field.getNextBoard().y) + 1 << ") board!\n";
}
if (pcross) {
do {
cout << "Input: bx by x y\n\n";
cin >> bx >> by >> x >> y;
cout << "\n";
if (bx == -1 && reverts) {
field.revert();
field.revert();
cout << "\n2 MOVES WERE CANCELLED.\n" << field;
continue;
}
success = field.isLegalMove(--by, --bx, --y, --x);
} while (!success);
}
else {
vector <EvalMove> evals = edgeBot.eval(field, depth);
if (analyze) {
cout << "\nEdgeBot: here's my top " << min(evals.size(), (size_t)9) << " of possible moves:\n";
for (size_t i = 0; i < min(evals.size(), (size_t)9); i++) {
cout << (i + 1) << ". " << evals[i].move << ": " << evals[i].eval << "\n";
}
}
size_t m = getRandomTopMove(evals, margin);
cout << "\nEdgeBot: I chose to make move on " << evals[m].move << ".\n\n";
by = evals[m].move.by;
bx = evals[m].move.bx;
y = evals[m].move.y;
x = evals[m].move.x;
}
field.move(by, bx, y, x);
switch (field.getWinner()) {
case Cell::Cross:
cout << field << "CROSS won this game. GG!\n";
return;
case Cell::Null:
cout << field << "NULLS won this game. GG!\n";
return;
case Cell::Any:
cout << field << "Round draw. Incredible!\n";
return;
default:
break;
}
pcross = !pcross;
}
}
void edgeVsEdge(Field& field, size_t depth1, size_t depth2, double margin1, double margin2, bool firstFirst = true) {
EdgeBot_v1_1 edgeBot;
size_t by, bx, y, x;
while (true) {
if (field.getNextBoard().y == -1) {
cout << field << getSide(field.getTurn()) << " turn on ANY board!\n";
}
else {
cout << field << getSide(field.getTurn()) << " turn on (" << (field.getNextBoard().x) + 1 << ", " << (field.getNextBoard().y) + 1 << ") board!\n";
}
if (firstFirst) {
vector <EvalMove> evals = edgeBot.eval(field, depth1);
cout << "\nEdgeBot A: here's my top " << min(evals.size(), (size_t)9) << " of possible moves:\n";
for (size_t i = 0; i < min(evals.size(), (size_t)9); i++) {
cout << (i + 1) << ". " << evals[i].move << ": " << evals[i].eval << "\n";
}
size_t m = getRandomTopMove(evals, margin1);
cout << "\nEdgeBot A: I chose to make move on " << evals[m].move << ".\n\n";
by = evals[m].move.by;
bx = evals[m].move.bx;
y = evals[m].move.y;
x = evals[m].move.x;
}
else {
vector <EvalMove> evals = edgeBot.eval(field, depth2);
cout << "\nEdgeBot B: here's my top " << min(evals.size(), (size_t)9) << " of possible moves:\n";
for (size_t i = 0; i < min(evals.size(), (size_t)9); i++) {
cout << (i + 1) << ". " << evals[i].move << ": " << evals[i].eval << "\n";
}
size_t m = getRandomTopMove(evals, margin2);
cout << "\nEdgeBot B: I chose to make move on " << evals[m].move << ".\n\n";
by = evals[m].move.by;
bx = evals[m].move.bx;
y = evals[m].move.y;
x = evals[m].move.x;
}
field.move(by, bx, y, x);
switch (field.getWinner()) {
case Cell::Cross:
cout << field << "CROSS won this game. GG!\n";
return;
case Cell::Null:
cout << field << "NULLS won this game. GG!\n";
return;
case Cell::Any:
cout << field << "Round draw. Incredible!\n";
return;
default:
break;
}
firstFirst = !firstFirst;
}
}
void edgeVsOldEdge(Field& field, size_t depth10, size_t depth11, double margin10, double margin11, bool oldFirst) {
EdgeBot_v1_0 edgeBot0;
EdgeBot_v1_1 edgeBot1;
size_t by, bx, y, x;
while (true) {
if (field.getNextBoard().y == -1) {
cout << field << getSide(field.getTurn()) << " turn on ANY board!\n";
}
else {
cout << field << getSide(field.getTurn()) << " turn on (" << (field.getNextBoard().x) + 1 << ", " << (field.getNextBoard().y) + 1 << ") board!\n";
}
if (oldFirst) {
vector <EvalMove> evals = edgeBot0.eval(field, depth10);
cout << "\nEdgeBot v1.0: here's my top " << min(evals.size(), (size_t)9) << " of possible moves:\n";
for (size_t i = 0; i < min(evals.size(), (size_t)9); i++) {
cout << (i + 1) << ". " << evals[i].move << ": " << evals[i].eval << "\n";
}
size_t m = getRandomTopMove(evals, margin10);
cout << "\nEdgeBot v1.0: I chose to make move on " << evals[m].move << ".\n\n";
by = evals[m].move.by;
bx = evals[m].move.bx;
y = evals[m].move.y;
x = evals[m].move.x;
}
else {
vector <EvalMove> evals = edgeBot1.eval(field, depth11);
cout << "\nEdgeBot v1.1: here's my top " << min(evals.size(), (size_t)9) << " of possible moves:\n";
for (size_t i = 0; i < min(evals.size(), (size_t)9); i++) {
cout << (i + 1) << ". " << evals[i].move << ": " << evals[i].eval << "\n";
}
size_t m = getRandomTopMove(evals, margin11);
cout << "\nEdgeBot v1.1: I chose to make move on " << evals[m].move << ".\n\n";
by = evals[m].move.by;
bx = evals[m].move.bx;
y = evals[m].move.y;
x = evals[m].move.x;
}
field.move(by, bx, y, x);
switch (field.getWinner()) {
case Cell::Cross:
cout << field << "CROSS won this game. GG!\n";
return;
case Cell::Null:
cout << field << "NULLS won this game. GG!\n";
return;
case Cell::Any:
cout << field << "Round draw. Incredible!\n";
return;
default:
break;
}
oldFirst = !oldFirst;
}
}
int main(int argc, char* argv[]) {
srand(time(0));
Field field;
size_t edgeBotHalfDepth = 6;
size_t edgeBotHalfDepthEasier = 4;
double margin100 = 0.51;
double margin101 = 0.01;
double margin110 = 0.11;
double margin111 = 1.01;
char input = '0';
char input2 = '0';
do {
cout << "Select a gamemode!\n\n";
cout << "1 - Play vs bot\n";
cout << "2 - Play as two\n";
cout << "3 - Watch bot game\n\n";
cin >> input;
cout << "\n";
} while (input < '1' || input > '3');
switch (input) {
case '1':
do {
cout << "Choose your enemy:\n\n";
cout << "1 - EdgeBot v1.1 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin110 << ")\n";
cout << "2 - EdgeBot v1.1 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin111 << ")\n";
cout << "3 - EdgeBot v1.1 (depth = " << edgeBotHalfDepthEasier / 2 << ", margin = " << margin110 << ")\n";
cout << "4 - EdgeBot v1.1 (depth = " << edgeBotHalfDepthEasier / 2 << ", margin = " << margin111 << ")\n";
cout << "5 - EdgeBot v1.0 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin100 << ")\n\n";
cin >> input;
cout << "\n";
} while (input < '1' || input > '4');
do {
cout << "Choose your side:\n\n";
cout << "1 - CROSS\n";
cout << "2 - NULLS\n\n";
cin >> input2;
cout << "\n";
} while (input2 < '1' || input2 > '2');
switch (input) {
case '1':
playerVsEdge(field, edgeBotHalfDepth, margin110, (input2 == '1'), true, true);
break;
case '2':
playerVsEdge(field, edgeBotHalfDepth, margin111, (input2 == '1'), true, true);
break;
case '3':
playerVsEdge(field, edgeBotHalfDepthEasier, margin110, (input2 == '1'), true, true);
break;
case '4':
playerVsEdge(field, edgeBotHalfDepthEasier, margin111, (input2 == '1'), true, true);
break;
case '5':
playerVsOldEdge(field, edgeBotHalfDepth, margin100, (input2 == '1'), true, true);
break;
default:
break;
}
break;
case '2':
do {
cout << "Choose game type:\n\n";
cout << "1 - Two player game\n";
cout << "2 - Analyze every move with EdgeBot v1.1 (depth = " << edgeBotHalfDepth / 2 << ")\n\n";
cin >> input;
cout << "\n";
} while (input < '1' || input > '2');
switch (input) {
case '1':
playerVsPlayer(field, false, true);
break;
case '2':
playerVsPlayer(field, true, true);
break;
default:
break;
}
break;
case '3':
do {
cout << "Choose a matchup:\n\n";
cout << "1 - EdgeBot v1.1 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin110 << ") vs EdgeBot v1.0 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin100 << ")\n";
cout << "2 - EdgeBot v1.0 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin100 << ") vs EdgeBot v1.1 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin110 << ")\n";
cout << "3 - EdgeBot v1.1 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin110 << ") vs EdgeBot v1.1 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin110 << ")\n";
cout << "4 - EdgeBot v1.1 (depth = " << edgeBotHalfDepthEasier / 2 << ", margin = " << margin110 << ") vs EdgeBot v1.0 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin101 << ")\n";
cout << "5 - EdgeBot v1.0 (depth = " << edgeBotHalfDepth / 2 << ", margin = " << margin101 << ") vs EdgeBot v1.1 (depth = " << edgeBotHalfDepthEasier / 2 << ", margin = " << margin110 << ")\n\n";
cin >> input;
cout << "\n";
} while (input < '1' || input > '5');
switch (input) {
case '1':
edgeVsOldEdge(field, edgeBotHalfDepth, edgeBotHalfDepth, margin100, margin110, false);
break;
case '2':
edgeVsOldEdge(field, edgeBotHalfDepth, edgeBotHalfDepth, margin100, margin110, true);
break;
case '3':
edgeVsEdge(field, edgeBotHalfDepth, edgeBotHalfDepth, margin110, margin110);
break;
case '4':
edgeVsOldEdge(field, edgeBotHalfDepth, edgeBotHalfDepthEasier, margin101, margin110, false);
break;
case '5':
edgeVsOldEdge(field, edgeBotHalfDepth, edgeBotHalfDepthEasier, margin101, margin110, true);
break;
default:
break;
}
break;
default:
break;
}
cout << "Press ENTER to kill this app.\n";
cin.get();
cin.get();
return 0;
}