1 1.73 rillig /* $NetBSD: main.c,v 1.73 2022/05/29 21:02:37 rillig Exp $ */ 2 1.3 cgd 3 1.1 tls /* 4 1.1 tls * Copyright (c) 1994 5 1.1 tls * The Regents of the University of California. All rights reserved. 6 1.1 tls * 7 1.1 tls * This code is derived from software contributed to Berkeley by 8 1.1 tls * Ralph Campbell. 9 1.1 tls * 10 1.1 tls * Redistribution and use in source and binary forms, with or without 11 1.1 tls * modification, are permitted provided that the following conditions 12 1.1 tls * are met: 13 1.1 tls * 1. Redistributions of source code must retain the above copyright 14 1.1 tls * notice, this list of conditions and the following disclaimer. 15 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 tls * notice, this list of conditions and the following disclaimer in the 17 1.1 tls * documentation and/or other materials provided with the distribution. 18 1.11 agc * 3. Neither the name of the University nor the names of its contributors 19 1.1 tls * may be used to endorse or promote products derived from this software 20 1.1 tls * without specific prior written permission. 21 1.1 tls * 22 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 1.1 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 1.1 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 tls * SUCH DAMAGE. 33 1.1 tls */ 34 1.1 tls 35 1.4 lukem #include <sys/cdefs.h> 36 1.14 lukem __COPYRIGHT("@(#) Copyright (c) 1994\ 37 1.14 lukem The Regents of the University of California. All rights reserved."); 38 1.41 rillig /* @(#)main.c 8.4 (Berkeley) 5/4/95 */ 39 1.73 rillig __RCSID("$NetBSD: main.c,v 1.73 2022/05/29 21:02:37 rillig Exp $"); 40 1.1 tls 41 1.45 rillig #include <sys/stat.h> 42 1.1 tls #include <curses.h> 43 1.1 tls #include <err.h> 44 1.21 dholland #include <limits.h> 45 1.1 tls #include <signal.h> 46 1.16 dholland #include <stdarg.h> 47 1.1 tls #include <stdlib.h> 48 1.1 tls #include <string.h> 49 1.5 perry #include <time.h> 50 1.1 tls #include <unistd.h> 51 1.1 tls 52 1.1 tls #include "gomoku.h" 53 1.1 tls 54 1.47 rillig enum input_source { 55 1.47 rillig USER, /* get input from standard input */ 56 1.47 rillig PROGRAM, /* get input from program */ 57 1.47 rillig INPUTF /* get input from a file */ 58 1.47 rillig }; 59 1.1 tls 60 1.57 rillig enum testing_mode { 61 1.57 rillig NORMAL_PLAY, 62 1.57 rillig USER_VS_USER, 63 1.57 rillig PROGRAM_VS_PROGRAM 64 1.57 rillig }; 65 1.57 rillig 66 1.37 rillig bool interactive = true; /* true if interactive */ 67 1.33 rillig int debug; /* > 0 if debugging */ 68 1.57 rillig static enum testing_mode test = NORMAL_PLAY; 69 1.20 dholland static char *prog; /* name of program */ 70 1.23 dholland static char user[LOGIN_NAME_MAX]; /* name of player */ 71 1.20 dholland static FILE *debugfp; /* file for debug output */ 72 1.20 dholland static FILE *inputfp; /* file for debug input */ 73 1.1 tls 74 1.6 jsm const char pdir[4] = "-\\|/"; 75 1.1 tls 76 1.1 tls struct spotstr board[BAREA]; /* info for board */ 77 1.1 tls struct combostr frames[FAREA]; /* storage for all frames */ 78 1.1 tls struct combostr *sortframes[2]; /* sorted list of non-empty frames */ 79 1.60 rillig u_char overlap[FAREA * FAREA]; /* non-zero if frame [a][b] overlap; 80 1.60 rillig * see init_overlap */ 81 1.68 rillig spot_index intersect[FAREA * FAREA]; /* frame [a][b] intersection */ 82 1.61 rillig struct game game; 83 1.56 rillig const char *plyr[2] = { "???", "???" }; /* who's who */ 84 1.1 tls 85 1.71 rillig static spot_index readinput(FILE *); 86 1.20 dholland static void misclog(const char *, ...) __printflike(1, 2); 87 1.20 dholland static void quit(void) __dead; 88 1.35 rillig #if !defined(DEBUG) 89 1.35 rillig static void quitsig(int) __dead; 90 1.35 rillig #endif 91 1.1 tls 92 1.45 rillig static void 93 1.45 rillig warn_if_exists(const char *fname) 94 1.45 rillig { 95 1.45 rillig struct stat st; 96 1.45 rillig 97 1.45 rillig if (lstat(fname, &st) == 0) { 98 1.45 rillig int x, y; 99 1.45 rillig getyx(stdscr, y, x); 100 1.45 rillig addstr(" (already exists)"); 101 1.45 rillig move(y, x); 102 1.45 rillig } else 103 1.45 rillig clrtoeol(); 104 1.45 rillig } 105 1.45 rillig 106 1.50 rillig static void 107 1.50 rillig save_game(void) 108 1.50 rillig { 109 1.50 rillig char fname[PATH_MAX]; 110 1.50 rillig FILE *fp; 111 1.50 rillig 112 1.50 rillig ask("Save file name? "); 113 1.50 rillig (void)get_line(fname, sizeof(fname), warn_if_exists); 114 1.50 rillig if ((fp = fopen(fname, "w")) == NULL) { 115 1.50 rillig misclog("cannot create save file"); 116 1.50 rillig return; 117 1.50 rillig } 118 1.70 rillig for (unsigned int m = 0; m < game.nmoves; m++) 119 1.70 rillig fprintf(fp, "%s\n", stoc(game.moves[m])); 120 1.50 rillig fclose(fp); 121 1.50 rillig } 122 1.50 rillig 123 1.51 rillig static void 124 1.51 rillig parse_args(int argc, char **argv) 125 1.1 tls { 126 1.51 rillig int ch; 127 1.4 lukem 128 1.1 tls prog = strrchr(argv[0], '/'); 129 1.46 rillig prog = prog != NULL ? prog + 1 : argv[0]; 130 1.1 tls 131 1.4 lukem while ((ch = getopt(argc, argv, "bcdD:u")) != -1) { 132 1.1 tls switch (ch) { 133 1.1 tls case 'b': /* background */ 134 1.33 rillig interactive = false; 135 1.1 tls break; 136 1.57 rillig case 'c': 137 1.57 rillig test = PROGRAM_VS_PROGRAM; 138 1.51 rillig break; 139 1.46 rillig case 'd': 140 1.1 tls debug++; 141 1.1 tls break; 142 1.1 tls case 'D': /* log debug output to file */ 143 1.1 tls if ((debugfp = fopen(optarg, "w")) == NULL) 144 1.1 tls err(1, "%s", optarg); 145 1.1 tls break; 146 1.57 rillig case 'u': 147 1.57 rillig test = USER_VS_USER; 148 1.1 tls break; 149 1.38 rillig default: 150 1.46 rillig usage: 151 1.38 rillig fprintf(stderr, "usage: %s [-bcdu] [-Dfile] [file]\n", 152 1.38 rillig getprogname()); 153 1.51 rillig exit(EXIT_FAILURE); 154 1.1 tls } 155 1.1 tls } 156 1.1 tls argc -= optind; 157 1.1 tls argv += optind; 158 1.46 rillig if (argc > 1) 159 1.46 rillig goto usage; 160 1.46 rillig if (argc == 1 && (inputfp = fopen(*argv, "r")) == NULL) 161 1.46 rillig err(1, "%s", *argv); 162 1.51 rillig } 163 1.51 rillig 164 1.52 rillig static void 165 1.73 rillig set_input_sources(enum input_source *input, player_color color) 166 1.52 rillig { 167 1.52 rillig switch (test) { 168 1.57 rillig case NORMAL_PLAY: 169 1.52 rillig input[color] = USER; 170 1.52 rillig input[color != BLACK ? BLACK : WHITE] = PROGRAM; 171 1.52 rillig break; 172 1.57 rillig case USER_VS_USER: 173 1.52 rillig input[BLACK] = USER; 174 1.52 rillig input[WHITE] = USER; 175 1.52 rillig break; 176 1.57 rillig case PROGRAM_VS_PROGRAM: 177 1.52 rillig input[BLACK] = PROGRAM; 178 1.52 rillig input[WHITE] = PROGRAM; 179 1.52 rillig break; 180 1.52 rillig } 181 1.52 rillig } 182 1.52 rillig 183 1.53 rillig static int 184 1.54 rillig ask_user_color(void) 185 1.54 rillig { 186 1.54 rillig int color; 187 1.54 rillig 188 1.54 rillig mvprintw(BSZ + 3, 0, "Black moves first. "); 189 1.54 rillig ask("(B)lack or (W)hite? "); 190 1.54 rillig for (;;) { 191 1.54 rillig int ch = get_key(NULL); 192 1.54 rillig if (ch == 'b' || ch == 'B') { 193 1.54 rillig color = BLACK; 194 1.54 rillig break; 195 1.54 rillig } 196 1.54 rillig if (ch == 'w' || ch == 'W') { 197 1.54 rillig color = WHITE; 198 1.54 rillig break; 199 1.54 rillig } 200 1.54 rillig if (ch == 'q' || ch == 'Q') 201 1.54 rillig quit(); 202 1.54 rillig 203 1.54 rillig beep(); 204 1.54 rillig ask("Please choose (B)lack or (W)hite: "); 205 1.54 rillig } 206 1.54 rillig move(BSZ + 3, 0); 207 1.54 rillig clrtoeol(); 208 1.54 rillig return color; 209 1.54 rillig } 210 1.54 rillig 211 1.54 rillig static int 212 1.56 rillig read_color(void) 213 1.56 rillig { 214 1.56 rillig char buf[128]; 215 1.56 rillig 216 1.56 rillig get_line(buf, sizeof(buf), NULL); 217 1.56 rillig if (strcmp(buf, "black") == 0) 218 1.56 rillig return BLACK; 219 1.56 rillig if (strcmp(buf, "white") == 0) 220 1.56 rillig return WHITE; 221 1.56 rillig panic("Huh? Expected `black' or `white', got `%s'\n", buf); 222 1.56 rillig /* NOTREACHED */ 223 1.56 rillig } 224 1.56 rillig 225 1.67 rillig static spot_index 226 1.53 rillig read_move(void) 227 1.53 rillig { 228 1.53 rillig again: 229 1.53 rillig if (interactive) { 230 1.53 rillig ask("Select move, (S)ave or (Q)uit."); 231 1.67 rillig spot_index s = get_coord(); 232 1.53 rillig if (s == SAVE) { 233 1.53 rillig save_game(); 234 1.53 rillig goto again; 235 1.53 rillig } 236 1.53 rillig if (s != RESIGN && board[s].s_occ != EMPTY) { 237 1.53 rillig beep(); 238 1.53 rillig goto again; 239 1.53 rillig } 240 1.53 rillig return s; 241 1.53 rillig } else { 242 1.53 rillig char buf[128]; 243 1.53 rillig if (!get_line(buf, sizeof(buf), NULL)) 244 1.53 rillig return RESIGN; 245 1.53 rillig if (buf[0] == '\0') 246 1.53 rillig goto again; 247 1.53 rillig return ctos(buf); 248 1.53 rillig } 249 1.53 rillig } 250 1.53 rillig 251 1.55 rillig static void 252 1.73 rillig declare_winner(int outcome, const enum input_source *input, player_color color) 253 1.55 rillig { 254 1.55 rillig 255 1.55 rillig move(BSZ + 3, 0); 256 1.55 rillig switch (outcome) { 257 1.55 rillig case WIN: 258 1.55 rillig if (input[color] == PROGRAM) 259 1.55 rillig addstr("Ha ha, I won"); 260 1.55 rillig else if (input[0] == USER && input[1] == USER) 261 1.55 rillig addstr("Well, you won (and lost)"); 262 1.55 rillig else 263 1.55 rillig addstr("Rats! you won"); 264 1.55 rillig break; 265 1.55 rillig case TIE: 266 1.55 rillig addstr("Wow! It's a tie"); 267 1.55 rillig break; 268 1.55 rillig case ILLEGAL: 269 1.55 rillig addstr("Illegal move"); 270 1.55 rillig break; 271 1.55 rillig } 272 1.55 rillig clrtoeol(); 273 1.55 rillig bdisp(); 274 1.55 rillig } 275 1.55 rillig 276 1.58 rillig struct outcome { 277 1.58 rillig int result; 278 1.73 rillig player_color winner; 279 1.58 rillig }; 280 1.58 rillig 281 1.58 rillig static struct outcome 282 1.58 rillig main_game_loop(enum input_source *input) 283 1.58 rillig { 284 1.71 rillig spot_index curmove = 0; 285 1.71 rillig player_color color = BLACK; 286 1.58 rillig 287 1.58 rillig again: 288 1.58 rillig switch (input[color]) { 289 1.58 rillig case INPUTF: 290 1.58 rillig curmove = readinput(inputfp); 291 1.66 rillig if (curmove != END_OF_INPUT) 292 1.58 rillig break; 293 1.58 rillig set_input_sources(input, color); 294 1.58 rillig plyr[BLACK] = input[BLACK] == USER ? user : prog; 295 1.58 rillig plyr[WHITE] = input[WHITE] == USER ? user : prog; 296 1.58 rillig bdwho(); 297 1.58 rillig refresh(); 298 1.58 rillig goto again; 299 1.58 rillig 300 1.58 rillig case USER: 301 1.58 rillig curmove = read_move(); 302 1.58 rillig break; 303 1.58 rillig 304 1.58 rillig case PROGRAM: 305 1.58 rillig if (interactive) 306 1.58 rillig ask("Thinking..."); 307 1.58 rillig curmove = pickmove(color); 308 1.58 rillig break; 309 1.58 rillig } 310 1.58 rillig 311 1.58 rillig if (interactive && curmove != ILLEGAL) { 312 1.59 rillig misclog("%3u%*s%-6s", 313 1.61 rillig game.nmoves + 1, color == BLACK ? 2 : 9, "", 314 1.61 rillig stoc(curmove)); 315 1.58 rillig } 316 1.58 rillig 317 1.71 rillig int outcome; 318 1.58 rillig if ((outcome = makemove(color, curmove)) != MOVEOK) 319 1.58 rillig return (struct outcome){ outcome, color }; 320 1.58 rillig 321 1.58 rillig if (interactive) 322 1.58 rillig bdisp(); 323 1.58 rillig color = color != BLACK ? BLACK : WHITE; 324 1.58 rillig goto again; 325 1.58 rillig } 326 1.58 rillig 327 1.51 rillig int 328 1.51 rillig main(int argc, char **argv) 329 1.51 rillig { 330 1.51 rillig char *user_name; 331 1.58 rillig int color; 332 1.51 rillig enum input_source input[2]; 333 1.51 rillig 334 1.51 rillig /* Revoke setgid privileges */ 335 1.51 rillig setgid(getgid()); 336 1.51 rillig 337 1.51 rillig setprogname(argv[0]); 338 1.51 rillig 339 1.51 rillig user_name = getlogin(); 340 1.51 rillig strlcpy(user, user_name != NULL ? user_name : "you", sizeof(user)); 341 1.51 rillig 342 1.56 rillig color = BLACK; 343 1.51 rillig 344 1.51 rillig parse_args(argc, argv); 345 1.1 tls 346 1.33 rillig if (debug == 0) 347 1.32 rillig srandom((unsigned int)time(0)); 348 1.1 tls if (interactive) 349 1.1 tls cursinit(); /* initialize curses */ 350 1.1 tls again: 351 1.62 rillig init_board(); /* initialize board contents */ 352 1.1 tls 353 1.1 tls if (interactive) { 354 1.1 tls bdisp_init(); /* initialize display of board */ 355 1.1 tls #ifdef DEBUG 356 1.1 tls signal(SIGINT, whatsup); 357 1.1 tls #else 358 1.4 lukem signal(SIGINT, quitsig); 359 1.1 tls #endif 360 1.1 tls 361 1.57 rillig if (inputfp == NULL && test == NORMAL_PLAY) 362 1.54 rillig color = ask_user_color(); 363 1.1 tls } else { 364 1.56 rillig setbuf(stdout, NULL); 365 1.56 rillig color = read_color(); 366 1.1 tls } 367 1.1 tls 368 1.33 rillig if (inputfp != NULL) { 369 1.1 tls input[BLACK] = INPUTF; 370 1.1 tls input[WHITE] = INPUTF; 371 1.1 tls } else { 372 1.52 rillig set_input_sources(input, color); 373 1.1 tls } 374 1.1 tls if (interactive) { 375 1.23 dholland plyr[BLACK] = input[BLACK] == USER ? user : prog; 376 1.23 dholland plyr[WHITE] = input[WHITE] == USER ? user : prog; 377 1.43 rillig bdwho(); 378 1.43 rillig refresh(); 379 1.1 tls } 380 1.1 tls 381 1.58 rillig struct outcome outcome = main_game_loop(input); 382 1.1 tls 383 1.1 tls if (interactive) { 384 1.58 rillig declare_winner(outcome.result, input, outcome.winner); 385 1.58 rillig if (outcome.result != RESIGN) { 386 1.1 tls replay: 387 1.24 dholland ask("Play again? "); 388 1.51 rillig int ch = get_key("YyNnQqSs"); 389 1.24 dholland if (ch == 'Y' || ch == 'y') 390 1.1 tls goto again; 391 1.64 rillig if (ch == 'S' || ch == 's') { 392 1.50 rillig save_game(); 393 1.1 tls goto replay; 394 1.1 tls } 395 1.1 tls } 396 1.1 tls } 397 1.1 tls quit(); 398 1.1 tls } 399 1.1 tls 400 1.71 rillig static spot_index 401 1.15 dholland readinput(FILE *fp) 402 1.1 tls { 403 1.1 tls int c; 404 1.17 dholland char buf[128]; 405 1.17 dholland size_t pos; 406 1.1 tls 407 1.17 dholland pos = 0; 408 1.17 dholland while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1) 409 1.17 dholland buf[pos++] = c; 410 1.17 dholland buf[pos] = '\0'; 411 1.66 rillig return c == EOF ? END_OF_INPUT : ctos(buf); 412 1.1 tls } 413 1.1 tls 414 1.1 tls #ifdef DEBUG 415 1.72 rillig 416 1.72 rillig static bool 417 1.72 rillig skip_any(const char **pp, const char *s) 418 1.72 rillig { 419 1.72 rillig while (strchr(s, **pp) != NULL) 420 1.72 rillig (*pp)++; 421 1.72 rillig return true; 422 1.72 rillig } 423 1.72 rillig 424 1.72 rillig static bool 425 1.72 rillig parse_char_index(const char **pp, const char *s, unsigned int *out) 426 1.72 rillig { 427 1.72 rillig const char *found = strchr(s, **pp); 428 1.72 rillig if (found != NULL) 429 1.72 rillig *out = (unsigned int)(found - s), (*pp)++; 430 1.72 rillig return found != NULL; 431 1.72 rillig } 432 1.72 rillig 433 1.72 rillig static bool 434 1.72 rillig parse_direction(const char **pp, direction *out) 435 1.72 rillig { 436 1.72 rillig unsigned int u; 437 1.72 rillig if (!parse_char_index(pp, "-\\|/", &u)) 438 1.72 rillig return false; 439 1.72 rillig *out = (direction)u; 440 1.72 rillig return true; 441 1.72 rillig } 442 1.72 rillig 443 1.72 rillig static bool 444 1.72 rillig parse_row(const char **pp, unsigned int *out) 445 1.72 rillig { 446 1.72 rillig if (!('0' <= **pp && **pp <= '9')) 447 1.72 rillig return false; 448 1.72 rillig unsigned int u = *(*pp)++ - '0'; 449 1.72 rillig if ('0' <= **pp && **pp <= '9') 450 1.72 rillig u = 10 * u + *(*pp)++ - '0'; 451 1.72 rillig *out = u; 452 1.72 rillig return 1 <= u && u <= BSZ; 453 1.72 rillig } 454 1.72 rillig 455 1.72 rillig static bool 456 1.72 rillig parse_spot(const char **pp, spot_index *out) 457 1.72 rillig { 458 1.72 rillig unsigned row, col; 459 1.72 rillig if (!parse_char_index(pp, "abcdefghjklmnopqrst", &col) && 460 1.72 rillig !parse_char_index(pp, "ABCDEFGHJKLMNOPQRST", &col)) 461 1.72 rillig return false; 462 1.72 rillig if (!parse_row(pp, &row)) 463 1.72 rillig return false; 464 1.72 rillig *out = PT(col + 1, row); 465 1.72 rillig return true; 466 1.72 rillig } 467 1.72 rillig 468 1.1 tls /* 469 1.65 rillig * Handle strange situations and ^C. 470 1.1 tls */ 471 1.35 rillig /* ARGSUSED */ 472 1.1 tls void 473 1.65 rillig whatsup(int signum __unused) 474 1.1 tls { 475 1.72 rillig unsigned int n; 476 1.71 rillig player_color color; 477 1.71 rillig spot_index s, s1, s2; 478 1.72 rillig direction r1, r2; 479 1.1 tls struct spotstr *sp; 480 1.1 tls FILE *fp; 481 1.72 rillig const char *str; 482 1.1 tls struct elist *ep; 483 1.1 tls struct combostr *cbp; 484 1.18 dholland char input[128]; 485 1.18 dholland char tmp[128]; 486 1.1 tls 487 1.1 tls if (!interactive) 488 1.1 tls quit(); 489 1.1 tls top: 490 1.24 dholland ask("debug command: "); 491 1.45 rillig if (!get_line(input, sizeof(input), NULL)) 492 1.1 tls quit(); 493 1.18 dholland switch (*input) { 494 1.1 tls case '\0': 495 1.1 tls goto top; 496 1.1 tls case 'q': /* conservative quit */ 497 1.1 tls quit(); 498 1.35 rillig /* NOTREACHED */ 499 1.1 tls case 'd': /* set debug level */ 500 1.18 dholland debug = input[1] - '0'; 501 1.16 dholland debuglog("Debug set to %d", debug); 502 1.44 rillig goto top; 503 1.1 tls case 'c': 504 1.72 rillig ask(""); 505 1.72 rillig return; 506 1.1 tls case 'b': /* back up a move */ 507 1.61 rillig if (game.nmoves > 0) { 508 1.61 rillig game.nmoves--; 509 1.61 rillig board[game.moves[game.nmoves]].s_occ = EMPTY; 510 1.1 tls bdisp(); 511 1.1 tls } 512 1.1 tls goto top; 513 1.1 tls case 's': /* suggest a move */ 514 1.70 rillig color = input[1] == 'b' ? BLACK : WHITE; 515 1.70 rillig debuglog("suggest %c %s", color == BLACK ? 'B' : 'W', 516 1.70 rillig stoc(pickmove(color))); 517 1.1 tls goto top; 518 1.1 tls case 'f': /* go forward a move */ 519 1.61 rillig board[game.moves[game.nmoves]].s_occ = 520 1.61 rillig game.nmoves % 2 == 0 ? BLACK : WHITE; 521 1.61 rillig game.nmoves++; 522 1.1 tls bdisp(); 523 1.1 tls goto top; 524 1.1 tls case 'l': /* print move history */ 525 1.18 dholland if (input[1] == '\0') { 526 1.61 rillig for (unsigned int m = 0; m < game.nmoves; m++) 527 1.61 rillig debuglog("%s", stoc(game.moves[m])); 528 1.1 tls goto top; 529 1.1 tls } 530 1.18 dholland if ((fp = fopen(input + 1, "w")) == NULL) 531 1.1 tls goto top; 532 1.61 rillig for (unsigned int m = 0; m < game.nmoves; m++) { 533 1.61 rillig fprintf(fp, "%s", stoc(game.moves[m])); 534 1.61 rillig if (++m < game.nmoves) 535 1.61 rillig fprintf(fp, " %s\n", stoc(game.moves[m])); 536 1.1 tls else 537 1.1 tls fputc('\n', fp); 538 1.1 tls } 539 1.1 tls bdump(fp); 540 1.1 tls fclose(fp); 541 1.1 tls goto top; 542 1.1 tls case 'o': 543 1.72 rillig str = input + 1; 544 1.72 rillig if (skip_any(&str, " ") && 545 1.72 rillig parse_spot(&str, &s1) && 546 1.72 rillig parse_direction(&str, &r1) && 547 1.72 rillig skip_any(&str, ", ") && 548 1.72 rillig parse_spot(&str, &s2) && 549 1.72 rillig parse_direction(&str, &r2) && 550 1.72 rillig *str == '\0') { 551 1.72 rillig n = board[s1].s_frame[r1] * FAREA 552 1.72 rillig + board[s2].s_frame[r2]; 553 1.72 rillig debuglog("overlap %s%c,%s%c = %02x", 554 1.72 rillig stoc(s1), pdir[r1], stoc(s2), pdir[r2], 555 1.72 rillig overlap[n]); 556 1.72 rillig } else 557 1.72 rillig debuglog("usage: o <spot><dir> <spot><dir>"); 558 1.1 tls goto top; 559 1.1 tls case 'p': 560 1.70 rillig sp = &board[s = ctos(input + 1)]; 561 1.70 rillig debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(s), 562 1.1 tls sp->s_combo[BLACK].s, sp->s_level[BLACK], 563 1.1 tls sp->s_nforce[BLACK], 564 1.1 tls sp->s_combo[WHITE].s, sp->s_level[WHITE], 565 1.18 dholland sp->s_nforce[WHITE], sp->s_wval, sp->s_flags); 566 1.70 rillig debuglog("FB %s %x %x %x %x", stoc(s), 567 1.1 tls sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s, 568 1.1 tls sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s); 569 1.70 rillig debuglog("FW %s %x %x %x %x", stoc(s), 570 1.1 tls sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s, 571 1.1 tls sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s); 572 1.1 tls goto top; 573 1.72 rillig case 'e': /* e [0-9] spot */ 574 1.18 dholland str = input + 1; 575 1.1 tls if (*str >= '0' && *str <= '9') 576 1.1 tls n = *str++ - '0'; 577 1.1 tls else 578 1.1 tls n = 0; 579 1.70 rillig sp = &board[ctos(str)]; 580 1.35 rillig for (ep = sp->s_empty; ep != NULL; ep = ep->e_next) { 581 1.1 tls cbp = ep->e_combo; 582 1.35 rillig if (n != 0) { 583 1.1 tls if (cbp->c_nframes > n) 584 1.1 tls continue; 585 1.1 tls if (cbp->c_nframes != n) 586 1.1 tls break; 587 1.1 tls } 588 1.18 dholland printcombo(cbp, tmp, sizeof(tmp)); 589 1.18 dholland debuglog("%s", tmp); 590 1.1 tls } 591 1.1 tls goto top; 592 1.1 tls default: 593 1.16 dholland debuglog("Options are:"); 594 1.16 dholland debuglog("q - quit"); 595 1.16 dholland debuglog("c - continue"); 596 1.16 dholland debuglog("d# - set debug level to #"); 597 1.16 dholland debuglog("p# - print values at #"); 598 1.1 tls goto top; 599 1.1 tls } 600 1.1 tls } 601 1.1 tls #endif /* DEBUG */ 602 1.1 tls 603 1.1 tls /* 604 1.1 tls * Display debug info. 605 1.1 tls */ 606 1.4 lukem void 607 1.16 dholland debuglog(const char *fmt, ...) 608 1.1 tls { 609 1.16 dholland va_list ap; 610 1.16 dholland char buf[128]; 611 1.16 dholland 612 1.16 dholland va_start(ap, fmt); 613 1.16 dholland vsnprintf(buf, sizeof(buf), fmt, ap); 614 1.16 dholland va_end(ap); 615 1.1 tls 616 1.33 rillig if (debugfp != NULL) 617 1.16 dholland fprintf(debugfp, "%s\n", buf); 618 1.1 tls if (interactive) 619 1.16 dholland dislog(buf); 620 1.1 tls else 621 1.16 dholland fprintf(stderr, "%s\n", buf); 622 1.1 tls } 623 1.1 tls 624 1.20 dholland static void 625 1.16 dholland misclog(const char *fmt, ...) 626 1.1 tls { 627 1.16 dholland va_list ap; 628 1.16 dholland char buf[128]; 629 1.16 dholland 630 1.16 dholland va_start(ap, fmt); 631 1.16 dholland vsnprintf(buf, sizeof(buf), fmt, ap); 632 1.16 dholland va_end(ap); 633 1.1 tls 634 1.33 rillig if (debugfp != NULL) 635 1.16 dholland fprintf(debugfp, "%s\n", buf); 636 1.1 tls if (interactive) 637 1.16 dholland dislog(buf); 638 1.1 tls else 639 1.16 dholland printf("%s\n", buf); 640 1.1 tls } 641 1.1 tls 642 1.20 dholland static void 643 1.15 dholland quit(void) 644 1.1 tls { 645 1.1 tls if (interactive) { 646 1.1 tls bdisp(); /* show final board */ 647 1.1 tls cursfini(); 648 1.1 tls } 649 1.1 tls exit(0); 650 1.1 tls } 651 1.1 tls 652 1.35 rillig #if !defined(DEBUG) 653 1.20 dholland static void 654 1.15 dholland quitsig(int dummy __unused) 655 1.4 lukem { 656 1.4 lukem quit(); 657 1.4 lukem } 658 1.35 rillig #endif 659 1.4 lukem 660 1.1 tls /* 661 1.1 tls * Die gracefully. 662 1.1 tls */ 663 1.4 lukem void 664 1.16 dholland panic(const char *fmt, ...) 665 1.1 tls { 666 1.16 dholland va_list ap; 667 1.16 dholland 668 1.27 dholland if (interactive) { 669 1.27 dholland bdisp(); 670 1.27 dholland cursfini(); 671 1.27 dholland } 672 1.27 dholland 673 1.16 dholland fprintf(stderr, "%s: ", prog); 674 1.16 dholland va_start(ap, fmt); 675 1.16 dholland vfprintf(stderr, fmt, ap); 676 1.16 dholland va_end(ap); 677 1.16 dholland fprintf(stderr, "\n"); 678 1.16 dholland 679 1.27 dholland fputs("I resign\n", stdout); 680 1.27 dholland exit(1); 681 1.1 tls } 682