-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.c
More file actions
362 lines (290 loc) · 8.74 KB
/
Copy pathinterface.c
File metadata and controls
362 lines (290 loc) · 8.74 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "gotodie.h"
#include "gtp.h"
/* Forward declarations. */
static int gtp_protocol_version(char *s);
static int gtp_name(char *s);
static int gtp_version(char *s);
static int gtp_known_command(char *s);
static int gtp_list_commands(char *s);
static int gtp_quit(char *s);
static int gtp_boardsize(char *s);
static int gtp_clear_board(char *s);
static int gtp_komi(char *s);
static int gtp_fixed_handicap(char *s);
static int gtp_place_free_handicap(char *s);
static int gtp_set_free_handicap(char *s);
static int gtp_play(char *s);
static int gtp_genmove(char *s);
static int gtp_final_score(char *s);
static int gtp_final_status_list(char *s);
static int gtp_showboard(char *s);
/* List of known commands. */
static struct gtp_command commands[] = {
{"protocol_version", gtp_protocol_version},
{"name", gtp_name},
{"version", gtp_version},
{"known_command", gtp_known_command},
{"list_commands", gtp_list_commands},
{"quit", gtp_quit},
{"boardsize", gtp_boardsize},
{"clear_board", gtp_clear_board},
{"komi", gtp_komi},
{"fixed_handicap", gtp_fixed_handicap},
{"place_free_handicap", gtp_place_free_handicap},
{"set_free_handicap", gtp_set_free_handicap},
{"play", gtp_play},
{"genmove", gtp_genmove},
{"final_score", gtp_final_score},
{"final_status_list", gtp_final_status_list},
{"showboard", gtp_showboard},
{NULL, NULL}
};
int main(int argc, char **argv)
{
setbuf(stdout, NULL);
gtp_internal_set_boardsize(board_size);
board_array_size = board_size * board_size;
init_gotodie();
gtp_main_loop(commands, stdin, stdout, NULL);
return 0;
}
static int gtp_protocol_version(char *s)
{
return gtp_success("2");
}
static int gtp_name(char *s)
{
return gtp_success(GOTODIE_NAME);
}
static int gtp_version(char *s)
{
return gtp_success(GOTODIE_VERSION);
}
static int gtp_known_command(char *s)
{
int i;
char command_name[GTP_BUFSIZE];
/* If no command name supplied, return false (this command never
* fails according to specification).
*/
if (sscanf(s, "%s", command_name) != 1)
return gtp_success("false");
for (i = 0; commands[i].name != NULL; i++)
if (strcmp(command_name, commands[i].name) == 0)
return gtp_success("true");
return gtp_success("false");
}
static int gtp_list_commands(char *s)
{
int i;
gtp_start_response(GTP_SUCCESS);
for (i = 0; commands[i].name != NULL; i++)
gtp_printf("%s\n", commands[i].name);
gtp_printf("\n");
return GTP_OK;
}
static int gtp_quit(char *s)
{
gtp_success("");
return GTP_QUIT;
}
static int gtp_boardsize(char *s)
{
int boardsize;
if (sscanf(s, "%d", &boardsize) < 1)
return gtp_failure("boardsize not an integer");
if (boardsize < MIN_BOARD || boardsize > MAX_BOARD)
return gtp_failure("unacceptable size");
board_size = boardsize;
gtp_internal_set_boardsize(boardsize);
board_array_size = boardsize * boardsize;
init_gotodie();
return gtp_success("");
}
static int gtp_clear_board(char *s)
{
clear_board(&main_board);
return gtp_success("");
}
static int gtp_komi(char *s)
{
if (sscanf(s, "%f", &komi) < 1)
return gtp_failure("komi not a float");
return gtp_success("");
}
/* Common code for fixed_handicap and place_free_handicap. */
static int place_handicap(char *s, int fixed)
{
int handicap;
int m, n;
int first_stone = 1;
if (!board_empty(&main_board))
return gtp_failure("board not empty");
if (sscanf(s, "%d", &handicap) < 1)
return gtp_failure("handicap not an integer");
if (handicap < 2)
return gtp_failure("invalid handicap");
if (fixed && !valid_fixed_handicap(handicap))
return gtp_failure("invalid handicap");
if (fixed)
place_fixed_handicap(&main_board, handicap);
else
place_free_handicap(&main_board, handicap);
gtp_start_response(GTP_SUCCESS);
for (m = 0; m < board_size; m++)
for (n = 0; n < board_size; n++)
if (main_board.board[POS(m, n)] != EMPTY) {
if (first_stone)
first_stone = 0;
else
gtp_printf(" ");
gtp_mprintf("%m", m, n);
}
return gtp_finish_response();
}
static int gtp_fixed_handicap(char *s)
{
return place_handicap(s, 1);
}
static int gtp_place_free_handicap(char *s)
{
return place_handicap(s, 0);
}
static int gtp_set_free_handicap(char *s)
{
int i, j;
int n;
int handicap = 0;
if (!board_empty(&main_board))
return gtp_failure("board not empty");
while ((n = gtp_decode_coord(s, &i, &j)) > 0) {
s += n;
if (main_board.board[POS(i, j)] != EMPTY) {
clear_board(&main_board);
return gtp_failure("repeated vertex");
}
play_move(&main_board, i, j, BLACK);
handicap++;
}
if (sscanf(s, "%*s") != EOF) {
clear_board(&main_board);
return gtp_failure("invalid coordinate");
}
if (handicap < 2 || handicap >= board_array_size) {
clear_board(&main_board);
return gtp_failure("invalid handicap");
}
return gtp_success("");
}
static int gtp_play(char *s)
{
int i, j;
int color = EMPTY;
if (!gtp_decode_move(s, &color, &i, &j))
return gtp_failure("invalid color or coordinate");
if (!legal_move(&main_board, i, j, color))
return gtp_failure("illegal move");
play_move(&main_board, i, j, color);
return gtp_success("");
}
static int gtp_genmove(char *s)
{
int i, j, pos;
int color = EMPTY;
if (!gtp_decode_color(s, &color))
return gtp_failure("invalid color");
pos = generate_move(&main_board, color);
i = I(pos);
j = J(pos);
play_move(&main_board, i, j, color);
gtp_start_response(GTP_SUCCESS);
gtp_mprintf("%m", i, j);
return gtp_finish_response();
}
/* Compute final score. We use area scoring since that is the only
* option that makes sense for this move generation algorithm.
*/
static int gtp_final_score(char *s)
{
float score = komi;
int i, j;
compute_final_status(&main_board);
for (i = 0; i < board_size; i++)
for (j = 0; j < board_size; j++) {
int status = get_final_status(&main_board, i, j);
if (status == BLACK_TERRITORY)
score--;
else if (status == WHITE_TERRITORY)
score++;
else if ((status == ALIVE) ^ (main_board.board[POS(i, j)] == WHITE))
score--;
else
score++;
}
if (score > 0.0)
return gtp_success("W+%3.1f", score);
if (score < 0.0)
return gtp_success("B+%3.1f", -score);
return gtp_success("0");
}
static int gtp_final_status_list(char *s)
{
int n;
int i, j;
int status = UNKNOWN;
char status_string[GTP_BUFSIZE];
int first_string;
if (sscanf(s, "%s %n", status_string, &n) != 1)
return gtp_failure("missing status");
if (!strcmp(status_string, "alive"))
status = ALIVE;
else if (!strcmp(status_string, "dead"))
status = DEAD;
else if (!strcmp(status_string, "seki"))
status = SEKI;
else
return gtp_failure("invalid status");
compute_final_status(&main_board);
gtp_start_response(GTP_SUCCESS);
first_string = 1;
for (i = 0; i < board_size; i++)
for (j = 0; j < board_size; j++)
if (get_final_status(&main_board, i, j) == status) {
int k;
int stonei[MAX_BOARDSIZE];
int stonej[MAX_BOARDSIZE];
int num_stones = get_string(&main_board, i, j, stonei, stonej);
/* Clear the status so we don't find the string again. */
for (k = 0; k < num_stones; k++)
set_final_status(&main_board, stonei[k], stonej[k], UNKNOWN);
if (first_string)
first_string = 0;
else
gtp_printf("\n");
gtp_print_vertices(num_stones, stonei, stonej);
}
return gtp_finish_response();
}
static int gtp_showboard(char *s)
{
int i, j, k;
int symbols[3] = {'.', 'O', 'X'};
gtp_start_response(GTP_SUCCESS);
gtp_printf("\n");
printf(" ");
for (k = 0; k < board_size; k++)
printf(" %c", 'A' + k + (k >= 8));
for (i = 0; i < board_size; i++) {
printf("\n%2d", board_size - i);
for (j = 0; j < board_size; j++)
printf(" %c", symbols[main_board.board[POS(i, j)]]);
printf(" %d", board_size - i);
}
printf("\n");
printf(" ");
for (k = 0; k < board_size; k++)
printf(" %c", 'A' + k + (k >= 8));
return gtp_finish_response();
}