Moggy 3x3: Do not consider pattern moves within eyes
[pachi/json.git] / gtp.c
blob1482bb6008e2db00927fbca93eb12b9a5d13036e
1 #define DEBUG
2 #include <assert.h>
3 #include <ctype.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include "board.h"
10 #include "debug.h"
11 #include "engine.h"
12 #include "gtp.h"
13 #include "uct/uct.h"
14 #include "version.h"
16 void
17 gtp_prefix(char prefix, int id)
19 if (id >= 0)
20 printf("%c%d ", prefix, id);
21 else
22 printf("%c ", prefix);
25 void
26 gtp_output(char prefix, int id, va_list params)
28 gtp_prefix(prefix, id);
29 char *s;
30 while ((s = va_arg(params, char *))) {
31 fputs(s, stdout);
33 putchar('\n'); putchar('\n');
34 fflush(stdout);
37 void
38 gtp_reply(int id, ...)
40 va_list params;
41 va_start(params, id);
42 gtp_output('=', id, params);
43 va_end(params);
46 void
47 gtp_error(int id, ...)
49 va_list params;
50 va_start(params, id);
51 gtp_output('?', id, params);
52 va_end(params);
56 /* XXX: THIS IS TOTALLY INSECURE!!!!
57 * Even basic input checking is missing. */
59 void
60 gtp_parse(struct board *board, struct engine *engine, char *buf)
62 #define next_tok(to_) \
63 to_ = next; \
64 next = next + strcspn(next, " \t\r\n"); \
65 if (*next) { \
66 *next = 0; next++; \
67 next += strspn(next, " \t\r\n"); \
70 if (strchr(buf, '#'))
71 *strchr(buf, '#') = 0;
73 char *cmd, *next = buf;
74 next_tok(cmd);
76 int id = -1;
77 if (isdigit(*cmd)) {
78 id = atoi(cmd);
79 next_tok(cmd);
82 if (!*cmd)
83 return;
85 if (!strcasecmp(cmd, "protocol_version")) {
86 gtp_reply(id, "2", NULL);
88 } else if (!strcasecmp(cmd, "name")) {
89 /* KGS hack */
90 gtp_reply(id, "Pachi ", engine->name, NULL);
92 } else if (!strcasecmp(cmd, "version")) {
93 gtp_reply(id, PACHI_VERSION, ": ", engine->comment, NULL);
95 /* TODO: known_command */
97 } else if (!strcasecmp(cmd, "list_commands")) {
98 gtp_reply(id, "protocol_version\nname\nversion\nlist_commands\nquit\nboardsize\nclear_board\nkomi\nplay\ngenmove\nset_free_handicap\nplace_free_handicap\nfinal_status_list", NULL);
100 } else if (!strcasecmp(cmd, "quit")) {
101 gtp_reply(id, NULL);
102 exit(0);
104 } else if (!strcasecmp(cmd, "boardsize")) {
105 char *arg;
106 next_tok(arg);
107 board_resize(board, atoi(arg));
109 gtp_reply(id, NULL);
111 } else if (!strcasecmp(cmd, "clear_board")) {
112 board_clear(board);
113 gtp_reply(id, NULL);
115 } else if (!strcasecmp(cmd, "komi")) {
116 char *arg;
117 next_tok(arg);
118 sscanf(arg, "%f", &board->komi);
120 gtp_reply(id, NULL);
122 } else if (!strcasecmp(cmd, "play")) {
123 struct move m;
125 char *arg;
126 next_tok(arg);
127 m.color = str2stone(arg);
128 next_tok(arg);
129 coord_t *c = str2coord(arg, board_size(board));
130 m.coord = *c; coord_done(c);
132 if (DEBUGL(1))
133 fprintf(stderr, "got move %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
134 engine->notify_play(engine, board, &m);
135 if (board_play(board, &m) < 0) {
136 if (DEBUGL(0)) {
137 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
138 board_print(board, stderr);
140 gtp_error(id, "illegal move", NULL);
141 } else {
142 gtp_reply(id, NULL);
145 } else if (!strcasecmp(cmd, "genmove")) {
146 char *arg;
147 next_tok(arg);
148 enum stone color = str2stone(arg);
149 coord_t *c = engine->genmove(engine, board, color);
150 struct move m = { *c, color };
151 board_play(board, &m);
152 char *str = coord2str(*c, board);
153 if (DEBUGL(1))
154 fprintf(stderr, "playing move %s\n", str);
155 gtp_reply(id, str, NULL);
156 free(str); coord_done(c);
158 } else if (!strcasecmp(cmd, "set_free_handicap")) {
159 struct move m;
160 m.color = S_BLACK;
162 char *arg;
163 next_tok(arg);
164 do {
165 coord_t *c = str2coord(arg, board_size(board));
166 m.coord = *c; coord_done(c);
167 if (DEBUGL(1))
168 fprintf(stderr, "setting handicap %d,%d\n", coord_x(m.coord, board), coord_y(m.coord, board));
170 if (board_play(board, &m) < 0) {
171 if (DEBUGL(0))
172 fprintf(stderr, "! ILLEGAL MOVE %d,%d,%d\n", m.color, coord_x(m.coord, board), coord_y(m.coord, board));
173 gtp_error(id, "illegal move", NULL);
175 next_tok(arg);
176 } while (*arg);
177 gtp_reply(id, NULL);
179 /* TODO: Engine should choose free handicap; however, it tends to take
180 * overly long to think it all out, and unless it's clever its
181 * handicap stones won't be of much help. ;-) */
182 } else if (!strcasecmp(cmd, "place_free_handicap")
183 || !strcasecmp(cmd, "fixed_handicap")) {
184 char *arg;
185 next_tok(arg);
186 int stones = atoi(arg);
188 gtp_prefix('=', id);
189 board_handicap(board, stones, stdout);
190 printf("\n\n"); fflush(stdout);
192 } else if (!strcasecmp(cmd, "final_score")) {
193 float score = board_official_score(board);
194 char str[64];
195 if (DEBUGL(1))
196 fprintf(stderr, "counted score %.1f\n", score);
197 if (score == 0) {
198 gtp_reply(id, "0", NULL);
199 } else if (score > 0) {
200 snprintf(str, 64, "W+%.1f", score);
201 gtp_reply(id, str, NULL);
202 } else {
203 snprintf(str, 64, "B+%.1f", -score);
204 gtp_reply(id, str, NULL);
207 /* XXX: This is a huge hack. */
208 } else if (!strcasecmp(cmd, "final_status_list")) {
209 char *arg;
210 next_tok(arg);
211 assert(!strcasecmp(arg, "dead")); // yes, I know...
212 gtp_reply(id, "", NULL);
214 /* Custom commands for handling UCT opening book */
215 } else if (!strcasecmp(cmd, "uct_genbook")) {
216 /* Board must be initialized properly, as if for genmove;
217 * makes sense only as 'uct_genbook b'. */
218 char *arg;
219 next_tok(arg);
220 enum stone color = str2stone(arg);
221 if (uct_genbook(engine, board, color))
222 gtp_reply(id, NULL);
223 else
224 gtp_error(id, "error generating book", NULL);
226 } else if (!strcasecmp(cmd, "uct_dumpbook")) {
227 char *arg;
228 next_tok(arg);
229 enum stone color = str2stone(arg);
230 uct_dumpbook(engine, board, color);
231 gtp_reply(id, NULL);
233 } else {
234 gtp_error(id, "unknown command", NULL);
237 #undef next_tok