14 gtp_prefix(char prefix
, int id
)
17 printf("%c%d ", prefix
, id
);
19 printf("%c ", prefix
);
23 gtp_output(char prefix
, int id
, va_list params
)
25 gtp_prefix(prefix
, id
);
27 while ((s
= va_arg(params
, char *))) {
30 putchar('\n'); putchar('\n');
35 gtp_reply(int id
, ...)
39 gtp_output('=', id
, params
);
44 gtp_error(int id
, ...)
48 gtp_output('?', id
, params
);
53 /* XXX: THIS IS TOTALLY INSECURE!!!!
54 * Even basic input checking is missing. */
57 gtp_parse(struct board
*board
, struct engine
*engine
, char *buf
)
59 #define next_tok(to_) \
61 next = next + strcspn(next, " \t\r\n"); \
64 next += strspn(next, " \t\r\n"); \
68 *strchr(buf
, '#') = 0;
70 char *cmd
, *next
= buf
;
82 if (!strcasecmp(cmd
, "protocol_version")) {
83 gtp_reply(id
, "2", NULL
);
85 } else if (!strcasecmp(cmd
, "name")) {
87 gtp_reply(id
, "Pachi ", engine
->name
, ": ", engine
->comment
, NULL
);
89 } else if (!strcasecmp(cmd
, "version")) {
92 /* TODO: known_command */
94 } else if (!strcasecmp(cmd
, "list_commands")) {
95 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
);
97 } else if (!strcasecmp(cmd
, "quit")) {
101 } else if (!strcasecmp(cmd
, "boardsize")) {
104 board_resize(board
, atoi(arg
));
108 } else if (!strcasecmp(cmd
, "clear_board")) {
112 } else if (!strcasecmp(cmd
, "komi")) {
115 sscanf(arg
, "%f", &board
->komi
);
119 } else if (!strcasecmp(cmd
, "play")) {
124 m
.color
= str2stone(arg
);
126 coord_t
*c
= str2coord(arg
, board
->size
);
127 m
.coord
= *c
; coord_done(c
);
130 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
131 if (board_play(board
, &m
) < 0) {
133 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
134 board_print(board
, stderr
);
136 gtp_error(id
, "illegal move", NULL
);
141 } else if (!strcasecmp(cmd
, "genmove")) {
144 enum stone color
= str2stone(arg
);
145 coord_t
*c
= engine
->genmove(engine
, board
, color
);
146 struct move m
= { *c
, color
};
147 board_play(board
, &m
);
148 char *str
= coord2str(*c
, board
);
150 fprintf(stderr
, "playing move %s\n", str
);
151 gtp_reply(id
, str
, NULL
);
152 free(str
); coord_done(c
);
154 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
161 coord_t
*c
= str2coord(arg
, board
->size
);
162 m
.coord
= *c
; coord_done(c
);
164 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
166 if (board_play(board
, &m
) < 0) {
168 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
169 gtp_error(id
, "illegal move", NULL
);
175 /* TODO: Engine should choose free handicap; however, it tends to take
176 * overly long to think it all out, and unless it's clever its
177 * handicap stones won't be of much help. ;-) */
178 } else if (!strcasecmp(cmd
, "place_free_handicap")
179 || !strcasecmp(cmd
, "fixed_handicap")) {
182 int stones
= atoi(arg
);
185 board_handicap(board
, stones
, stdout
);
186 printf("\n\n"); fflush(stdout
);
188 } else if (!strcasecmp(cmd
, "final_score")) {
189 float score
= board_official_score(board
);
192 fprintf(stderr
, "counted score %.1f\n", score
);
194 gtp_reply(id
, "0", NULL
);
195 } else if (score
> 0) {
196 snprintf(str
, 64, "W+%.1f", score
);
197 gtp_reply(id
, str
, NULL
);
199 snprintf(str
, 64, "B+%.1f", -score
);
200 gtp_reply(id
, str
, NULL
);
203 /* XXX: This is a huge hack. */
204 } else if (!strcasecmp(cmd
, "final_status_list")) {
207 assert(!strcasecmp(arg
, "dead")); // yes, I know...
208 gtp_reply(id
, "", NULL
);
211 gtp_error(id
, "unknown command", NULL
);