-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
123 lines (107 loc) · 2.61 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
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "benchmark.h"
#include "board.h"
#include "search.h"
#include "stats.h"
#include "ucitable.h"
#include "parser.h"
#include "eval.h"
#include "ttable.h"
static bool check_game_end(Board *b) {
if (b->winner != NOBODY) {
printf("%s wins!\n", name(b->winner));
return true;
} else if (board_full(b)) {
printf("Game drawn!\n");
return true;
} else {
return false;
}
}
static char read_column(Board *b) {
int c = ' ';
do {
if (c != '\n') {
printf("\n[0-6]> ");
}
c = getchar();
} while (c < '0' || c > '6' || board_column_full(b, c - '0'));
putchar('\n');
return c;
}
static void play_game() {
Board b;
board_init(&b);
// Reload the transposition table with positions that occur in the opening
// TODO: check whether this is helpful or not
FILE *f = fopen("data/ttable.ser", "r");
if (f) {
ttable_read(f);
fclose(f);
#ifdef DEBUG
printf("Read serialized ttable.");
#endif
}
#ifndef DISABLE_UCITABLE
f = fopen("data/connect-4.data", "r");
if (f) {
ucitable_read(f);
fclose(f);
#ifdef DEBUG
printf("Read UCI ML table.");
#endif
}
#endif
Player ai = WHITE;
while (true) {
if (b.turn == ai) {
// put piece selected by the engine
Variation var;
variation_init(&var);
SearchRecord rec;
searchrecord_init(&rec);
search(&b, &var, &rec);
#ifdef DEBUG
stats_print(&b, &var, &rec);
#endif
assert(var.length > 0);
assert(var.moves[0] >= 0);
assert(var.moves[0] < NUM_COLS);
board_put(&b, var.moves[0]);
if (check_game_end(&b)) {
break;
}
} else {
board_print(&b);
char c = read_column(&b);
// put piece selected by the human player
board_put(&b, c - '0');
board_print(&b);
if (check_game_end(&b)) {
break;
}
}
}
printf("\nFinal position\n");
board_print(&b);
}
int main() { //int argc, char** argv) {
#ifdef BENCHMARK
FILE *boards = tmpfile();
benchmark_sample(boards);
rewind(boards);
benchmark_run(boards, stdout, stdout);
fclose(boards);
#endif
puts("Welcome to 'Connect Four'!");
while (true) {
puts("\nA new game starts!\n");
play_game();
}
return (EXIT_SUCCESS);
}