-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
378 lines (321 loc) · 7.2 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
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <random>
#include <time.h>
#include <io.h>
#include <map>
#include "defs.hpp"
#include "gamestate.hpp"
#include "engine.hpp"
#include "perft.hpp"
using namespace std;
enum COMMANDS
{
U_unrecognized, U_help, U_newgame, U_stop,
U_perft, U_quit, U_simulate, U_search,
U_display, U_board, U_game
};
static map<string, COMMANDS> MAP_COMMANDS =
{
{"help", U_help}, {"newgame", U_newgame}, {"stop", U_stop}, {"perft", U_perft},
{"quit", U_quit}, {"simulate", U_simulate}, {"search", U_search},
{"d", U_display}, {"board", U_board}, {"game", U_game}
};
enum ENGINES
{
HUMAN_PLAYER = 0, RANDOM_PLAYER = 1, MIN_MAX_PLAYER = 2, ALPHA_BETA_PLAYER = 3, SIMPLE_THREADED_ALPHA_BETA_PLAYER = 4,
HEURISTIC_ALPHA_BETA_PLAYER = 5, BETA_ALPHA_PLAYER = 6,
};
static map<int, int (*)(GAMESTATE* gs, int)> MAP_ENGINES = {
{HUMAN_PLAYER, human_player}, {RANDOM_PLAYER, random_player},
{MIN_MAX_PLAYER, min_max_player}, {ALPHA_BETA_PLAYER, alpha_beta_player},
{SIMPLE_THREADED_ALPHA_BETA_PLAYER, simple_threaded_alpha_beta_player},
{HEURISTIC_ALPHA_BETA_PLAYER, heuristic_alpha_beta_player},
{BETA_ALPHA_PLAYER, beta_alpha_player}
};
vector<string> split_command(const string& command)
{
stringstream stream(command);
string intermediate;
vector<string> tokens;
while (getline(stream, intermediate, ' '))
{
tokens.push_back(intermediate);
}
return tokens;
}
void print_help() {
int current_hole_index;
cout << "+---+--+--+--+--+--+--+---+" << endl;
// Print player B side
cout << "| ";
current_hole_index = PLAYER_TO_STORE_INDEX[PLAYER_B] - 1;
for (; current_hole_index > PLAYER_TO_STORE_INDEX[PLAYER_A]; --current_hole_index) {
cout << "|";
cout << HOLE_INDEX_TO_STRING[current_hole_index];
}
cout << "| |" << endl;
// Print Stores
cout << "|";
cout << HOLE_INDEX_TO_STRING[PLAYER_TO_STORE_INDEX[PLAYER_B]];
cout << " +--+--+--+--+--+--+ ";
cout << HOLE_INDEX_TO_STRING[PLAYER_TO_STORE_INDEX[PLAYER_A]];
cout << "|" << endl;
// Print player A side
cout << "| ";
current_hole_index = 0;
for (; current_hole_index < PLAYER_TO_STORE_INDEX[PLAYER_A]; ++current_hole_index) {
cout << "|";
cout << HOLE_INDEX_TO_STRING[current_hole_index];
}
cout << "| |" << endl;
cout << "+---+--+--+--+--+--+--+---+\n" << endl;
return;
}
int parse_board(GAMESTATE* gs, vector<string> tokens) {
GAMESTATE gamestate;
static const int expected_board_size = (NUMBER_OF_TOTAL_HOLES);
if (tokens.size() != (2 + expected_board_size)) {
return true;
}
int current_number;
for (int current_index = 0; current_index < expected_board_size; ++current_index) {
gamestate.board[current_index] = stoi(tokens.at(current_index + 1));
}
const int current_player_index = stoi(tokens.back());
if ((current_player_index != PLAYER_A) && (current_player_index != PLAYER_B)) {
return true;
}
gamestate.current_player = current_player_index;
update_game_over(&gamestate);
memcpy(gs, &gamestate, sizeof(gamestate));
// ex: 4 4 4 4 4 4 0 4 4 4 4 4 4 0 0
return false;
}
int parse_simulation(vector<string> tokens) {
int (*player_a)(GAMESTATE * gs, int);
int (*player_b)(GAMESTATE * gs, int);
if (MAP_ENGINES.find(stoi(tokens.at(1))) == MAP_ENGINES.end()) {
return 1;
}
else {
player_a = MAP_ENGINES[stoi(tokens.at(1))];
}
if (MAP_ENGINES.find(stoi(tokens.at(2))) == MAP_ENGINES.end()) {
return 1;
}
else {
player_b = MAP_ENGINES[stoi(tokens.at(2))];
}
int trials = stoi(tokens.at(3));
int opt_a = 0;
int opt_b = 0;
if (tokens.size() > 4) {
opt_a = stoi(tokens.at(4));
if (tokens.size() == 6) {
opt_b = stoi(tokens.at(5));
}
}
try {
simulate_games(player_a, player_b, trials, opt_a, opt_b);
}
catch (...) {
cout << "Simulation failed!\n";
return 1;
}
// simulate 3 3 100 16 16
return false;
}
int parse_game(GAMESTATE* gamestate, vector<string> tokens) {
int (*player_a)(GAMESTATE * gs, int);
int (*player_b)(GAMESTATE * gs, int);
if (MAP_ENGINES.find(stoi(tokens.at(1))) == MAP_ENGINES.end()) {
return 1;
}
else {
player_a = MAP_ENGINES[stoi(tokens.at(1))];
}
if (MAP_ENGINES.find(stoi(tokens.at(2))) == MAP_ENGINES.end()) {
return 1;
}
else {
player_b = MAP_ENGINES[stoi(tokens.at(2))];
}
int print_output = stoi(tokens.at(3));
int opt_a = 0;
int opt_b = 0;
if (tokens.size() > 4) {
opt_a = stoi(tokens.at(4));
if (tokens.size() == 6) {
opt_b = stoi(tokens.at(5));
}
}
try {
game_loop(gamestate, player_a, player_b, print_output, opt_a, opt_b);
}
catch (...) {
cout << "Game could not start!\n";
return 1;
}
// game 3 3 100 16 16
return false;
}
int parse_search(GAMESTATE* gamestate, vector<string> tokens) {
int (*engine)(GAMESTATE * gs, int);
if (MAP_ENGINES.find(stoi(tokens.at(1))) == MAP_ENGINES.end()) {
return 1;
}
else {
engine = MAP_ENGINES[stoi(tokens.at(1))];
}
if (engine == human_player)
{
return 1;
}
int opt = 1;
if (tokens.size() > 2) {
opt = stoi(tokens.at(2));
}
int hole_selected = -1;
try {
hole_selected = engine(gamestate, opt);
cout << hole_selected << '\n';
}
catch (...) {
cout << "Engine could not be started!\n";
return 1;
}
// search 3 16
return false;
}
void console_loop() {
GAMESTATE gamestate;
bool game_started = false;
while (1) {
string input;
// get user / GUI input
if (!getline(cin, input))
{
// continue the loop
break;
}
// make sure input is available
if (!input.length())
{
// continue the loop
continue;
}
vector<string> tokens = split_command(input);
switch (MAP_COMMANDS[tokens[0]])
{
case (U_stop):
case (U_quit):
{
goto exit_loop;
break;
}
case (U_newgame):
{
start_game(&gamestate);
game_started = true;
break;
}
case (U_perft):
{
const int p_depth = stoi(tokens[1]);
if (!game_started) // call parse position function
{
start_game(&gamestate);
game_started = true;
}
perft_test(&gamestate, p_depth);
break;
}
case (U_display):
{
print_board(&gamestate);
break;
}
case (U_help):
{
print_help();
break;
}
case (U_unrecognized):
{
printf("Unknown command\n");
break;
}
case (U_simulate):
{
try {
if (parse_simulation(tokens)) {
cout << "Invalid simulation commands.\n";
}
}
catch (...) {
cout << "Could not parse input.\n";
}
break;
}
case (U_game):
{
try {
if (!game_started) {
start_game(&gamestate);
game_started = true;
}
if (parse_game(&gamestate, tokens)) {
cout << "Invalid game commands.\n";
}
}
catch (...) {
cout << "Could not parse input.\n";
}
break;
}
case (U_search):
{
try {
if (!game_started) {
start_game(&gamestate);
game_started = true;
}
if (parse_search(&gamestate, tokens)) {
cout << "Invalid search commands.\n";
}
}
catch (...) {
cout << "Could not parse input.\n";
}
break;
}
case (U_board):
{
try {
if (parse_board(&gamestate, tokens)) {
cout << "Board could not be parsed!\n";
}
else
{
game_started = true;
}
}
catch (...) {
cout << "Invalid input!\n";
}
break;
}
}
}
exit_loop:
return;
}
int main() {
time_seed = time(NULL);
srand(time_seed);
console_loop();
return 0;
}