16 gtp_prefix(char prefix
, int id
)
19 printf("%c%d ", prefix
, id
);
21 printf("%c ", prefix
);
25 gtp_output(char prefix
, int id
, va_list params
)
27 gtp_prefix(prefix
, id
);
29 while ((s
= va_arg(params
, char *))) {
32 putchar('\n'); putchar('\n');
37 gtp_reply(int id
, ...)
41 gtp_output('=', id
, params
);
46 gtp_error(int id
, ...)
50 gtp_output('?', id
, params
);
55 /* XXX: THIS IS TOTALLY INSECURE!!!!
56 * Even basic input checking is missing. */
59 gtp_parse(struct board
*board
, struct engine
*engine
, char *buf
)
61 #define next_tok(to_) \
63 next = next + strcspn(next, " \t\r\n"); \
66 next += strspn(next, " \t\r\n"); \
70 *strchr(buf
, '#') = 0;
72 char *cmd
, *next
= buf
;
84 if (!strcasecmp(cmd
, "protocol_version")) {
85 gtp_reply(id
, "2", NULL
);
87 } else if (!strcasecmp(cmd
, "name")) {
89 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
91 } else if (!strcasecmp(cmd
, "version")) {
92 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
94 /* TODO: known_command */
96 } else if (!strcasecmp(cmd
, "list_commands")) {
97 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
);
99 } else if (!strcasecmp(cmd
, "quit")) {
103 } else if (!strcasecmp(cmd
, "boardsize")) {
106 board_resize(board
, atoi(arg
));
110 } else if (!strcasecmp(cmd
, "clear_board")) {
114 } else if (!strcasecmp(cmd
, "komi")) {
117 sscanf(arg
, "%f", &board
->komi
);
121 } else if (!strcasecmp(cmd
, "play")) {
126 m
.color
= str2stone(arg
);
128 coord_t
*c
= str2coord(arg
, board_size(board
));
129 m
.coord
= *c
; coord_done(c
);
132 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
133 engine
->notify_play(engine
, board
, &m
);
134 if (board_play(board
, &m
) < 0) {
136 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
137 board_print(board
, stderr
);
139 gtp_error(id
, "illegal move", NULL
);
144 } else if (!strcasecmp(cmd
, "genmove")) {
147 enum stone color
= str2stone(arg
);
148 coord_t
*c
= engine
->genmove(engine
, board
, color
);
149 struct move m
= { *c
, color
};
150 board_play(board
, &m
);
151 char *str
= coord2str(*c
, board
);
153 fprintf(stderr
, "playing move %s\n", str
);
154 gtp_reply(id
, str
, NULL
);
155 free(str
); coord_done(c
);
157 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
164 coord_t
*c
= str2coord(arg
, board_size(board
));
165 m
.coord
= *c
; coord_done(c
);
167 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
169 if (board_play(board
, &m
) < 0) {
171 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
172 gtp_error(id
, "illegal move", NULL
);
178 /* TODO: Engine should choose free handicap; however, it tends to take
179 * overly long to think it all out, and unless it's clever its
180 * handicap stones won't be of much help. ;-) */
181 } else if (!strcasecmp(cmd
, "place_free_handicap")
182 || !strcasecmp(cmd
, "fixed_handicap")) {
185 int stones
= atoi(arg
);
188 board_handicap(board
, stones
, stdout
);
189 printf("\n\n"); fflush(stdout
);
191 } else if (!strcasecmp(cmd
, "final_score")) {
192 float score
= board_official_score(board
);
195 fprintf(stderr
, "counted score %.1f\n", score
);
197 gtp_reply(id
, "0", NULL
);
198 } else if (score
> 0) {
199 snprintf(str
, 64, "W+%.1f", score
);
200 gtp_reply(id
, str
, NULL
);
202 snprintf(str
, 64, "B+%.1f", -score
);
203 gtp_reply(id
, str
, NULL
);
206 /* XXX: This is a huge hack. */
207 } else if (!strcasecmp(cmd
, "final_status_list")) {
210 assert(!strcasecmp(arg
, "dead")); // yes, I know...
211 gtp_reply(id
, "", NULL
);
213 /* Custom commands for handling UCT opening book */
214 } else if (!strcasecmp(cmd
, "uct_genbook")) {
215 /* Board must be initialized properly, as if for genmove;
216 * makes sense only as 'uct_genbook b'. */
219 enum stone color
= str2stone(arg
);
220 if (uct_genbook(engine
, board
, color
))
223 gtp_error(id
, "error generating book", NULL
);
225 } else if (!strcasecmp(cmd
, "uct_dumpbook")) {
228 enum stone color
= str2stone(arg
);
229 uct_dumpbook(engine
, board
, color
);
233 gtp_error(id
, "unknown command", NULL
);