forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorottt.c
297 lines (256 loc) · 7.51 KB
/
corottt.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
/* Implementing coroutines with setjmp/longjmp */
#include "corottt.h"
#include <ctype.h>
#include "game.h"
void display_current_time()
{
time_t current_time;
struct tm *time_info;
char time_string[80];
current_time = time(NULL);
time_info = localtime(¤t_time);
strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", time_info);
printf("Current Time: %s\r\n", time_string);
fflush(stdout);
}
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);
}
static LIST_HEAD(tasklist);
static void (**tasks)(void *);
static struct arg *args;
static int ntasks;
static jmp_buf sched;
static struct task *cur_task;
static void task_add(struct task *task)
{
list_add_tail(&task->list, &tasklist);
}
static void task_switch()
{
if (!list_empty(&tasklist)) {
struct task *t = list_first_entry(&tasklist, struct task, list);
list_del(&t->list);
cur_task = t;
longjmp(t->env, 1);
}
}
void schedule(void)
{
static int i = 0;
setjmp(sched);
while (ntasks-- > 0) {
struct arg arg = args[i];
tasks[i++](&arg);
printf("Never reached\n");
}
task_switch();
}
/* A task yields control n times */
void task0(void *arg)
{
struct task *task = malloc(sizeof(struct task));
// strncpy(task->task_name, ((struct arg *) arg)->task_name, 5);
task->turn = ((struct arg *) arg)->turn;
task->table = ((struct arg *) arg)->table;
INIT_LIST_HEAD(&task->list);
if (setjmp(task->env) == 0) {
task_add(task);
longjmp(sched, 1);
}
task = cur_task;
for (;;) {
if (setjmp(task->env) == 0) {
char win = check_win(task->table);
if (win == 'D') {
draw_board(task->table);
printf("It is a draw!\n");
break;
} else if (win != ' ') {
draw_board(task->table);
printf("%c won!\n", win);
break;
}
int move = mcts(task->table, task->turn);
if (move != -1) {
task->table[move] = task->turn;
record_move(move);
}
draw_board(task->table);
task_add(task);
task_switch();
}
task = cur_task;
}
printf("%s: complete\n", task->task_name);
longjmp(sched, 1);
}
void task1(void *arg)
{
struct task *task = malloc(sizeof(struct task));
// strncpy(task->task_name, ((struct arg *) arg)->task_name, 8);
task->turn = ((struct arg *) arg)->turn;
task->table = ((struct arg *) arg)->table;
INIT_LIST_HEAD(&task->list);
if (setjmp(task->env) == 0) {
task_add(task);
longjmp(sched, 1);
}
task = cur_task;
for (;;) {
if (setjmp(task->env) == 0) {
char win = check_win(task->table);
if (win == 'D') {
draw_board(task->table);
printf("It is a draw!\n");
break;
} else if (win != ' ') {
draw_board(task->table);
printf("%c won!\n", win);
break;
}
int move = negamax_predict(task->table, task->turn).move;
if (move != -1) {
task->table[move] = task->turn;
record_move(move);
}
draw_board(task->table);
task_add(task);
task_switch();
}
task = cur_task;
}
printf("%s: complete\n", task->task_name);
longjmp(sched, 1);
}
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
void ttt_coro(int cvc)
{
srand(time(NULL));
char table[N_GRIDS];
memset(table, ' ', N_GRIDS);
if (cvc) {
negamax_init();
char task0Turn = 'X';
char task1Turn = 'O';
void (*registered_task[])(void *) = {task0, task1};
struct arg arg0 = {
.turn = task0Turn, .table = table, .task_name = "mcts"};
struct arg arg1 = {
.turn = task1Turn, .table = table, .task_name = "negamax"};
struct arg registered_arg[] = {arg0, arg1};
tasks = registered_task;
args = registered_arg;
ntasks = ARRAY_SIZE(registered_task);
schedule();
print_moves();
} else {
char turn = 'X';
char ai = 'O';
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) {
int move = mcts(table, ai);
if (move != -1) {
table[move] = ai;
record_move(move);
}
} 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();
}
}