-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
571 lines (506 loc) · 19.5 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
//region Headers
using namespace std;
#include "chrono"
#include <cstdlib>
#include <iostream>
#include <queue>
#include <iomanip>
#include <string>
#include "vector"
#include <ctime>
#include<random>
#include<stack>
#include<algorithm>
#include "Cell.h"
#include "Board/Board.cpp"
#include "Players/Player.h"
#include "HexGame.h"
#include "Players/Minimax/MinimaxPlayer.h"
#include "Players/AStarPlayer.h"
#include "Players/HumanPlayer.h"
#include "Players/RandomPlayer.h"
#include "Players/MonteCarloPlayer.h"
//endregion
struct Setup
{
Player *p1{};
Player *p2{};
Board *board{};
int times = 1;
double p1Accuracy = 0;
double p2Accuracy = 0;
};
struct Stats
{
vector<int> wins;
vector<int> turnsTaken;
vector<int> durations;
string p1Name;
string p2Name;
int boardsize{};
double p1Accuracy{};
double p2Accuracy{};
};
Setup Human();
Setup Simulation();
void PrintResults(Stats stats);
int main()
{
RESTART:
system("CLS");
srand(time(nullptr));
//region Pre-Game Input
//SECTION - AI Simulation Check
string yesNo;
Setup setup;
cout << "Do you want to run an AI Simulation? [y/n] (use capitals if you want to see all moves)" << endl;
cin >> yesNo;
if (yesNo == "Y" || yesNo == "y")
{
system("CLS");
setup = Simulation();
}
else if (yesNo == "N" || yesNo == "n")
{
system("CLS");
setup = Human();
}
else
{
system("CLS");
cout << "Invalid input so I'll run a normal game for you." << endl;
setup = Human();
}
bool showAll;
if (yesNo == "Y" || yesNo == "N")
showAll = true;
else
showAll = false;
Stats stats;
stats.p1Name = setup.p1->GetName();
stats.p2Name = setup.p2->GetName();
stats.boardsize = setup.board->GetBoardSize();
stats.p1Accuracy = setup.p1Accuracy;
stats.p2Accuracy = setup.p2Accuracy;
int startingPlayer = 1;
while (setup.times > 0)
{
auto *currentBoard = new Board(setup.board->GetBoardSize());
HexGame game(currentBoard, setup.p1, setup.p2);
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int winner = game.Play(showAll, startingPlayer);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
if (winner == 1 || winner == -1)
{
stats.wins.push_back(winner);
}
else
{
printf("\nFATAL ERROR: No winner was found in main()\n");
return 0;
}
stats.turnsTaken.push_back((currentBoard->GetBoardSize() * currentBoard->GetBoardSize()) - currentBoard->EmptySize());
stats.durations.push_back(static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(end - begin).count()));
if (setup.times <= 1)
{
if (!showAll)
system("CLS");
PrintResults(stats);
string yesNo;
cout << "Do you want to Play again? [Y]es | [N]o | [R]estart program" << endl;
cin >> yesNo;
if (!showAll)
system("CLS");
if (yesNo == "Y" || yesNo == "y")
setup.times++;
else if (yesNo == "N" || yesNo == "n")
cout << "Okay, bye." << endl;
else if (yesNo == "R")
goto RESTART;
else
cout << "Invalid input so I'll take that as a no. Bye." << endl;
}
setup.times--;
startingPlayer *= (-1);
}
delete setup.board;
delete setup.p1;
delete setup.p2;
return 0;
}
Setup Human()
{
//region Pre-Game Input
//SECTION - Get Input for who the player wants to verse (default is of type RandomPlayer)
int p2Type = 1;
cout << "Who do you want to verse? (0 = another player, 1 = Random AI, 2 = Monte-Carlo AI, 3 = Minimax AI)" << endl;
cin >> p2Type;
system("CLS");
//SECTION - Monte-Carlo AI Accuracy Input
double accuracy = 2.5;
if (p2Type == 2 || p2Type == 3)
{
cout << "How accurate do you want the AI to be? (Between 1 and 10)" << endl;
cin >> accuracy;
if (accuracy <= 0)
accuracy = 1;
else if (accuracy > 10)
accuracy = 10;
system("CLS");
}
//SECTION - Get input for the size of the board. When using Minimax, this is restricted to a board of either 3 or 4
int boardSize = 5;
if (p2Type == 3)
{
cout << "Input the size of board: (NOTE: When using Minimax the only boards are 3, 4, or 5)" << endl;
cin >> boardSize;
if (boardSize < 3)
boardSize = 3;
else if (boardSize > 5)
boardSize = 5;
system("CLS");
}
else
{
cout << "Input the size of board: (NOTE: Minimum is 3, Maximum 15, but boards from 5-12 are best)" << endl;
cin >> boardSize;
if (boardSize < 3)
boardSize = 3;
else if (boardSize > 15)
boardSize = 15;
system("CLS");
}
//endregion
//region Game Setup
//SECTION - Setup the players based off of the input from above.
auto *board = new Board(boardSize);
Player *p1 = new HumanPlayer(1, "Crosses (X)");
Player *p2;
switch (p2Type)
{
case 0: //Human Player 2 (not AI)
{
p2 = new HumanPlayer(-1, "Naughts (O)");
break;
}
case 1: //Random Player 2 (simple AI that chooses random empty position)
{
p2 = new RandomPlayer(-1, "Naughts (O)");
break;
}
case 2: //Monte Carlo Player 2 (Medium AI that takes samples of random positions and chooses the best one through heuristic analysis)
{
accuracy *= 1000;
p2 = new MonteCarloPlayer(-1, "Naughts (O)", accuracy);
break;
}
case 3: //Minimax Player 2 (Hard AI that simulates all possible moves from the current state and chooses the one that leads to the fastes win)
{
p2 = new MinimaxPlayer(-1, "Naughts (O)", static_cast<double>(accuracy) * (0.25 * boardSize));
break;
}
case 4: //A* Player 2 (Experimental AI)
{
p2 = new AStarPlayer(-1, "Naughts (O)");
break;
}
default: //Choose Random Player 2 in the case of an error such as invalid input
{
cout << "That was invalid so I'll choose Bad AI for you" << endl;
p2 = new RandomPlayer(-1, "Naughts (O)");
break;
}
}
//endregion
Setup setup;
setup.p1 = p1;
setup.p2 = p2;
setup.board = board;
setup.p2Accuracy = accuracy;
return setup;
}
Setup Simulation()
{
//region Pre-Game Input
//SECTION - Get Input for player 1 (default is of type RandomPlayer)
int p1Type = 1;
cout << "Who do you want as player 1? (1 = Bad AI, 2 = Better AI, 3 = Best AI)" << endl;
cin >> p1Type;
system("CLS");
//SECTION - Get Input for player 2 (default is of type RandomPlayer)
int p2Type = 1;
cout << "Who do you want as player 2? (1 = Bad AI, 2 = Better AI, 3 = Best AI)" << endl;
cin >> p2Type;
system("CLS");
//SECTION - AI1 Accuracy Input
double accuracy1 = 2.5;
if (p1Type == 2 || p1Type == 3)
{
cout << "How accurate do you want the player 1 AI to be? (Between 1 and 10)" << endl;
cin >> accuracy1;
if (accuracy1 <= 0)
accuracy1 = 1;
else if (accuracy1 > 10)
accuracy1 = 10;
system("CLS");
}
//SECTION - AI2 Accuracy Input
double accuracy2 = 2.5;
if (p2Type == 2 || p2Type == 3)
{
cout << "How accurate do you want the player 2 AI to be? (Between 1 and 10)" << endl;
cin >> accuracy2;
if (accuracy2 <= 0)
accuracy2 = 1;
else if (accuracy2 > 10)
accuracy2 = 10;
system("CLS");
}
//SECTION - Get input for the size of the board
int boardSize = 10;
if (p2Type == 3 || p1Type == 3)
{
cout << "Input the size of board: (NOTE: When using Minimax the only boards are 3, 4, or 5)" << endl;
cin >> boardSize;
if (boardSize < 3)
boardSize = 3;
else if (boardSize > 5)
boardSize = 5;
system("CLS");
}
else
{
cout << "How big do you want the board? (NOTE: Minimum is 3, Maximum 15, but boards from 5-12 are best)" << endl;
cin >> boardSize;
if (boardSize < 3)
boardSize = 3;
else if (boardSize > 15)
boardSize = 15;
system("CLS");
}
//SECTION - Get No. of times to run the Simulation
int times = 10;
cout << "How many times did you want to run the Simulation? (Between 1 and 100)" << endl;
cin >> times;
if (boardSize < 1)
boardSize = 1;
else if (boardSize > 100)
boardSize = 100;
system("CLS");
//endregion
//region Game Setup
//SECTION - Setup the players based off of the input from above.
auto *board = new Board(boardSize);
Player *p1;
Player *p2;
switch (p1Type)
{
case 1: //Random Player 1 (simple AI that chooses random empty position)
{
p1 = new RandomPlayer(1, "Crosses (X)");
break;
}
case 2: //Monte Carlo Player 1 (Medium AI that takes samples of random positions and chooses the best one through heuristic analysis)
{
p1 = new MonteCarloPlayer(1, "Crosses (X)", accuracy1 * 1000);
break;
}
case 3: //Minimax Player 1 (Hard AI that simulates all possible moves from the current state and chooses the one that leads to the fastes win)
{
p1 = new MinimaxPlayer(1, "Crosses (X)", static_cast<double>(accuracy1) * (0.25 * boardSize));
break;
}
case 4: //Negascout Player 1 (Experimental variation of Minimax with aimed at being more efficient whilst also being more accurate, hopefully allowing for better Play on big boards
{
p1 = new AStarPlayer(1, "Crosses (X)");
break;
}
default: //Choose Random Player 1 in the case of an error such as invalid input
{
cout << "That was invalid so I'll choose Bad AI for you" << endl;
p1 = new RandomPlayer(1, "Crosses (X)");
break;
}
}
switch (p2Type)
{
case 1: //Random Player 2 (simple AI that chooses random empty position)
{
p2 = new RandomPlayer(-1, "Naughts (O)");
break;
}
case 2: //Monte Carlo Player 2 (Medium AI that takes samples of random positions and chooses the best one through heuristic analysis)
{
p2 = new MonteCarloPlayer(-1, "Naughts (O)", accuracy2 * 1000);
break;
}
case 3: //Minimax Player 2 (Hard AI that simulates all possible moves from the current state and chooses the one that leads to the fastes win)
{
p2 = new MinimaxPlayer(-1, "Naughts (O)", static_cast<double>(accuracy2) * (0.25 * boardSize));
break;
}
case 4: //Negascout Player 2 (Experimental variation of Minimax with aimed at being more efficient whilst also being more accurate, hopefully allowing for better Play on big boards
{
p2 = new AStarPlayer(-1, "Naughts (O)");
break;
}
default: //Choose Random Player 2 in the case of an error such as invalid input
{
cout << "That was invalid so I'll choose Bad AI for you" << endl;
p2 = new RandomPlayer(-1, "Naughts (O)");
break;
}
}
//endregion
Setup setup;
setup.p1 = p1;
setup.p2 = p2;
setup.board = board;
setup.times = times;
setup.p1Accuracy = accuracy1;
setup.p2Accuracy = accuracy2;
return setup;
}
void PrintResults(Stats stats)
{
//region Setup
//HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int colWidth = 20;
//table header
cout << endl << setfill('*') << setw(3 * colWidth) << "*" << endl;
cout << setfill(' ') << fixed;
cout << setw(colWidth) << "" << setw(colWidth);
//SetConsoleTextAttribute(hConsole, 12);//RED
cout << "X Results" << setw(colWidth);
//SetConsoleTextAttribute(hConsole, 9);//BLUE
cout << "O Results" << endl;
//SetConsoleTextAttribute(hConsole, 15);//WHITE
cout << setfill('~') << setw(3 * colWidth) << "~" << endl;
cout << setfill(' ') << fixed;
//endregion
//region Data
//region Player Info
//SECTION - Player Type
cout << setprecision(0) << setw(colWidth) << "PLAYER INFORMATION:" << setprecision(4) << " " << setw(colWidth) << " " << setw(colWidth) << endl;
cout << setprecision(0) << setw(colWidth) << "Player Type" << setprecision(4) << setw(colWidth) << stats.p1Name << setw(colWidth) << stats.p2Name << endl;
//SECTION - Accuracy
if (stats.p1Accuracy == 0 && stats.p2Accuracy == 0)
cout << setprecision(0) << setw(colWidth) << "Preset Accuracy" << setprecision(4) << setw(colWidth) << "N/A" << setw(colWidth) << "N/A" << endl;
else if (stats.p1Accuracy == 0)
cout << setprecision(0) << setw(colWidth) << "Preset Accuracy" << setprecision(4) << setw(colWidth) << "N/A" << setw(colWidth) << stats.p2Accuracy << endl;
else if (stats.p2Accuracy == 0)
cout << setprecision(0) << setw(colWidth) << "Preset Accuracy" << setprecision(4) << setw(colWidth) << stats.p1Accuracy << setw(colWidth) << "N/A" << endl;
else
cout << setprecision(0) << setw(colWidth) << "Preset Accuracy" << setprecision(4) << setw(colWidth) << stats.p1Accuracy << setw(colWidth) << stats.p2Accuracy << endl;
//endregion
//region WIN INFO
cout << setfill('~') << setw(3 * colWidth) << "~" << endl;
cout << setfill(' ') << fixed;
cout << setprecision(0) << setw(colWidth) << "WIN INFORMATION:" << setprecision(4) << " " << setw(colWidth) << " " << setw(colWidth) << endl;
//Loop through all the wins, counting for each side and making a list of the winning indices (for later)
vector<int> xIndices, oIndices;
int xWins = 0, oWins = 0;
for (int i = 0; i < stats.wins.size(); i++)
{
if (stats.wins[i] == 1)
{
xWins++;
xIndices.push_back(i);
}
else
{
oWins++;
oIndices.push_back(i);
}
}
//SECTION - Win-Count
cout << setprecision(0) << setw(colWidth) << "No. of Wins" << setprecision(4) << setw(colWidth) << xWins << setw(colWidth) << oWins << endl;
//SECTION - Win Percent
int xPct = round((xWins / stats.wins.size()) * 100);
int oPct = round((oWins / stats.wins.size()) * 100);
string xPctString = std::to_string(xPct) + "%";
string oPctString = std::to_string(oPct) + "%";
cout << setprecision(0) << setw(colWidth) << "% of Wins" << setprecision(4) << setw(colWidth) << xPctString << setw(colWidth) << oPctString << endl;
//endregion
//region TURN INFO
cout << setfill('~') << setw(3 * colWidth) << "~" << endl;
cout << setfill(' ') << fixed;
cout << setprecision(0) << setw(colWidth) << "TURN INFORMATION:" << setprecision(4) << " " << setw(colWidth) << " " << setw(colWidth) << endl;
int xTurns = 0, oTurns = 0;
vector<int> xTurnsTaken, oTurnsTaken;
//SECTION - Turns for each round
//Loop through the Turns and print the turns for each round
for (int i = 0; i < stats.turnsTaken.size(); i++)
{
if (stats.turnsTaken[i] % 2 == 0) //Check if its an even number
{
xTurns += (stats.turnsTaken[i] / 2);
oTurns += (stats.turnsTaken[i] / 2);
cout << setprecision(0) << setw(colWidth) << "Round " + to_string(i + 1) + " Turns" << setprecision(4) << setw(colWidth) << stats.turnsTaken[i] / 2 << setw(colWidth)
<< stats.turnsTaken[i] / 2 << endl;
xTurnsTaken.push_back(stats.turnsTaken[i] / 2);
oTurnsTaken.push_back(stats.turnsTaken[i] / 2);
}
else //If not, then split the odd accordingly
{
int halfway = round(stats.turnsTaken[i] / 2);
xTurns += stats.turnsTaken[i] - halfway;
oTurns += halfway;
cout << setprecision(0) << setw(colWidth) << "Round " + to_string(i + 1) + " Turns" << setprecision(4) << setw(colWidth) << stats.turnsTaken[i] - halfway << setw(colWidth) << halfway
<< endl;
xTurnsTaken.push_back(stats.turnsTaken[i] - halfway);
oTurnsTaken.push_back(halfway);
}
}
//SECTION - Total Number or Turns
cout << setprecision(0) << setw(colWidth) << "Total Turns" << setprecision(4) << setw(colWidth) << xTurns << setw(colWidth) << oTurns << endl;
//SECTION - Average number of turns per round
int xAvg = round(xTurns / stats.turnsTaken.size());
int oAvg = round(oTurns / stats.turnsTaken.size());
cout << setprecision(0) << setw(colWidth) << "Average Turns" << setprecision(4) << setw(colWidth) << xAvg << setw(colWidth) << oAvg << endl;
//SECTION - Avg Turns Per Win
int xWinTurns = 0;
for (int i:xIndices)
{
xWinTurns += xTurnsTaken[i];
}
if (xWins != 0)
xWinTurns = xWinTurns / xWins;
int oWinTurns = 0;
for (int i:oIndices)
{
oWinTurns += oTurnsTaken[i];
}
if (oWins != 0)
oWinTurns = oWinTurns / oWins;
cout << setprecision(0) << setw(colWidth) << "Avg Turns Per Win" << setprecision(4) << setw(colWidth) << xWinTurns << setw(colWidth) << oWinTurns << endl;
//endregion
//SECTION - BoardO Info
cout << setfill('~') << setw(3 * colWidth) << "~" << endl;
cout << setfill(' ') << fixed;
xPct = round((static_cast<double>(xAvg) / (stats.boardsize * stats.boardsize)) * 100);
oPct = round((static_cast<double>(oAvg) / (stats.boardsize * stats.boardsize)) * 100);
xPctString = std::to_string(xPct) + "%";
oPctString = std::to_string(oPct) + "%";
cout << setprecision(0) << setw(colWidth) << "BOARD INFORMATION:" << setprecision(4) << " " << setw(colWidth) << " " << setw(colWidth) << endl;
cout << setprecision(0) << setw(colWidth) << "BoardO Size" << setprecision(4) << setw(colWidth) << stats.boardsize << setw(colWidth) << stats.boardsize << endl;
cout << setprecision(0) << setw(colWidth) << "Total No. of Cells" << setprecision(4) << setw(colWidth) << stats.boardsize * stats.boardsize << setw(colWidth) << stats.boardsize * stats.boardsize
<< endl;
cout << setprecision(0) << setw(colWidth) << "Avg % of BoardO Used" << setprecision(4) << setw(colWidth) << xPctString << setw(colWidth) << oPctString << endl;
//SECTION - Round Duration
cout << setfill('~') << setw(3 * colWidth) << "~" << endl;
cout << setfill(' ') << fixed;
cout << setprecision(0) << setw(colWidth) << "TIME INFORMATION:" << setprecision(4) << " " << setw(colWidth) << " " << setw(colWidth) << endl;
int totalDur = 0.0;
for (int i = 0; i < stats.durations.size(); i++)
{
totalDur += stats.durations[i];
cout << setprecision(0) << setw(colWidth) << "Round " + to_string(i + 1) + " Duration" << setprecision(4) << setw(colWidth) << "(Seconds)" << setw(colWidth) << stats.durations[i]
<< endl;
}
totalDur = round(totalDur / stats.durations.size());
cout << setprecision(0) << setw(colWidth) << "Avg Round Duration" << setprecision(4) << setw(colWidth) << "(Seconds)" << setw(colWidth) << totalDur << endl;
//endregion
cout << setfill('*') << setw(3 * colWidth) << "*" << endl;
cout << setfill(' ') << setw(3 * colWidth) << " " << endl;
}