-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
648 lines (555 loc) · 24.4 KB
/
main.c
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
Copyright (C) 2020- TheTrustedComputer
The driver function of the Make 7 game solver.
It reads command-line arguments, sets flags, inputs move sequences, and solves positions.
This repeats indefinitely until the user terminates the program.
*/
#define _POSIX_C_SOURCE 200809 // clock_gettime()
#include <string.h>
#include <assert.h>
#include <errno.h>
#if defined(_WIN64) || defined(_WIN32)
#include <windows.h>
#endif
#ifdef __unix__
#include <unistd.h>
#endif
#ifdef __linux__
#include <sys/sysinfo.h>
#include <sys/resource.h>
#endif
// We will include source files directly for maximum performance
#include "mt19937ar-cok.h"
//#include "mt19937-64.h"
#include "make7.c"
#include "table.c"
#include "result.c"
#include "negamax.c"
//#include "barrier.c"
#include "mcts.c"
int main(int argc, char **argv)
{
/* Variable declarations */
#if defined(_WIN64) || defined(_WIN32)
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#endif
// The Make 7 game and solution results per tile
static Make7 ms;
static Result r, r1[MAKE7_SIZE], r2[MAKE7_SIZE], r3[MAKE7_SIZE];
// To see if the game state is the same after solving
Make7 oldMS;
// The best move found by Monte Carlo tree search; list of possible moves; total moves
uint8_t best, mctsMove, totalMoves, movesList[MAKE7_SIZE_X3], m;
// Character input; move input from user; interactive mode type
char input, humanInput[3], type, argSeq[MAKE7_AREA_X2 + 1];
// Statistics on solving time, and number of positions evaluated
struct timespec parallelStart, parallelEnd;
clock_t stopwatch;
double sec, npsec;
// Flags for the main loop
bool over, running, notFixedTable, monteCarloTS, interactive, parallel, humanMove, invalidMove, pgo;
// The final calculated table size, used when setting up the transposition table for the first time
size_t finalTTSize;
// Pointer to iterate the results to check for correctness
int res;
// Lower our priority for other processes
#if defined(_WIN64) || defined(_WIN32)
SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
#elifdef __linux__
setpriority(PRIO_PROCESS, getpid(), 19);
#endif
// Read command-line arguments and set flags accordingly
{
int opt;
bool argTableNotDone, argMCTSNotDone, argInteractNotDone, argParallelNotDone, argSwapNotDone, argPGONotDone;
// Default flag values
monteCarloTS = false;
interactive = false;
parallel = false;
pgo = false;
g_swapColors = false;
argTableNotDone = true;
argMCTSNotDone = true;
argInteractNotDone = true;
argParallelNotDone = true;
argSwapNotDone = true;
argPGONotDone = true;
notFixedTable = true;
finalTTSize = 1;
argSeq[0] = '\0';
if (argc >= 2)
{
for (opt = 1; opt < argc; opt++)
{
// Tokenize the arguments: check for switches
if (argv[opt][0] == '-')
{
// Help
if (!(strcmp(argv[opt], "-h") && strcmp(argv[opt], "-?") && strcmp(argv[opt], "--help")))
{
Make7_helpMessage(argv[0]);
return 0;
}
// Others
else
{
// Interactivity
if (argInteractNotDone && !(strcmp(argv[opt], "-i") && strcmp(argv[opt], "--interactive")))
{
interactive = true;
argInteractNotDone = notFixedTable = false;
}
// Monte Carlo tree search
else if (argMCTSNotDone && !(strcmp(argv[opt], "-m") && strcmp(argv[opt], "--mcts")))
{
monteCarloTS = true;
argMCTSNotDone = false;
}
// Parallelization
else if (argParallelNotDone && !(strcmp(argv[opt], "-p") && strcmp(argv[opt], "--parallel")))
{
parallel = true;
argParallelNotDone = false;
}
// Tile color swapping
else if (argSwapNotDone && !(strcmp(argv[opt], "-s") && strcmp(argv[opt], "--swap-colors")))
{
g_swapColors = true;
argSwapNotDone = false;
}
// Transposition table size
else if (argTableNotDone && !(strcmp(argv[opt], "-t") && strcmp(argv[opt], "--table-size")))
{
opt++;
finalTTSize = argv[opt] ? atoi(argv[opt]) : 1;
argTableNotDone = notFixedTable = false;
}
// Profile guided optimization
else if (argPGONotDone && !(strcmp(argv[opt], "-g") && strcmp(argv[opt], "--guided")))
{
pgo = true;
argPGONotDone = false;
}
// Unknown switch
else
{
fprintf(stderr, "Could not understand the switch \"%s\". Please type \"-h\" for help.\n", argv[opt]);
return 1;
}
}
}
// Move sequence
else if ((argv[opt][0] >= '1' && argv[opt][0] <= '3') && (((argv[opt][1] >= 'A') && (argv[opt][1] <= 'G')) || ((argv[opt][1] >= 'a') && (argv[opt][1] <= 'g'))))
{
snprintf(argSeq, sizeof(argSeq), "%s", argv[opt]);
}
}
}
}
if (!monteCarloTS)
{
if (notFixedTable)
{
#ifdef __linux__ // Linux: get host memory size using sysinfo; set transposition table size to the host memory
// Structure for system information
struct sysinfo sysSpecs;
unsigned long gigs, power2;
if (sysinfo(&sysSpecs) != -1) // Request system information
{
// Fetch the host memory size in gigabytes
gigs = sysSpecs.totalram / 1073741824;
// Round up to the nearest power of two that is less than or equal to the system memory size
for (power2 = 1; power2 < gigs; power2 <<= 1);
// Keep asking for memory but use half of that memory if unable to get it reserved
while (power2 && !TransTable_initialize(&table, (finalTTSize = TT_HASHSIZE * power2)))
{
power2 >>= 1;
}
if (!table.entry)
{
fprintf(stderr, "Could not initialize the transposition table size to the installed memory size.\n");
exit(EXIT_FAILURE);
}
}
else // Use a fallback if cannot get system information3
{
TransTable_initialize(&table, (finalTTSize = TT_HASHSIZE));
}
#elif defined(_WIN64) || defined(_WIN32) // Windows: get host memory size using GlobalMemoryStatusEx; this works with virtual machines unlike GetPhysicallyInstalledSystemMemory
MEMORYSTATUSEX memory;
unsigned long long kilos, upower2;
long long gigs, power2;
bool success0, success1;
memory.dwLength = sizeof(memory);
kilos = 0ull;
// Returns in kilobytes, divide by 1048576 to get gigabytes
success0 = GetPhysicallyInstalledSystemMemory(&kilos);
kilos /= 1048576;
// Round up to nearest power of two
for (upower2 = 1; upower2 < kilos; upower2 <<= 1);
gigs = 0l;
// A non-zero return value means it's okay; zero if cannot fetch installed memory size
success1 = GlobalMemoryStatusEx(&memory);
// Divide by 1024^3 to get gigabytes as it is in bytes
gigs = memory.ullTotalPhys / 1073741824;
for (power2 = 1; power2 < gigs; power2 <<= 1);
// If either succeeded, initialize transposition table with half of host memory
if (success0)
{
for (upower2 >>= 1; upower2 && !TransTable_initialize(&table, (finalTTSize = TT_HASHSIZE * upower2)); upower2 >>= 1);
}
else if (success1)
{
for (power2 >>= 1; upower2 && !TransTable_initialize(&table, (finalTTSize = TT_HASHSIZE * power2)); power2 >>= 1);
}
else // When neither of the above worked, use a gigabyte
{
TransTable_initialize(&table, (finalTTSize = TT_HASHSIZE));
}
#else
// Default to one gigabyte on all other platforms
TransTable_initialize(&table, (finalTTSize = TT_HASHSIZE));
#endif
}
else if (!interactive)
{
// Use a fixed size for the transposition table
if (!TransTable_initialize(&table, finalTTSize * (TT_HASHSIZE >> 1)))
{
fprintf(stderr, "Could not allocate memory for the transposition table. Please try a different size.\n");
return 1;
}
}
}
over = false;
running = true;
// For parallelized negamax, each thread will have its own transposition table, so there is no need to allocate a global one
if (parallel)
{
TransTable_destroy(&table);
}
// Seed the Mersenne Twister PRNG
init_genrand(time(nullptr) + clock());
// Prepare alpha-beta move ordering array
Negamax_setColMoveOrder();
// Initialize the game with the starting position
Make7_initialize(&ms);
// Print greetings and details
puts("Make 7 solver by TheTrustedComputer");
if (interactive)
{
type = '0';
humanMove = true;
puts("Running in interactive mode\n");
// Interactive loop
while (running)
{
puts("0: Human vs. Human\n1: Human vs. Computer\n2: Computer vs. Human\n3: Computer vs. Computer");
puts("When playing against the computer, hit Control+C to make it play its turn.");
while ((input = getchar()) != EOF)
{
type = input;
switch (input)
{
case '0':
case '1':
humanMove = true;
goto Label_startGame;
case '2':
case '3':
humanMove = false;
goto Label_startGame;
case '\n':
continue;
default:
fprintf(stderr, "Please select an option from the list.\n");
while (getchar() != '\n');
continue;
}
}
Label_startGame:
while (getchar() != '\n');
while (!over)
{
Make7_print(&ms);
if (Make7_tilesSumTo7(&ms))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("%s wins! Play again?\n", ms.turn ? "Player 1" : "Player 2");
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
puts("Y: Yes\nN: No");
#else
printf("\e[1m%s wins! Play again?\n\e[0mY: Yes\nN: No\n", ms.turn ? "Player 1" : "Player 2");
#endif
over = true;
continue;
}
else if (Make7_noMoreMoves(&ms) || Make7_gridFull(&ms))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
puts("Draw! Play again?");
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
puts("Y: Yes\nN: No");
#else
puts("\e[1mDraw! Play again?\n\e[0mY: Yes\nN: No");
#endif
over = true;
continue;
}
if (humanMove)
{
Make7_generate(&ms, movesList, &totalMoves);
for (m = 0; m < totalMoves; m++)
{
printf("%d%c ", movesList[m] >> 4, (movesList[m] & 0x7) + 'A');
}
puts("");
for (invalidMove = true; invalidMove;)
{
#ifdef _MSC_VER
scanf_s("%2s", humanInput, sizeof(humanInput) / sizeof(humanInput[0]));
#else
scanf("%2s", humanInput);
#endif
if (!humanInput[1] || !Make7_sequence(&ms, humanInput))
{
fprintf(stderr, "Please enter a valid move from the list.\n");
while (getchar() != '\n');
continue;
}
else
{
invalidMove = false;
while (getchar() != '\n');
}
}
}
else
{
#if defined(_WIN64) || defined(_WIN32)
mctsMove = MCTS_search(&ms, &handle, false);
#else
mctsMove = MCTS_search(&ms, nullptr, false);
#endif
Make7_drop(&ms, mctsMove >> 4, mctsMove & 0x7);
puts("");
}
// Switch player depending on the type
if ((type == '1') || (type == '2'))
{
humanMove = !humanMove;
}
}
switch ((input = getchar()))
{
case 'Y':
case 'y':
Make7_initialize(&ms);
over = false;
break;
default:
running = false;
break;
}
}
return 0;
}
else
{
#if (defined(__MINGW32__) || defined(__MINGW64__)) // size_t is an unsigned long long in MinGW
monteCarloTS ? puts("Using Monte Carlo tree search") : printf("Transposition table of %llu entries\n", table.size);
#else
monteCarloTS ? puts("Using Monte Carlo tree search") : printf("Transposition table of %lu entries\n", table.size);
#endif
#ifdef NO_SLIDERS
puts("Not utilizing sliding windows");
#else
puts("Utilizing sliding windows");
#endif
// Solving loop
while (running)
{
over = false;
best = 0;
// Are we running to generate a PGO (profile guided optimization) profile?
if (pgo)
{
puts("Benchmarking Negamax...");
Negamax_search(&ms, &table, 16, -NM_WIN, NM_WIN);
TransTable_destroy(&table);
puts("Benchmarking Monte Carlo...");
Make7_sequence(&ms, "2d2d2c2d3d1e2b1a2b2e1e");
MCTS_search(&ms, nullptr, false);
puts("All benchmarks completed.");
return 0;
}
else
{
// Get move sequence from command line arguments
if (argSeq[0])
{
running = false;
Make7_sequence(&ms, argSeq);
}
else
{
// Get sequence from user input
while ((input = getchar()) != EOF)
{
if (!Make7_getUserInput(&ms, input))
{
if (input == '\n')
{
break;
}
}
}
}
}
// Print game state
Make7_print(&ms);
oldMS = ms;
if (Make7_plyNum(&ms))
{
// Check if the move sequence results in a win, draw, or loss
if (Make7_tilesSumTo7(&ms))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("Game over. %s made 7!\n", ms.turn ? MAKE7_P1_NAME : MAKE7_P2_NAME);
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#else
printf("\e[1mGame over; %s made 7!\e[0m\n", ms.turn ? MAKE7_P1_NAME : MAKE7_P2_NAME);
#endif
over = true;
}
else if (Make7_gridFull(&ms))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
puts("The grid is full. Draw!");
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#else
puts("\e[1mThe grid is full. Draw!\e[0m");
#endif
over = true;
}
else if (Make7_noMoreMoves(&ms))
{
#if defined(_WIN64) || defined(_WIN32)
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("%s has no more number tiles remaining. Draw!\n", ms.turn ? MAKE7_P2_NAME : MAKE7_P1_NAME);
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
#else
printf("\e[1m%s has no more number tiles remaining. Draw!\e[0m\n", ms.turn ? MAKE7_P2_NAME : MAKE7_P1_NAME);
#endif
over = true;
}
}
// Check the game over flag and restart the game
if (over)
{
Make7_initialize(&ms);
continue;
}
if (monteCarloTS)
{
#if defined(_WIN64) || defined(_WIN32)
parallel ? MCTS_rootParallel(&ms, &handle, true) : MCTS_search(&ms, &handle, true);
#else
parallel ? MCTS_rootParallel(&ms, nullptr, true) : MCTS_search(&ms, nullptr, true);
#endif
}
else
{
// Time the search and print the solution, ensuring that the solution is valid for the game
atomic_store(&nodes, 0);
if (parallel)
{
clock_gettime(CLOCK_MONOTONIC, ¶llelStart);
r = Negamax_solve_parallel(&ms, true, r1, r2, r3, nullptr, &best);
clock_gettime(CLOCK_MONOTONIC, ¶llelEnd);
sec = (double)((parallelEnd.tv_sec - parallelStart.tv_sec) + (parallelEnd.tv_nsec - parallelStart.tv_nsec) / 1000000000.0);
}
else
{
stopwatch = clock();
r = Negamax_solve(&ms, &table, true);
stopwatch = clock() - stopwatch;
sec = (double)(stopwatch) / CLOCKS_PER_SEC;
}
npsec = (double)(atomic_load(&nodes)) / (sec ? sec : sec + 1.0);
assert((r.wdl == WIN_CHAR && !(r.dt7 & 1)) || (r.wdl == DRAW_CHAR) || (r.wdl == LOSS_CHAR && (r.dt7 & 1)) || (r.wdl == UNKNOWN_CHAR));
assert((oldMS.player[0] == ms.player[0]) && (oldMS.player[1] == ms.player[1]) && (oldMS.tiles23[0] == ms.tiles23[0]) && (oldMS.tiles23[1] == ms.tiles23[1]));
assert((oldMS.turn == ms.turn) && (oldMS.remaining[0] == ms.remaining[0]) && (oldMS.remaining[1] == ms.remaining[1]) && (oldMS.remaining[2] == ms.remaining[2]));
printf("\a");
#if defined(_WIN64) || defined(_WIN32)
if (r.wdl == DRAW_CHAR)
{
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("%s ", DRAW_TEXT);
SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else
{
Result_print(&r, &r);
}
#else
(r.wdl == DRAW_CHAR) ? printf("\e[1;33m%s\e[0m ", DRAW_TEXT) : Result_print(&r, &r);
#endif
printf("%llu %.0f %.3f\n", atomic_load(&nodes), npsec, sec);
// Do not show the solutions for all the moves if ran with arguments
if (!argSeq[0])
{
if (parallel)
{
printf("1 ");
for (res = 0; res < MAKE7_SIZE; res++)
{
Result_print(&r1[res], &r);
}
printf("\n2 ");
for (res = 0; res < MAKE7_SIZE; res++)
{
Result_print(&r2[res], &r);
}
printf("\n3 ");
for (res = 0; res < MAKE7_SIZE; res++)
{
Result_print(&r3[res], &r);
}
puts("");
}
else
{
Negamax_results(&ms, r1, r2, r3, &r);
best = Result_getBestMove(r1, r2, r3);
}
printf("\aBest: %d%c\n", (best >> 4), (best & 0b1111) + 'A');
for (res = 0; res < MAKE7_SIZE; res++)
{
assert((r1[res].wdl == WIN_CHAR && !(r1[res].dt7 & 1)) || (r1[res].wdl == DRAW_CHAR) || (r1[res].wdl == LOSS_CHAR && (r1[res].dt7 & 1)) || (r1[res].wdl == UNKNOWN_CHAR));
assert((r2[res].wdl == WIN_CHAR && !(r2[res].dt7 & 1)) || (r2[res].wdl == DRAW_CHAR) || (r2[res].wdl == LOSS_CHAR && (r2[res].dt7 & 1)) || (r2[res].wdl == UNKNOWN_CHAR));
assert((r3[res].wdl == WIN_CHAR && !(r3[res].dt7 & 1)) || (r3[res].wdl == DRAW_CHAR) || (r3[res].wdl == LOSS_CHAR && (r3[res].dt7 & 1)) || (r3[res].wdl == UNKNOWN_CHAR));
}
}
// Reset game and transposition table for another search
// Most optimizing compilers will make these following statments take constant time
// Compiling with MSVC, on the other hand, will not, slowing it down linearly
if (!parallel)
{
TransTable_destroy(&table);
if (running)
{
TransTable_initialize(&table, table.size += 2);
}
}
}
Make7_initialize(&ms);
}
}
return 0;
}