Commit | Line | Data |
---|---|---|
7eeb782e AT |
1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ |
2 | * This is GNU Go, a Go program. Contact gnugo@gnu.org, or see * | |
3 | * http://www.gnu.org/software/gnugo/ for more information. * | |
4 | * * | |
5 | * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * | |
6 | * 2008 and 2009 by the Free Software Foundation. * | |
7 | * * | |
8 | * This program is free software; you can redistribute it and/or * | |
9 | * modify it under the terms of the GNU General Public License as * | |
10 | * published by the Free Software Foundation - version 3 or * | |
11 | * (at your option) any later version. * | |
12 | * * | |
13 | * This program is distributed in the hope that it will be useful, * | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
16 | * GNU General Public License in file COPYING for more details. * | |
17 | * * | |
18 | * You should have received a copy of the GNU General Public * | |
19 | * License along with this program; if not, write to the Free * | |
20 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * | |
21 | * Boston, MA 02111, USA. * | |
22 | \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
23 | ||
24 | #include "gnugo.h" | |
25 | ||
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <ctype.h> | |
30 | ||
31 | #if READLINE | |
32 | #include <readline/readline.h> | |
33 | #include <readline/history.h> | |
34 | #endif | |
35 | ||
36 | #include "liberty.h" | |
37 | #include "interface.h" | |
38 | #include "sgftree.h" | |
39 | #include "gg_utils.h" | |
40 | ||
41 | #define DEBUG_COMMANDS "\ | |
42 | capture <pos> try to capture indicated group\n\ | |
43 | dead Toggle display of dead stones\n\ | |
44 | defend <pos> try to defend indicated group\n\ | |
45 | listdragons print dragon info \n\ | |
46 | showarea display area\n\ | |
47 | showdragons display dragons\n\ | |
48 | showmoyo display moyo\n\ | |
49 | showterri display territory\n\ | |
50 | " | |
51 | ||
52 | /* some options for the ascii interface */ | |
53 | static int opt_showboard = 1; | |
54 | static int showdead = 0; | |
55 | static SGFTree sgftree; | |
56 | static int resignation_allowed; | |
57 | ||
58 | static int clock_on = 0; | |
59 | ||
60 | /* Unreasonable score used to detect missing information. */ | |
61 | #define NO_SCORE 4711 | |
62 | /* Keep track of the score estimated before the last computer move. */ | |
63 | static int current_score_estimate = NO_SCORE; | |
64 | ||
65 | static void do_play_ascii(Gameinfo *gameinfo); | |
66 | static int ascii_endgame(Gameinfo *gameinfo, int reason); | |
67 | static void ascii_count(Gameinfo *gameinfo); | |
68 | static void showcapture(char *line); | |
69 | static void showdefense(char *line); | |
70 | static void ascii_goto(Gameinfo *gameinfo, char *line); | |
71 | static void ascii_free_handicap(Gameinfo *gameinfo, char *handicap_string); | |
72 | ||
73 | /* If sgf game info is written can't reset parameters like handicap, etc. */ | |
74 | static int sgf_initialized; | |
75 | ||
76 | /* | |
77 | * Create letterbar for the top and bottom of the ASCII board. | |
78 | */ | |
79 | ||
80 | static void | |
81 | make_letterbar(int boardsize, char *letterbar) | |
82 | { | |
83 | int i, letteroffset; | |
84 | char spaces[64]; | |
85 | char letter[64]; | |
86 | ||
87 | if (boardsize <= 25) | |
88 | strcpy(spaces, " "); | |
89 | strcpy(letterbar, " "); | |
90 | ||
91 | for (i = 0; i < boardsize; i++) { | |
92 | letteroffset = 'A'; | |
93 | if (i+letteroffset >= 'I') | |
94 | letteroffset++; | |
95 | strcat(letterbar, spaces); | |
96 | sprintf(letter, "%c", i+letteroffset); | |
97 | strcat(letterbar, letter); | |
98 | } | |
99 | } | |
100 | ||
101 | ||
102 | /* This array contains +'s and -'s for the empty board positions. | |
103 | * hspot_size contains the board size that the grid has been | |
104 | * initialized to. | |
105 | */ | |
106 | ||
107 | static int hspot_size; | |
108 | static char hspots[MAX_BOARD][MAX_BOARD]; | |
109 | ||
110 | ||
111 | /* | |
112 | * Mark the handicap spots on the board. | |
113 | */ | |
114 | ||
115 | static void | |
116 | set_handicap_spots(int boardsize) | |
117 | { | |
118 | if (hspot_size == boardsize) | |
119 | return; | |
120 | ||
121 | hspot_size = boardsize; | |
122 | ||
123 | memset(hspots, '.', sizeof(hspots)); | |
124 | ||
125 | if (boardsize == 5) { | |
126 | /* place the outer 4 */ | |
127 | hspots[1][1] = '+'; | |
128 | hspots[boardsize-2][1] = '+'; | |
129 | hspots[1][boardsize-2] = '+'; | |
130 | hspots[boardsize-2][boardsize-2] = '+'; | |
131 | /* and the middle one */ | |
132 | hspots[boardsize/2][boardsize/2] = '+'; | |
133 | return; | |
134 | } | |
135 | ||
136 | if (!(boardsize%2)) { | |
137 | /* If the board size is even, no center handicap spots. */ | |
138 | if (boardsize > 2 && boardsize < 12) { | |
139 | /* Place the outer 4 only. */ | |
140 | hspots[2][2] = '+'; | |
141 | hspots[boardsize-3][2] = '+'; | |
142 | hspots[2][boardsize-3] = '+'; | |
143 | hspots[boardsize-3][boardsize-3] = '+'; | |
144 | } | |
145 | else { | |
146 | /* Place the outer 4 only. */ | |
147 | hspots[3][3] = '+'; | |
148 | hspots[boardsize-4][3] = '+'; | |
149 | hspots[3][boardsize-4] = '+'; | |
150 | hspots[boardsize-4][boardsize-4] = '+'; | |
151 | } | |
152 | } | |
153 | else { | |
154 | /* Uneven board size */ | |
155 | if (boardsize > 2 && boardsize < 12) { | |
156 | /* Place the outer 4... */ | |
157 | hspots[2][2] = '+'; | |
158 | hspots[boardsize-3][2] = '+'; | |
159 | hspots[2][boardsize-3] = '+'; | |
160 | hspots[boardsize-3][boardsize-3] = '+'; | |
161 | ||
162 | /* ...and the middle one. */ | |
163 | hspots[boardsize/2][boardsize/2] = '+'; | |
164 | } | |
165 | else if (boardsize > 12) { | |
166 | /* Place the outer 4... */ | |
167 | hspots[3][3] = '+'; | |
168 | hspots[boardsize-4][3] = '+'; | |
169 | hspots[3][boardsize-4] = '+'; | |
170 | hspots[boardsize-4][boardsize-4] = '+'; | |
171 | ||
172 | /* ...and the inner 4... */ | |
173 | hspots[3][boardsize/2] = '+'; | |
174 | hspots[boardsize/2][3] = '+'; | |
175 | hspots[boardsize/2][boardsize-4] = '+'; | |
176 | hspots[boardsize-4][boardsize/2] = '+'; | |
177 | ||
178 | /* ...and the middle one. */ | |
179 | hspots[boardsize/2][boardsize/2] = '+'; | |
180 | } | |
181 | } | |
182 | ||
183 | return; | |
184 | } | |
185 | ||
186 | ||
59cca85c AT |
187 | #define WHITECOLOR "\x1B[41m" |
188 | #define BLACKCOLOR "\x1B[44m" | |
189 | #define RESETCOLOR "\x1B[0m" | |
190 | ||
191 | #define BACK1COLOR "\x1B[47m" | |
192 | #define BACK2COLOR "\x1B[107m" | |
193 | ||
7eeb782e AT |
194 | /* |
195 | * Display the board position when playing in ASCII. | |
196 | */ | |
197 | ||
198 | static void | |
199 | ascii_showboard(void) | |
200 | { | |
201 | int i, j; | |
202 | char letterbar[64]; | |
203 | int last_pos_was_move; | |
204 | int pos_is_move; | |
205 | int dead; | |
206 | int last_move = get_last_move(); | |
207 | ||
208 | make_letterbar(board_size, letterbar); | |
209 | set_handicap_spots(board_size); | |
210 | ||
211 | printf("\n"); | |
59cca85c AT |
212 | printf(" White (" WHITECOLOR " " RESETCOLOR ") has captured %d pieces\n", black_captured); |
213 | printf(" Black (" BLACKCOLOR " " RESETCOLOR ") has captured %d pieces\n", white_captured); | |
7eeb782e AT |
214 | if (showscore) { |
215 | if (current_score_estimate == NO_SCORE) | |
216 | printf(" No score estimate is available yet.\n"); | |
217 | else if (current_score_estimate < 0) | |
218 | printf(" Estimated score: Black is ahead by %d\n", | |
219 | -current_score_estimate); | |
220 | else if (current_score_estimate > 0) | |
221 | printf(" Estimated score: White is ahead by %d\n", | |
222 | current_score_estimate); | |
223 | else | |
224 | printf(" Estimated score: Even!\n"); | |
225 | } | |
226 | ||
227 | printf("\n"); | |
228 | ||
229 | fflush(stdout); | |
230 | printf("%s", letterbar); | |
231 | ||
232 | if (get_last_player() != EMPTY) { | |
233 | gfprintf(stdout, " Last move: %s %1m", | |
234 | get_last_player() == WHITE ? "White" : "Black", | |
235 | last_move); | |
236 | } | |
237 | ||
238 | printf("\n"); | |
239 | fflush(stdout); | |
240 | ||
59cca85c AT |
241 | for (i = 0; i < board_size; i++) { |
242 | printf(" %2d", board_size - i); | |
243 | last_pos_was_move = 0; | |
244 | for (j = 0; j < board_size; j++) { | |
245 | if (POS(i, j) == last_move) pos_is_move = 128; | |
246 | else pos_is_move = 0; | |
247 | dead = (dragon_status(POS(i, j)) == DEAD) && showdead; | |
248 | switch (BOARD(i, j) + pos_is_move + last_pos_was_move) { | |
249 | case EMPTY+128: | |
250 | case EMPTY: | |
251 | if (i % 2 == 0) { | |
252 | if (j % 2 == 0) { | |
253 | printf(BACK1COLOR " " RESETCOLOR); | |
254 | } else { | |
255 | printf(BACK2COLOR " " RESETCOLOR); | |
256 | } | |
257 | } else { | |
258 | if (j % 2 == 0) { | |
259 | printf(BACK2COLOR " " RESETCOLOR); | |
260 | } else { | |
261 | printf(BACK1COLOR " " RESETCOLOR); | |
262 | } | |
263 | } | |
264 | last_pos_was_move = 0; | |
265 | break; | |
266 | case BLACK: | |
267 | printf(BLACKCOLOR " " RESETCOLOR); | |
268 | last_pos_was_move = 0; | |
269 | break; | |
270 | case WHITE: | |
271 | printf(WHITECOLOR " " RESETCOLOR); | |
272 | last_pos_was_move = 0; | |
273 | break; | |
274 | case BLACK+128: | |
275 | printf(BLACKCOLOR "()" RESETCOLOR); | |
276 | last_pos_was_move = 256; | |
277 | break; | |
278 | case WHITE+128: | |
279 | printf(WHITECOLOR "()" RESETCOLOR); | |
280 | last_pos_was_move = 256; | |
281 | break; | |
282 | case EMPTY+256: | |
283 | if (i % 2 == 0) { | |
284 | if (j % 2 == 0) { | |
285 | printf(BACK1COLOR " " RESETCOLOR); | |
286 | } else { | |
287 | printf(BACK2COLOR " " RESETCOLOR); | |
288 | } | |
289 | } else { | |
290 | if (j % 2 == 0) { | |
291 | printf(BACK2COLOR " " RESETCOLOR); | |
292 | } else { | |
293 | printf(BACK1COLOR " " RESETCOLOR); | |
294 | } | |
295 | } | |
296 | last_pos_was_move = 0; | |
297 | break; | |
298 | case BLACK+256: | |
299 | printf(BLACKCOLOR " " RESETCOLOR); | |
300 | last_pos_was_move = 0; | |
301 | break; | |
302 | case WHITE+256: | |
303 | printf(WHITECOLOR " " RESETCOLOR); | |
304 | last_pos_was_move = 0; | |
305 | break; | |
306 | default: | |
307 | fprintf(stderr, "Illegal board value %d\n", (int) BOARD(i, j)); | |
308 | exit(EXIT_FAILURE); | |
309 | break; | |
310 | } | |
311 | } | |
7eeb782e AT |
312 | |
313 | if (last_pos_was_move == 0) { | |
314 | if (board_size > 10) | |
315 | printf(" %2d", board_size - i); | |
316 | else | |
317 | printf(" %1d", board_size - i); | |
318 | } | |
319 | else { | |
320 | if (board_size > 10) | |
321 | printf("%2d", board_size - i); | |
322 | else | |
323 | printf("%1d", board_size - i); | |
324 | } | |
325 | printf("\n"); | |
326 | } | |
327 | ||
328 | fflush(stdout); | |
329 | printf("%s\n\n", letterbar); | |
330 | fflush(stdout); | |
331 | ||
332 | if (clock_on) { | |
333 | clock_print(WHITE); | |
334 | clock_print(BLACK); | |
335 | } | |
336 | ||
337 | } /* end ascii_showboard */ | |
338 | ||
339 | /* | |
340 | * command help | |
341 | */ | |
342 | ||
343 | static void | |
344 | show_commands(void) | |
345 | { | |
346 | printf("\nCommands:\n"); | |
347 | printf(" back Take back your last move\n"); | |
348 | printf(" boardsize Set boardsize (on move 1 only)\n"); | |
349 | printf(" comment Write a comment to outputfile\n"); | |
350 | printf(" depth <num> Set depth for reading\n"); | |
351 | printf(" display Display game board\n"); | |
352 | printf(" exit Exit GNU Go\n"); | |
353 | printf(" force <move> Force a move for current color\n"); | |
354 | printf(" forward Go to next node in game tree\n"); | |
355 | printf(" goto <movenum> Go to movenum in game tree\n"); | |
356 | printf(" level <amount> Playing level (default = 10)\n"); | |
357 | printf(" handicap <num> Set fixed handicap (on move 1 only)\n"); | |
358 | printf(" freehandicap <num> Place free handicap (on move 1 only)\n"); | |
359 | printf(" Omit <num> to place handicap yourself\n"); | |
360 | printf(" help Display this help menu\n"); | |
361 | printf(" helpdebug Display debug help menu\n"); | |
362 | printf(" info Display program settings\n"); | |
363 | printf(" komi Set komi (on move 1 only)\n"); | |
364 | printf(" last Goto last node in game tree\n"); | |
365 | printf(" pass Pass on your move\n"); | |
366 | printf(" play <num> Play <num> moves\n"); | |
367 | printf(" playblack Play as Black (switch if White)\n"); | |
368 | printf(" playwhite Play as White (switch if Black)\n"); | |
369 | printf(" quit Exit GNU Go\n"); | |
370 | printf(" resign Resign the current game\n"); | |
371 | printf(" save <file> Save the current game\n"); | |
372 | printf(" load <file> Load a game from file\n"); | |
373 | printf(" score Toggle display of score On/Off\n"); | |
374 | printf(" showboard Toggle display of board On/Off\n"); | |
375 | printf(" switch Switch the color you are playing\n"); | |
376 | printf(" undo Take the last move back (same as back)\n"); | |
377 | printf(" <move> A move of the format <letter><number>"); | |
378 | printf("\n"); | |
379 | } | |
380 | ||
381 | enum commands {INVALID = -1, END, EXIT, QUIT, RESIGN, | |
382 | PASS, MOVE, FORCE, SWITCH, | |
383 | PLAY, PLAYBLACK, PLAYWHITE, | |
384 | SETHANDICAP, FREEHANDICAP, SETBOARDSIZE, SETKOMI, | |
385 | SETDEPTH, | |
386 | INFO, DISPLAY, SHOWBOARD, HELP, UNDO, COMMENT, SCORE, | |
387 | CMD_DEAD, CMD_BACK, CMD_FORWARD, CMD_LAST, | |
388 | CMD_CAPTURE, CMD_DEFEND, | |
389 | CMD_HELPDEBUG, CMD_SHOWAREA, CMD_SHOWMOYO, CMD_SHOWTERRI, | |
390 | CMD_GOTO, CMD_SAVE, CMD_LOAD, CMD_SHOWDRAGONS, CMD_LISTDRAGONS, | |
391 | SETLEVEL, NEW, COUNT, CONTINUE | |
392 | }; | |
393 | ||
394 | ||
395 | /* | |
396 | * Check if we have a valid command. | |
397 | */ | |
398 | ||
399 | static int | |
400 | get_command(char *command) | |
401 | { | |
402 | char c; | |
403 | int d; | |
404 | ||
405 | /* Check to see if a move was input. */ | |
406 | if (!((sscanf(command, "%c%d", &c, &d) != 2) | |
407 | || ((c = toupper((int) c)) < 'A') | |
408 | || ((c = toupper((int) c)) > 'Z') | |
409 | || (c == 'I'))) | |
410 | return MOVE; | |
411 | ||
412 | /* help shortcut */ | |
413 | if (command[0] == '?') | |
414 | return HELP; | |
415 | ||
416 | /* Kill leading spaces. */ | |
417 | while (command[0] == ' ') | |
418 | command++; | |
419 | ||
420 | if (!strncmp(command, "playblack", 9)) return PLAYBLACK; | |
421 | if (!strncmp(command, "playwhite", 9)) return PLAYWHITE; | |
422 | if (!strncmp(command, "showboard", 9)) return SHOWBOARD; | |
423 | if (!strncmp(command, "showdragons", 9)) return CMD_SHOWDRAGONS; | |
424 | if (!strncmp(command, "listdragons", 9)) return CMD_LISTDRAGONS; | |
425 | if (!strncmp(command, "boardsize", 9)) return SETBOARDSIZE; | |
426 | if (!strncmp(command, "freehandicap", 9)) return FREEHANDICAP; | |
427 | if (!strncmp(command, "handicap", 5)) return SETHANDICAP; | |
428 | if (!strncmp(command, "display", 7)) return DISPLAY; | |
429 | if (!strncmp(command, "helpdebug", 7)) return CMD_HELPDEBUG; | |
430 | if (!strncmp(command, "resign", 6)) return RESIGN; | |
431 | if (!strncmp(command, "showmoyo", 6)) return CMD_SHOWMOYO; | |
432 | if (!strncmp(command, "showterri", 6)) return CMD_SHOWTERRI; | |
433 | if (!strncmp(command, "showarea", 6)) return CMD_SHOWAREA; | |
434 | if (!strncmp(command, "depth", 5)) return SETDEPTH; | |
435 | if (!strncmp(command, "switch", 5)) return SWITCH; | |
436 | if (!strncmp(command, "komi", 4)) return SETKOMI; | |
437 | if (!strncmp(command, "play", 4)) return PLAY; | |
438 | if (!strncmp(command, "info", 4)) return INFO; | |
439 | if (!strncmp(command, "force", 4)) return FORCE; | |
440 | if (!strncmp(command, "level", 5)) return SETLEVEL; | |
441 | if (!strncmp(command, "pass", 4)) return PASS; | |
442 | if (!strncmp(command, "save", 3)) return CMD_SAVE; | |
443 | if (!strncmp(command, "load", 3)) return CMD_LOAD; | |
444 | if (!strncmp(command, "end", 3)) return END; | |
445 | if (!strncmp(command, "move", 3)) return MOVE; | |
446 | if (!strncmp(command, "undo", 3)) return UNDO; | |
447 | if (!strncmp(command, "comment", 3)) return COMMENT; | |
448 | if (!strncmp(command, "score", 3)) return SCORE; | |
449 | if (!strncmp(command, "dead", 3)) return CMD_DEAD; | |
450 | if (!strncmp(command, "capture", 3)) return CMD_CAPTURE; | |
451 | if (!strncmp(command, "defend", 3)) return CMD_DEFEND; | |
452 | if (!strncmp(command, "exit", 4)) return EXIT; | |
453 | if (!strncmp(command, "quit", 4)) return QUIT; | |
454 | if (!strncmp(command, "help", 1)) return HELP; | |
455 | if (!strncmp(command, "back", 2)) return CMD_BACK; | |
456 | if (!strncmp(command, "forward", 2)) return CMD_FORWARD; | |
457 | if (!strncmp(command, "last", 2)) return CMD_LAST; | |
458 | if (!strncmp(command, "goto", 2)) return CMD_GOTO; | |
459 | if (!strncmp(command, "game", 2)) return NEW; | |
460 | if (!strncmp(command, "count", 3)) return COUNT; | |
461 | if (!strncmp(command, "continue", 4)) return CONTINUE; | |
462 | ||
463 | /* No valid command was found. */ | |
464 | return INVALID; | |
465 | } | |
466 | ||
467 | ||
468 | /* | |
469 | * Write sgf root node. | |
470 | */ | |
471 | ||
472 | static void | |
473 | init_sgf(Gameinfo *ginfo) | |
474 | { | |
475 | if (sgf_initialized) | |
476 | return; | |
477 | sgf_initialized = 1; | |
478 | ||
479 | sgf_write_header(sgftree.root, 1, get_random_seed(), komi, | |
480 | ginfo->handicap, get_level(), chinese_rules); | |
481 | if (ginfo->handicap > 0) | |
482 | sgffile_recordboard(sgftree.root); | |
483 | } | |
484 | ||
485 | ||
486 | /* | |
487 | * Generate the computer move. | |
488 | */ | |
489 | ||
490 | static int | |
491 | computer_move(Gameinfo *gameinfo, int *passes) | |
492 | { | |
493 | int move; | |
494 | float move_value; | |
495 | int resign; | |
496 | int resignation_declined = 0; | |
497 | float upper_bound, lower_bound; | |
498 | ||
499 | init_sgf(gameinfo); | |
500 | ||
501 | /* Generate computer move. */ | |
502 | if (autolevel_on) | |
503 | adjust_level_offset(gameinfo->to_move); | |
504 | move = genmove(gameinfo->to_move, &move_value, &resign); | |
505 | if (resignation_allowed && resign) { | |
506 | int state = ascii_endgame(gameinfo, 2); | |
507 | if (state != -1) | |
508 | return state; | |
509 | ||
510 | /* The opponent declined resignation. Remember not to resign again. */ | |
511 | resignation_allowed = 0; | |
512 | resignation_declined = 1; | |
513 | } | |
514 | ||
515 | if (showscore) { | |
516 | gnugo_estimate_score(&upper_bound, &lower_bound); | |
517 | current_score_estimate = (int) ((lower_bound + upper_bound) / 2.0); | |
518 | } | |
519 | ||
520 | mprintf("%s(%d): %1m\n", color_to_string(gameinfo->to_move), | |
521 | movenum + 1, move); | |
522 | if (is_pass(move)) | |
523 | (*passes)++; | |
524 | else | |
525 | *passes = 0; | |
526 | ||
527 | gnugo_play_move(move, gameinfo->to_move); | |
528 | sgffile_add_debuginfo(sgftree.lastnode, move_value); | |
529 | sgftreeAddPlay(&sgftree, gameinfo->to_move, I(move), J(move)); | |
530 | if (resignation_declined) | |
531 | sgftreeAddComment(&sgftree, "GNU Go resignation was declined"); | |
532 | sgffile_output(&sgftree); | |
533 | ||
534 | gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); | |
535 | return 0; | |
536 | } | |
537 | ||
538 | ||
539 | /* | |
540 | * Make a move. | |
541 | */ | |
542 | ||
543 | static int | |
544 | do_move(Gameinfo *gameinfo, char *command, int *passes, int force) | |
545 | { | |
546 | int move = string_to_location(board_size, command); | |
547 | ||
548 | if (move == NO_MOVE) { | |
549 | printf("\nInvalid move: %s\n", command); | |
550 | return 0; | |
551 | } | |
552 | ||
553 | if (!is_allowed_move(move, gameinfo->to_move)) { | |
554 | printf("\nIllegal move: %s", command); | |
555 | return 0; | |
556 | } | |
557 | ||
558 | *passes = 0; | |
559 | TRACE("\nyour move: %1m\n\n", move); | |
560 | init_sgf(gameinfo); | |
561 | gnugo_play_move(move, gameinfo->to_move); | |
562 | sgffile_add_debuginfo(sgftree.lastnode, 0.0); | |
563 | sgftreeAddPlay(&sgftree, gameinfo->to_move, I(move), J(move)); | |
564 | sgffile_output(&sgftree); | |
565 | ||
566 | if (opt_showboard) { | |
567 | ascii_showboard(); | |
568 | printf("GNU Go is thinking...\n"); | |
569 | } | |
570 | ||
571 | if (force) { | |
572 | gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player); | |
573 | gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); | |
574 | sgftreeAddComment(&sgftree, "forced"); | |
575 | return 0; | |
576 | } | |
577 | ||
578 | gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); | |
579 | return computer_move(gameinfo, passes); | |
580 | } | |
581 | ||
582 | ||
583 | /* | |
584 | * Make a pass. | |
585 | */ | |
586 | ||
587 | static int | |
588 | do_pass(Gameinfo *gameinfo, int *passes, int force) | |
589 | { | |
590 | (*passes)++; | |
591 | init_sgf(gameinfo); | |
592 | gnugo_play_move(PASS_MOVE, gameinfo->to_move); | |
593 | sgffile_add_debuginfo(sgftree.lastnode, 0.0); | |
594 | sgftreeAddPlay(&sgftree, gameinfo->to_move, -1, -1); | |
595 | sgffile_output(&sgftree); | |
596 | ||
597 | gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); | |
598 | if (force) { | |
599 | gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player); | |
600 | sgftreeAddComment(&sgftree, "forced"); | |
601 | return 0; | |
602 | } | |
603 | ||
604 | return computer_move(gameinfo, passes); | |
605 | } | |
606 | ||
607 | ||
608 | /* | |
609 | * Play a game against an ascii client. | |
610 | */ | |
611 | ||
612 | void | |
613 | play_ascii(SGFTree *tree, Gameinfo *gameinfo, char *filename, char *until) | |
614 | { | |
615 | int sz; | |
616 | ||
617 | setvbuf(stdout, (char *)NULL, _IONBF, 0); /* No buffering. */ | |
618 | ||
619 | sgftree = *tree; | |
620 | ||
621 | if (filename) { | |
622 | /* No need to check for failure here since that was already done | |
623 | * when it was loaded in main(). | |
624 | * | |
625 | * FIXME: Why do we load the game again? | |
626 | */ | |
627 | gameinfo_play_sgftree(gameinfo, &sgftree, until); | |
628 | sgf_initialized = 1; | |
629 | } | |
630 | else { | |
631 | if (sgfGetIntProperty(sgftree.root, "SZ", &sz)) | |
632 | gnugo_clear_board(sz); | |
633 | if (gameinfo->handicap == 0) | |
634 | gameinfo->to_move = BLACK; | |
635 | else { | |
636 | gameinfo->handicap = place_fixed_handicap(gameinfo->handicap); | |
637 | gameinfo->to_move = WHITE; | |
638 | } | |
639 | sgf_initialized = 0; | |
640 | } | |
641 | ||
642 | do_play_ascii(gameinfo); | |
643 | printf("\nThanks! for playing GNU Go.\n\n"); | |
644 | ||
645 | /* main() frees the tree and we might have changed it. */ | |
646 | *tree = sgftree; | |
647 | } | |
648 | ||
649 | ||
650 | void | |
651 | do_play_ascii(Gameinfo *gameinfo) | |
652 | { | |
653 | int m, num; | |
654 | float fnum; | |
655 | int passes = 0; /* two passes and its over */ | |
656 | int tmp; | |
657 | char line[80]; | |
658 | char *line_ptr = line; | |
659 | char *command; | |
660 | char *tmpstring; | |
661 | int state = 1; | |
662 | ||
663 | if (have_time_settings()) | |
664 | clock_on = 1; | |
665 | ||
666 | while (state == 1) { | |
667 | state = 0; | |
668 | ||
669 | /* No score is estimated yet. */ | |
670 | current_score_estimate = NO_SCORE; | |
671 | ||
672 | /* Allow resignation at interface level (the engine may still be not | |
673 | * allowed to resign. | |
674 | */ | |
675 | resignation_allowed = 1; | |
676 | ||
677 | printf("\nBeginning ASCII mode game.\n\n"); | |
678 | gameinfo_print(gameinfo); | |
679 | ||
680 | /* Does the computer play first? If so, make a move. */ | |
681 | if (gameinfo->computer_player == gameinfo->to_move) | |
682 | state = computer_move(gameinfo, &passes); | |
683 | ||
684 | /* main ASCII Play loop */ | |
685 | while (state == 0) { | |
686 | /* Display game board. */ | |
687 | if (opt_showboard) | |
688 | ascii_showboard(); | |
689 | ||
690 | #if !READLINE | |
691 | /* Print the prompt */ | |
692 | mprintf("%s(%d): ", color_to_string(gameinfo->to_move), movenum + 1); | |
693 | ||
694 | /* Read a line of input. */ | |
695 | line_ptr = line; | |
696 | if (!fgets(line, 80, stdin)) | |
697 | return; | |
698 | #else | |
699 | snprintf(line, 79, "%s(%d): ", | |
700 | color_to_string(gameinfo->to_move), movenum + 1); | |
701 | if (!(line_ptr = readline(line))) | |
702 | return; | |
703 | ||
704 | add_history(line_ptr); | |
705 | #endif | |
706 | ||
707 | while (state == 0 | |
708 | && (command = strtok(line_ptr, ";"), line_ptr = 0, command)) { | |
709 | /* Get the command or move. */ | |
710 | switch (get_command(command)) { | |
711 | case RESIGN: | |
712 | state = ascii_endgame(gameinfo, 1); | |
713 | break; | |
714 | ||
715 | case END: | |
716 | case EXIT: | |
717 | case QUIT: | |
718 | return; | |
719 | ||
720 | case HELP: | |
721 | show_commands(); | |
722 | break; | |
723 | ||
724 | case CMD_HELPDEBUG: | |
725 | printf(DEBUG_COMMANDS); | |
726 | break; | |
727 | ||
728 | case SHOWBOARD: | |
729 | opt_showboard = !opt_showboard; | |
730 | break; | |
731 | ||
732 | case INFO: | |
733 | printf("\n"); | |
734 | gameinfo_print(gameinfo); | |
735 | break; | |
736 | ||
737 | case SETBOARDSIZE: | |
738 | if (sgf_initialized) { | |
739 | printf("Boardsize cannot be changed after record is started!\n"); | |
740 | break; | |
741 | } | |
742 | command += 10; | |
743 | if (sscanf(command, "%d", &num) != 1) { | |
744 | printf("\nInvalid command syntax!\n"); | |
745 | break; | |
746 | } | |
747 | if (!check_boardsize(num, stdout)) | |
748 | break; | |
749 | /* Init board. */ | |
750 | board_size = num; | |
751 | clear_board(); | |
752 | /* In case max handicap changes on smaller board. */ | |
753 | gameinfo->handicap = place_fixed_handicap(gameinfo->handicap); | |
754 | sgfOverwritePropertyInt(sgftree.root, "SZ", board_size); | |
755 | sgfOverwritePropertyInt(sgftree.root, "HA", gameinfo->handicap); | |
756 | break; | |
757 | ||
758 | case SETHANDICAP: | |
759 | if (sgf_initialized) { | |
760 | printf("Handicap cannot be changed after game is started!\n"); | |
761 | break; | |
762 | } | |
763 | command += 9; | |
764 | if (sscanf(command, "%d", &num) != 1) { | |
765 | printf("\nInvalid command syntax!\n"); | |
766 | break; | |
767 | } | |
768 | if (num < 0 || num > MAX_HANDICAP) { | |
769 | printf("\nInvalid handicap: %d\n", num); | |
770 | break; | |
771 | } | |
772 | /* Init board. */ | |
773 | clear_board(); | |
774 | /* Place stones on board but don't record sgf | |
775 | * in case we change more info. */ | |
776 | gameinfo->handicap = place_fixed_handicap(num); | |
777 | printf("\nSet handicap to %d\n", gameinfo->handicap); | |
778 | gameinfo->to_move = (gameinfo->handicap ? WHITE : BLACK); | |
779 | break; | |
780 | ||
781 | case FREEHANDICAP: | |
782 | if (sgf_initialized) { | |
783 | printf("Handicap cannot be changed after game is started!\n"); | |
784 | break; | |
785 | } | |
786 | while (*command && *command != ' ') | |
787 | command++; | |
788 | ascii_free_handicap(gameinfo, command); | |
789 | break; | |
790 | ||
791 | case SETKOMI: | |
792 | if (sgf_initialized) { | |
793 | printf("Komi cannot be modified after game record is started!\n"); | |
794 | break; | |
795 | } | |
796 | command += 5; | |
797 | if (sscanf(command, "%f", &fnum) != 1) { | |
798 | printf("\nInvalid command syntax!\n"); | |
799 | break; | |
800 | } | |
801 | komi = fnum; | |
802 | printf("\nSet Komi to %.1f\n", komi); | |
803 | break; | |
804 | ||
805 | case SETDEPTH: | |
806 | command += 6; | |
807 | if (sscanf(command, "%d", &num) != 1) { | |
808 | printf("\nInvalid command syntax!\n"); | |
809 | break; | |
810 | } | |
811 | mandated_depth = num; | |
812 | printf("\nSet depth to %d\n", mandated_depth); | |
813 | break; | |
814 | ||
815 | case SETLEVEL: | |
816 | command += 6; | |
817 | if (sscanf(command, "%d", &num) != 1) { | |
818 | printf("\nInvalid command syntax!\n"); | |
819 | break; | |
820 | } | |
821 | set_level(num); | |
822 | printf("\nSet level to %d\n", num); | |
823 | break; | |
824 | ||
825 | case DISPLAY: | |
826 | if (!opt_showboard) | |
827 | ascii_showboard(); | |
828 | break; | |
829 | ||
830 | case FORCE: | |
831 | command += 6; /* skip the force part... */ | |
832 | switch (get_command(command)) { | |
833 | case MOVE: | |
834 | state = do_move(gameinfo, command, &passes, 1); | |
835 | break; | |
836 | case PASS: | |
837 | state = do_pass(gameinfo, &passes, 1); | |
838 | break; | |
839 | default: | |
840 | printf("Illegal forced move: %s %d\n", command, | |
841 | get_command(command)); | |
842 | break; | |
843 | } | |
844 | break; | |
845 | ||
846 | case MOVE: | |
847 | state = do_move(gameinfo, command, &passes, 0); | |
848 | break; | |
849 | ||
850 | case PASS: | |
851 | state = do_pass(gameinfo, &passes, 0); | |
852 | break; | |
853 | ||
854 | case PLAY: | |
855 | command += 5; | |
856 | if (sscanf(command, "%d", &num) != 1) { | |
857 | printf("\nInvalid command syntax!\n"); | |
858 | break; | |
859 | } | |
860 | if (num >= 0) | |
861 | for (m = 0; m < num; m++) { | |
862 | gameinfo->computer_player | |
863 | = OTHER_COLOR(gameinfo->computer_player); | |
864 | state = computer_move(gameinfo, &passes); | |
865 | if (state) | |
866 | break; | |
867 | if (passes >= 2) | |
868 | break; | |
869 | } | |
870 | else { | |
871 | printf("\nInvalid number of moves specified: %d\n", num); | |
872 | break; | |
873 | } | |
874 | break; | |
875 | ||
876 | case PLAYBLACK: | |
877 | if (gameinfo->computer_player == WHITE) | |
878 | gameinfo->computer_player = BLACK; | |
879 | if (gameinfo->computer_player == gameinfo->to_move) | |
880 | state = computer_move(gameinfo, &passes); | |
881 | break; | |
882 | ||
883 | case PLAYWHITE: | |
884 | if (gameinfo->computer_player == BLACK) | |
885 | gameinfo->computer_player = WHITE; | |
886 | if (gameinfo->computer_player == gameinfo->to_move) | |
887 | state = computer_move(gameinfo, &passes); | |
888 | break; | |
889 | ||
890 | case SWITCH: | |
891 | gameinfo->computer_player = OTHER_COLOR(gameinfo->computer_player); | |
892 | state = computer_move(gameinfo, &passes); | |
893 | break; | |
894 | ||
895 | case UNDO: | |
896 | case CMD_BACK: | |
897 | if (undo_move(1)) { | |
898 | sgftreeAddComment(&sgftree, "undone"); | |
899 | sgftreeBack(&sgftree); | |
900 | gameinfo->to_move = OTHER_COLOR(gameinfo->to_move); | |
901 | } | |
902 | else | |
903 | printf("\nCan't undo.\n"); | |
904 | break; | |
905 | ||
906 | case CMD_FORWARD: | |
907 | if (sgftreeForward(&sgftree)) | |
908 | gameinfo->to_move = gnugo_play_sgfnode(sgftree.lastnode, | |
909 | gameinfo->to_move); | |
910 | else | |
911 | printf("\nEnd of game tree.\n"); | |
912 | break; | |
913 | ||
914 | case CMD_LAST: | |
915 | while (sgftreeForward(&sgftree)) | |
916 | gameinfo->to_move = gnugo_play_sgfnode(sgftree.lastnode, | |
917 | gameinfo->to_move); | |
918 | break; | |
919 | ||
920 | case COMMENT: | |
921 | printf("\nEnter comment. Press ENTER when ready.\n"); | |
922 | fgets(line, 80, stdin); | |
923 | sgftreeAddComment(&sgftree, line); | |
924 | break; | |
925 | ||
926 | case SCORE: | |
927 | showscore = !showscore; | |
928 | if (!showscore) | |
929 | current_score_estimate = NO_SCORE; | |
930 | break; | |
931 | ||
932 | case CMD_DEAD: | |
933 | silent_examine_position(FULL_EXAMINE_DRAGONS); | |
934 | showdead = !showdead; | |
935 | break; | |
936 | ||
937 | case CMD_CAPTURE: | |
938 | strtok(command, " "); | |
939 | showcapture(strtok(NULL, " ")); | |
940 | break; | |
941 | ||
942 | case CMD_DEFEND: | |
943 | strtok(command, " "); | |
944 | showdefense(strtok(NULL, " ")); | |
945 | break; | |
946 | ||
947 | case CMD_SHOWMOYO: | |
948 | tmp = printmoyo; | |
949 | printmoyo = PRINTMOYO_MOYO; | |
950 | silent_examine_position(EXAMINE_DRAGONS); | |
951 | printmoyo = tmp; | |
952 | break; | |
953 | ||
954 | case CMD_SHOWTERRI: | |
955 | tmp = printmoyo; | |
956 | printmoyo = PRINTMOYO_TERRITORY; | |
957 | silent_examine_position(EXAMINE_DRAGONS); | |
958 | printmoyo = tmp; | |
959 | break; | |
960 | ||
961 | case CMD_SHOWAREA: | |
962 | tmp = printmoyo; | |
963 | printmoyo = PRINTMOYO_AREA; | |
964 | silent_examine_position(EXAMINE_DRAGONS); | |
965 | printmoyo = tmp; | |
966 | break; | |
967 | ||
968 | case CMD_SHOWDRAGONS: | |
969 | silent_examine_position(EXAMINE_DRAGONS); | |
970 | showboard(1); | |
971 | break; | |
972 | ||
973 | case CMD_GOTO: | |
974 | strtok(command, " "); | |
975 | ascii_goto(gameinfo, strtok(NULL, " ")); | |
976 | break; | |
977 | ||
978 | case CMD_SAVE: | |
979 | strtok(command, " "); | |
980 | tmpstring = strtok(NULL, " "); | |
981 | if (tmpstring) { | |
982 | /* discard newline */ | |
983 | tmpstring[strlen(tmpstring) - 1] = 0; | |
984 | /* make sure we are saving proper handicap */ | |
985 | init_sgf(gameinfo); | |
986 | writesgf(sgftree.root, tmpstring); | |
987 | printf("You may resume the game"); | |
988 | printf(" with -l %s --mode ascii\n", tmpstring); | |
989 | printf("or load %s\n", tmpstring); | |
990 | } | |
991 | else | |
992 | printf("Please specify filename\n"); | |
993 | break; | |
994 | ||
995 | case CMD_LOAD: | |
996 | strtok(command, " "); | |
997 | tmpstring = strtok(NULL, " "); | |
998 | if (tmpstring) { | |
999 | /* discard newline */ | |
1000 | tmpstring[strlen(tmpstring) - 1] = 0; | |
1001 | if (!sgftree_readfile(&sgftree, tmpstring)) { | |
1002 | fprintf(stderr, "Cannot open or parse '%s'\n", tmpstring); | |
1003 | break; | |
1004 | } | |
1005 | /* to avoid changing handicap etc. */ | |
1006 | if (gameinfo_play_sgftree(gameinfo, &sgftree, NULL) == EMPTY) | |
1007 | fprintf(stderr, "Cannot load '%s'\n", tmpstring); | |
1008 | else { | |
1009 | sgf_initialized = 1; | |
1010 | sgfOverwritePropertyInt(sgftree.root, "HA", gameinfo->handicap); | |
1011 | } | |
1012 | } | |
1013 | else | |
1014 | printf("Please specify a filename\n"); | |
1015 | break; | |
1016 | ||
1017 | case CMD_LISTDRAGONS: | |
1018 | silent_examine_position(EXAMINE_DRAGONS); | |
1019 | show_dragons(); | |
1020 | break; | |
1021 | ||
1022 | default: | |
1023 | printf("\nInvalid command: %s", command); | |
1024 | break; | |
1025 | } | |
1026 | ||
1027 | if (passes >= 2) | |
1028 | state = ascii_endgame(gameinfo, 0); | |
1029 | } | |
1030 | #if READLINE | |
1031 | free(line_ptr); | |
1032 | #endif | |
1033 | ||
1034 | } | |
1035 | ||
1036 | sgffile_output(&sgftree); | |
1037 | passes = 0; | |
1038 | ||
1039 | /* Play a different game next time. */ | |
1040 | update_random_seed(); | |
1041 | ||
1042 | /* Free the sgf tree and prepare for a new game. */ | |
1043 | sgfFreeNode(sgftree.root); | |
1044 | sgftree_clear(&sgftree); | |
1045 | sgftreeCreateHeaderNode(&sgftree, board_size, komi, gameinfo->handicap); | |
1046 | sgf_initialized = 0; | |
1047 | ||
1048 | gameinfo_clear(gameinfo); | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | ||
1053 | /* Communicates with user after a game has ended. */ | |
1054 | static int | |
1055 | ascii_endgame(Gameinfo *gameinfo, int reason) | |
1056 | { | |
1057 | char line[80]; | |
1058 | char *line_ptr; | |
1059 | char *command; | |
1060 | char *tmpstring; | |
1061 | int state = 0; | |
1062 | ||
1063 | if (reason == 0) { /* Two passes, game is over. */ | |
1064 | who_wins(gameinfo->computer_player, stdout); | |
1065 | printf("\nIf you disagree, we may count the game together.\n"); | |
1066 | ||
1067 | sgftreeWriteResult(&sgftree, (white_score + black_score)/2.0, 1); | |
1068 | } | |
1069 | else { | |
1070 | int color = OTHER_COLOR(gameinfo->to_move); | |
1071 | ||
1072 | if (reason == 1) /* Our opponent has resigned. */ | |
1073 | printf("GNU Go wins by resignation.\n"); | |
1074 | else /* We have resigned. */ | |
1075 | printf("You win by resignation.\n"); | |
1076 | ||
1077 | printf("Result: %c+Resign\n\n", color == WHITE ? 'W' : 'B'); | |
1078 | sgftreeWriteResult(&sgftree, color == WHITE ? 1000.0 : -1000.0, 1); | |
1079 | } | |
1080 | ||
1081 | while (state == 0) { | |
1082 | printf("You may optionally save the game as an SGF file.\n\n"); | |
1083 | printf("Type \"save <filename>\" to save,\n"); | |
1084 | if (reason == 0) | |
1085 | printf(" \"count\" to recount,\n"); | |
1086 | else if (reason == 2) | |
1087 | printf(" \"continue\" to decline resignation and continue the game,\n"); | |
1088 | printf(" \"quit\" to quit\n"); | |
1089 | printf(" or \"game\" to play again\n"); | |
1090 | ||
1091 | line_ptr = line; | |
1092 | if (!fgets(line, 80, stdin)) | |
1093 | break; | |
1094 | ||
1095 | command = strtok(line_ptr, ""); | |
1096 | switch (get_command(command)) { | |
1097 | case CMD_SAVE: | |
1098 | strtok(command, " "); | |
1099 | tmpstring = strtok(NULL, " "); | |
1100 | if (tmpstring) { | |
1101 | /* discard newline */ | |
1102 | tmpstring[strlen(tmpstring) - 1] = 0; | |
1103 | init_sgf(gameinfo); | |
1104 | writesgf(sgftree.root, tmpstring); | |
1105 | } | |
1106 | else | |
1107 | printf("Please specify filename\n"); | |
1108 | break; | |
1109 | ||
1110 | case COUNT: | |
1111 | if (reason == 0) | |
1112 | ascii_count(gameinfo); | |
1113 | break; | |
1114 | ||
1115 | case CONTINUE: | |
1116 | state = -1; | |
1117 | break; | |
1118 | ||
1119 | case NEW: | |
1120 | state = 1; | |
1121 | break; | |
1122 | ||
1123 | case QUIT: | |
1124 | state = 2; | |
1125 | break; | |
1126 | ||
1127 | default: | |
1128 | state = 0; | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | return state; | |
1133 | } | |
1134 | ||
1135 | ||
1136 | /* ascii_count() scores the game. | |
1137 | */ | |
1138 | static void | |
1139 | ascii_count(Gameinfo *gameinfo) | |
1140 | { | |
1141 | char line[12]; | |
1142 | int done = 0; | |
1143 | int i; | |
1144 | int xyzzy = 1; | |
1145 | ||
1146 | printf("\nGame over. Let's count!.\n"); | |
1147 | showdead = 1; | |
1148 | ascii_showboard(); | |
1149 | while (!done) { | |
1150 | printf("Dead stones are marked with small letters (x,o).\n"); | |
1151 | printf("\nIf you don't agree, enter the location of a group\n"); | |
1152 | printf("to toggle its state or \"done\".\n"); | |
1153 | ||
1154 | if (!fgets(line, 12, stdin)) | |
1155 | return; /* EOF or some error */ | |
1156 | ||
1157 | for (i = 0; i < 12; i++) | |
1158 | line[i] = tolower((int) line[i]); | |
1159 | if (!strncmp(line, "done", 4)) | |
1160 | done = 1; | |
1161 | else if (!strncmp(line, "quit", 4)) | |
1162 | return; | |
1163 | else if (!strncmp(line, "xyzzy", 5)) { | |
1164 | if (xyzzy) { | |
1165 | printf("\nYou are in a debris room filled with stuff washed in from the\n"); | |
1166 | printf("surface. A low wide passage with cobbles becomes plugged\n"); | |
1167 | printf("with mud and debris here, but an awkward canyon leads\n"); | |
1168 | printf("upward and west. A note on the wall says:\n"); | |
1169 | printf(" Magic Word \"XYZZY\"\n\n"); | |
1170 | xyzzy = 0; | |
1171 | } | |
1172 | else { | |
1173 | printf("You are inside a building, a well house for a large spring.\n\n"); | |
1174 | xyzzy = 1; | |
1175 | } | |
1176 | } | |
1177 | else if (!strncmp(line, "help", 4)) { | |
1178 | printf("\nIf there are dead stones on the board I will remove them.\n"); | |
1179 | printf("You must tell me where they are. Type the coordinates of a\n"); | |
1180 | printf("dead group, or type \"done\"\n"); | |
1181 | } | |
1182 | else if (!strncmp(line, "undo", 4)) { | |
1183 | printf("UNDO not allowed anymore. The status of the stones now\n"); | |
1184 | printf("toggles after entering the location of it.\n"); | |
1185 | ascii_showboard(); | |
1186 | } | |
1187 | else { | |
1188 | int pos = string_to_location(board_size, line); | |
1189 | if (pos == NO_MOVE || board[pos] == EMPTY) | |
1190 | printf("\ninvalid!\n"); | |
1191 | else { | |
1192 | enum dragon_status status = dragon_status(pos); | |
1193 | status = (status == DEAD) ? ALIVE : DEAD; | |
1194 | change_dragon_status(pos, status); | |
1195 | ascii_showboard(); | |
1196 | } | |
1197 | } | |
1198 | } | |
1199 | who_wins(gameinfo->computer_player, stdout); | |
1200 | } | |
1201 | ||
1202 | ||
1203 | static void | |
1204 | showcapture(char *line) | |
1205 | { | |
1206 | int str; | |
1207 | int move; | |
1208 | ||
1209 | gg_assert(line); | |
1210 | str = string_to_location(board_size, line); | |
1211 | if (str == NO_MOVE || board[str] == EMPTY) { | |
1212 | printf("\ninvalid point!\n"); | |
1213 | return; | |
1214 | } | |
1215 | ||
1216 | if (attack(str, &move)) | |
1217 | mprintf("\nSuccessful attack of %1m at %1m\n", str, move); | |
1218 | else | |
1219 | mprintf("\n%1m cannot be attacked\n", str); | |
1220 | } | |
1221 | ||
1222 | ||
1223 | static void | |
1224 | showdefense(char *line) | |
1225 | { | |
1226 | int str; | |
1227 | int move; | |
1228 | ||
1229 | gg_assert(line); | |
1230 | str = string_to_location(board_size, line); | |
1231 | if (str == NO_MOVE || board[str] == EMPTY) { | |
1232 | printf("\ninvalid point!\n"); | |
1233 | return; | |
1234 | } | |
1235 | ||
1236 | if (attack(str, NULL)) { | |
1237 | if (find_defense(str, &move)) | |
1238 | mprintf("\nSuccessful defense of %1m at %1m\n", str, move); | |
1239 | else | |
1240 | mprintf("\n%1m cannot be defended\n", str); | |
1241 | } | |
1242 | else | |
1243 | mprintf("\nThere is no need to defend %1m\n", str); | |
1244 | } | |
1245 | ||
1246 | ||
1247 | static void | |
1248 | ascii_goto(Gameinfo *gameinfo, char *line) | |
1249 | { | |
1250 | const char *movenumber = line; | |
1251 | ||
1252 | if (!line) | |
1253 | return; | |
1254 | ||
1255 | if (!strncmp(line, "last", 4)) | |
1256 | movenumber = "9999"; | |
1257 | else if (!strncmp(line, "first", 4)) | |
1258 | movenumber = "1"; | |
1259 | ||
1260 | printf("goto %s\n", movenumber); | |
1261 | gameinfo_play_sgftree(gameinfo, &sgftree, movenumber); | |
1262 | } | |
1263 | ||
1264 | ||
1265 | static void | |
1266 | ascii_free_handicap(Gameinfo *gameinfo, char *handicap_string) | |
1267 | { | |
1268 | int handi; | |
1269 | int i; | |
1270 | char line[80]; | |
1271 | int stones[MAX_BOARD*MAX_BOARD]; | |
1272 | ||
1273 | if (sscanf(handicap_string, "%d", &handi) == 1) { | |
1274 | /* GNU Go is to place handicap */ | |
1275 | if (handi < 0 || handi == 1) { | |
1276 | printf("\nInvalid command syntax!\n"); | |
1277 | return; | |
1278 | } | |
1279 | ||
1280 | clear_board(); | |
1281 | handi = place_free_handicap(handi); | |
1282 | printf("\nPlaced %d stones of free handicap.\n", handi); | |
1283 | } | |
1284 | else { /* User is to place handicap */ | |
1285 | clear_board(); | |
1286 | handi = 0; | |
1287 | ||
1288 | while (1) { | |
1289 | ascii_showboard(); | |
1290 | printf("\nType in coordinates of next handicap stone, or one of the following commands:\n"); | |
1291 | printf(" undo take back the last stone placed\n"); | |
1292 | printf(" clear remove all the stones placed so far\n"); | |
1293 | printf(" done finish placing handicap\n\n"); | |
1294 | printf("You have placed %d handicap stone(s) so far.\n\n", handi); | |
1295 | ||
1296 | if (!fgets(line, 80, stdin)) | |
1297 | return; /* EOF or some error */ | |
1298 | for (i = 0; i < 80; i++) | |
1299 | line[i] = tolower((int) line[i]); | |
1300 | ||
1301 | if (!strncmp(line, "undo", 4)) { | |
1302 | if (!handi) | |
1303 | printf("\nNothing to undo.\n"); | |
1304 | else { | |
1305 | remove_stone(stones[--handi]); | |
1306 | gprintf("\nRemoved the stone at %m.\n", I(stones[handi]), | |
1307 | J(stones[handi])); | |
1308 | } | |
1309 | } | |
1310 | else if (!strncmp(line, "clear", 5)) { | |
1311 | clear_board(); | |
1312 | handi = 0; | |
1313 | } | |
1314 | else if (!strncmp(line, "done", 4)) { | |
1315 | if (handi == 1) /* Don't bother with checks later */ | |
1316 | printf("\nHandicap cannot be one stone. Either add " | |
1317 | "some more, or delete the only stone.\n"); | |
1318 | else | |
1319 | break; | |
1320 | } | |
1321 | else { | |
1322 | int pos = string_to_location(board_size, line); | |
1323 | if (pos != NO_MOVE) { | |
1324 | if (board[pos] != EMPTY) | |
1325 | printf("\nThere's already a stone there.\n"); | |
1326 | else { | |
1327 | add_stone(pos, BLACK); | |
1328 | stones[handi++] = pos; | |
1329 | } | |
1330 | } | |
1331 | else | |
1332 | printf("\nInvalid command: %s", line); | |
1333 | } | |
1334 | } | |
1335 | } | |
1336 | gameinfo->handicap = handi; | |
1337 | gameinfo->to_move = (handi ? WHITE : BLACK); | |
1338 | } | |
1339 | ||
1340 | ||
1341 | /* | |
1342 | * Local Variables: | |
1343 | * tab-width: 8 | |
1344 | * c-basic-offset: 2 | |
1345 | * End: | |
1346 | */ |