-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.c
166 lines (155 loc) · 4.2 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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "game.h"
#ifdef USE_RL
#include "agents/reinforcement_learning.h"
#elif defined(USE_MCTS)
#include "agents/mcts.h"
#else
#include "agents/negamax.h"
#endif
static int move_record[N_GRIDS];
static int move_count = 0;
static void record_move(int move)
{
move_record[move_count++] = move;
}
static void print_moves()
{
printf("Moves: ");
for (int i = 0; i < move_count; i++) {
printf("%c%d", 'A' + GET_COL(move_record[i]),
1 + GET_ROW(move_record[i]));
if (i < move_count - 1) {
printf(" -> ");
}
}
printf("\n");
}
static int get_input(char player)
{
char *line = NULL;
size_t line_length = 0;
int parseX = 1;
int x = -1, y = -1;
while (x < 0 || x > (BOARD_SIZE - 1) || y < 0 || y > (BOARD_SIZE - 1)) {
printf("%c> ", player);
int r = getline(&line, &line_length, stdin);
if (r == -1)
exit(1);
if (r < 2)
continue;
x = 0;
y = 0;
parseX = 1;
for (int i = 0; i < (r - 1); i++) {
if (isalpha(line[i]) && parseX) {
x = x * 26 + (tolower(line[i]) - 'a' + 1);
if (x > BOARD_SIZE) {
// could be any value in [BOARD_SIZE + 1, INT_MAX]
x = BOARD_SIZE + 1;
printf("Invalid operation: index exceeds board size\n");
break;
}
continue;
}
// input does not have leading alphabets
if (x == 0) {
printf("Invalid operation: No leading alphabet\n");
y = 0;
break;
}
parseX = 0;
if (isdigit(line[i])) {
y = y * 10 + line[i] - '0';
if (y > BOARD_SIZE) {
// could be any value in [BOARD_SIZE + 1, INT_MAX]
y = BOARD_SIZE + 1;
printf("Invalid operation: index exceeds board size\n");
break;
}
continue;
}
// any other character is invalid
// any non-digit char during digit parsing is invalid
// TODO: Error message could be better by separating these two cases
printf("Invalid operation\n");
x = y = 0;
break;
}
x -= 1;
y -= 1;
}
free(line);
return GET_INDEX(y, x);
}
int main()
{
srand(time(NULL));
char table[N_GRIDS];
memset(table, ' ', N_GRIDS);
char turn = 'X';
char ai = 'O';
#ifdef USE_RL
rl_agent_t agent;
unsigned int state_num = 1;
CALC_STATE_NUM(state_num);
init_rl_agent(&agent, state_num, 'O');
load_model(&agent, state_num, MODEL_NAME);
#elif defined(USE_MCTS)
// A routine for initializing MCTS is not required.
#else
negamax_init();
#endif
while (1) {
char win = check_win(table);
if (win == 'D') {
draw_board(table);
printf("It is a draw!\n");
break;
} else if (win != ' ') {
draw_board(table);
printf("%c won!\n", win);
break;
}
if (turn == ai) {
#ifdef USE_RL
int move = play_rl(table, &agent);
record_move(move);
#elif defined(USE_MCTS)
int move = mcts(table, ai);
if (move != -1) {
table[move] = ai;
record_move(move);
}
#else
int move = negamax_predict(table, ai).move;
if (move != -1) {
table[move] = ai;
record_move(move);
}
#endif
} else {
draw_board(table);
int move;
while (1) {
move = get_input(turn);
if (table[move] == ' ') {
break;
}
printf("Invalid operation: the position has been marked\n");
}
table[move] = turn;
record_move(move);
}
turn = turn == 'X' ? 'O' : 'X';
}
print_moves();
return 0;
}