15 gtp_prefix(char prefix
, int id
)
18 printf("%c%d ", prefix
, id
);
20 printf("%c ", prefix
);
24 gtp_output(char prefix
, int id
, va_list params
)
26 gtp_prefix(prefix
, id
);
28 while ((s
= va_arg(params
, char *))) {
31 putchar('\n'); putchar('\n');
36 gtp_reply(int id
, ...)
40 gtp_output('=', id
, params
);
45 gtp_error(int id
, ...)
49 gtp_output('?', id
, params
);
54 /* XXX: THIS IS TOTALLY INSECURE!!!!
55 * Even basic input checking is missing. */
58 gtp_parse(struct board
*board
, struct engine
*engine
, char *buf
)
60 #define next_tok(to_) \
62 next = next + strcspn(next, " \t\r\n"); \
65 next += strspn(next, " \t\r\n"); \
69 *strchr(buf
, '#') = 0;
71 char *cmd
, *next
= buf
;
83 if (!strcasecmp(cmd
, "protocol_version")) {
84 gtp_reply(id
, "2", NULL
);
86 } else if (!strcasecmp(cmd
, "name")) {
88 gtp_reply(id
, "Pachi ", engine
->name
, NULL
);
90 } else if (!strcasecmp(cmd
, "version")) {
91 gtp_reply(id
, PACHI_VERSION
, ": ", engine
->comment
, NULL
);
93 /* TODO: known_command */
95 } else if (!strcasecmp(cmd
, "list_commands")) {
96 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
);
98 } else if (!strcasecmp(cmd
, "quit")) {
102 } else if (!strcasecmp(cmd
, "boardsize")) {
105 board_resize(board
, atoi(arg
));
109 } else if (!strcasecmp(cmd
, "clear_board")) {
113 } else if (!strcasecmp(cmd
, "komi")) {
116 sscanf(arg
, "%f", &board
->komi
);
120 } else if (!strcasecmp(cmd
, "play")) {
125 m
.color
= str2stone(arg
);
127 coord_t
*c
= str2coord(arg
, board
->size
);
128 m
.coord
= *c
; coord_done(c
);
131 fprintf(stderr
, "got move %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
132 if (board_play(board
, &m
) < 0) {
134 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
135 board_print(board
, stderr
);
137 gtp_error(id
, "illegal move", NULL
);
142 } else if (!strcasecmp(cmd
, "genmove")) {
145 enum stone color
= str2stone(arg
);
146 coord_t
*c
= engine
->genmove(engine
, board
, color
);
147 struct move m
= { *c
, color
};
148 board_play(board
, &m
);
149 char *str
= coord2str(*c
, board
);
151 fprintf(stderr
, "playing move %s\n", str
);
152 gtp_reply(id
, str
, NULL
);
153 free(str
); coord_done(c
);
155 } else if (!strcasecmp(cmd
, "set_free_handicap")) {
162 coord_t
*c
= str2coord(arg
, board
->size
);
163 m
.coord
= *c
; coord_done(c
);
165 fprintf(stderr
, "setting handicap %d,%d\n", coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
167 if (board_play(board
, &m
) < 0) {
169 fprintf(stderr
, "! ILLEGAL MOVE %d,%d,%d\n", m
.color
, coord_x(m
.coord
, board
), coord_y(m
.coord
, board
));
170 gtp_error(id
, "illegal move", NULL
);
176 /* TODO: Engine should choose free handicap; however, it tends to take
177 * overly long to think it all out, and unless it's clever its
178 * handicap stones won't be of much help. ;-) */
179 } else if (!strcasecmp(cmd
, "place_free_handicap")
180 || !strcasecmp(cmd
, "fixed_handicap")) {
183 int stones
= atoi(arg
);
186 board_handicap(board
, stones
, stdout
);
187 printf("\n\n"); fflush(stdout
);
189 } else if (!strcasecmp(cmd
, "final_score")) {
190 float score
= board_official_score(board
);
193 fprintf(stderr
, "counted score %.1f\n", score
);
195 gtp_reply(id
, "0", NULL
);
196 } else if (score
> 0) {
197 snprintf(str
, 64, "W+%.1f", score
);
198 gtp_reply(id
, str
, NULL
);
200 snprintf(str
, 64, "B+%.1f", -score
);
201 gtp_reply(id
, str
, NULL
);
204 /* XXX: This is a huge hack. */
205 } else if (!strcasecmp(cmd
, "final_status_list")) {
208 assert(!strcasecmp(arg
, "dead")); // yes, I know...
209 gtp_reply(id
, "", NULL
);
212 gtp_error(id
, "unknown command", NULL
);