Home | History | Annotate | Line # | Download | only in gomoku
main.c revision 1.56
      1  1.56    rillig /*	$NetBSD: main.c,v 1.56 2022/05/22 08:47:26 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.56    rillig __RCSID("$NetBSD: main.c,v 1.56 2022/05/22 08:47:26 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.37    rillig bool	interactive = true;	/* true if interactive */
     61  1.33    rillig int	debug;			/* > 0 if debugging */
     62  1.20  dholland static int test;		/* both moves come from 1: input, 2: computer */
     63  1.20  dholland static char *prog;		/* name of program */
     64  1.23  dholland static char user[LOGIN_NAME_MAX]; /* name of player */
     65  1.20  dholland static FILE *debugfp;		/* file for debug output */
     66  1.20  dholland static FILE *inputfp;		/* file for debug input */
     67   1.1       tls 
     68   1.6       jsm const char	pdir[4]		= "-\\|/";
     69   1.1       tls 
     70   1.1       tls struct	spotstr	board[BAREA];		/* info for board */
     71   1.1       tls struct	combostr frames[FAREA];		/* storage for all frames */
     72   1.1       tls struct	combostr *sortframes[2];	/* sorted list of non-empty frames */
     73   1.1       tls u_char	overlap[FAREA * FAREA];		/* true if frame [a][b] overlap */
     74   1.1       tls short	intersect[FAREA * FAREA];	/* frame [a][b] intersection */
     75   1.1       tls int	movelog[BSZ * BSZ];		/* log of all the moves */
     76   1.1       tls int	movenum;			/* current move number */
     77  1.56    rillig const char *plyr[2] = { "???", "???" };	/* who's who */
     78   1.1       tls 
     79  1.20  dholland static int readinput(FILE *);
     80  1.20  dholland static void misclog(const char *, ...) __printflike(1, 2);
     81  1.20  dholland static void quit(void) __dead;
     82  1.35    rillig #if !defined(DEBUG)
     83  1.35    rillig static void quitsig(int) __dead;
     84  1.35    rillig #endif
     85   1.1       tls 
     86  1.45    rillig static void
     87  1.45    rillig warn_if_exists(const char *fname)
     88  1.45    rillig {
     89  1.45    rillig 	struct stat st;
     90  1.45    rillig 
     91  1.45    rillig 	if (lstat(fname, &st) == 0) {
     92  1.45    rillig 		int x, y;
     93  1.45    rillig 		getyx(stdscr, y, x);
     94  1.45    rillig 		addstr("  (already exists)");
     95  1.45    rillig 		move(y, x);
     96  1.45    rillig 	} else
     97  1.45    rillig 		clrtoeol();
     98  1.45    rillig }
     99  1.45    rillig 
    100  1.50    rillig static void
    101  1.50    rillig save_game(void)
    102  1.50    rillig {
    103  1.50    rillig 	char fname[PATH_MAX];
    104  1.50    rillig 	FILE *fp;
    105  1.50    rillig 
    106  1.50    rillig 	ask("Save file name? ");
    107  1.50    rillig 	(void)get_line(fname, sizeof(fname), warn_if_exists);
    108  1.50    rillig 	if ((fp = fopen(fname, "w")) == NULL) {
    109  1.50    rillig 		misclog("cannot create save file");
    110  1.50    rillig 		return;
    111  1.50    rillig 	}
    112  1.50    rillig 	for (int i = 0; i < movenum - 1; i++)
    113  1.50    rillig 		fprintf(fp, "%s\n", stoc(movelog[i]));
    114  1.50    rillig 	fclose(fp);
    115  1.50    rillig }
    116  1.50    rillig 
    117  1.51    rillig static void
    118  1.51    rillig parse_args(int argc, char **argv)
    119   1.1       tls {
    120  1.51    rillig 	int ch;
    121   1.4     lukem 
    122   1.1       tls 	prog = strrchr(argv[0], '/');
    123  1.46    rillig 	prog = prog != NULL ? prog + 1 : argv[0];
    124   1.1       tls 
    125   1.4     lukem 	while ((ch = getopt(argc, argv, "bcdD:u")) != -1) {
    126   1.1       tls 		switch (ch) {
    127   1.1       tls 		case 'b':	/* background */
    128  1.33    rillig 			interactive = false;
    129   1.1       tls 			break;
    130  1.51    rillig 		case 'c':	/* testing: computer versus computer */
    131  1.51    rillig 			test = 2;
    132  1.51    rillig 			break;
    133  1.46    rillig 		case 'd':
    134   1.1       tls 			debug++;
    135   1.1       tls 			break;
    136   1.1       tls 		case 'D':	/* log debug output to file */
    137   1.1       tls 			if ((debugfp = fopen(optarg, "w")) == NULL)
    138   1.1       tls 				err(1, "%s", optarg);
    139   1.1       tls 			break;
    140  1.22  dholland 		case 'u':	/* testing: user versus user */
    141   1.1       tls 			test = 1;
    142   1.1       tls 			break;
    143  1.38    rillig 		default:
    144  1.46    rillig 		usage:
    145  1.38    rillig 			fprintf(stderr, "usage: %s [-bcdu] [-Dfile] [file]\n",
    146  1.38    rillig 			    getprogname());
    147  1.51    rillig 			exit(EXIT_FAILURE);
    148   1.1       tls 		}
    149   1.1       tls 	}
    150   1.1       tls 	argc -= optind;
    151   1.1       tls 	argv += optind;
    152  1.46    rillig 	if (argc > 1)
    153  1.46    rillig 		goto usage;
    154  1.46    rillig 	if (argc == 1 && (inputfp = fopen(*argv, "r")) == NULL)
    155  1.46    rillig 		err(1, "%s", *argv);
    156  1.51    rillig }
    157  1.51    rillig 
    158  1.52    rillig static void
    159  1.52    rillig set_input_sources(enum input_source *input, int color)
    160  1.52    rillig {
    161  1.52    rillig 	switch (test) {
    162  1.52    rillig 	case 0:			/* user versus program */
    163  1.52    rillig 		input[color] = USER;
    164  1.52    rillig 		input[color != BLACK ? BLACK : WHITE] = PROGRAM;
    165  1.52    rillig 		break;
    166  1.52    rillig 
    167  1.52    rillig 	case 1:			/* user versus user */
    168  1.52    rillig 		input[BLACK] = USER;
    169  1.52    rillig 		input[WHITE] = USER;
    170  1.52    rillig 		break;
    171  1.52    rillig 
    172  1.52    rillig 	case 2:			/* program versus program */
    173  1.52    rillig 		input[BLACK] = PROGRAM;
    174  1.52    rillig 		input[WHITE] = PROGRAM;
    175  1.52    rillig 		break;
    176  1.52    rillig 	}
    177  1.52    rillig }
    178  1.52    rillig 
    179  1.53    rillig static int
    180  1.54    rillig ask_user_color(void)
    181  1.54    rillig {
    182  1.54    rillig 	int color;
    183  1.54    rillig 
    184  1.54    rillig 	mvprintw(BSZ + 3, 0, "Black moves first. ");
    185  1.54    rillig 	ask("(B)lack or (W)hite? ");
    186  1.54    rillig 	for (;;) {
    187  1.54    rillig 		int ch = get_key(NULL);
    188  1.54    rillig 		if (ch == 'b' || ch == 'B') {
    189  1.54    rillig 			color = BLACK;
    190  1.54    rillig 			break;
    191  1.54    rillig 		}
    192  1.54    rillig 		if (ch == 'w' || ch == 'W') {
    193  1.54    rillig 			color = WHITE;
    194  1.54    rillig 			break;
    195  1.54    rillig 		}
    196  1.54    rillig 		if (ch == 'q' || ch == 'Q')
    197  1.54    rillig 			quit();
    198  1.54    rillig 
    199  1.54    rillig 		beep();
    200  1.54    rillig 		ask("Please choose (B)lack or (W)hite: ");
    201  1.54    rillig 	}
    202  1.54    rillig 	move(BSZ + 3, 0);
    203  1.54    rillig 	clrtoeol();
    204  1.54    rillig 	return color;
    205  1.54    rillig }
    206  1.54    rillig 
    207  1.54    rillig static int
    208  1.56    rillig read_color(void)
    209  1.56    rillig {
    210  1.56    rillig 	char buf[128];
    211  1.56    rillig 
    212  1.56    rillig 	get_line(buf, sizeof(buf), NULL);
    213  1.56    rillig 	if (strcmp(buf, "black") == 0)
    214  1.56    rillig 		return BLACK;
    215  1.56    rillig 	if (strcmp(buf, "white") == 0)
    216  1.56    rillig 		return WHITE;
    217  1.56    rillig 	panic("Huh?  Expected `black' or `white', got `%s'\n", buf);
    218  1.56    rillig 	/* NOTREACHED */
    219  1.56    rillig }
    220  1.56    rillig 
    221  1.56    rillig static int
    222  1.53    rillig read_move(void)
    223  1.53    rillig {
    224  1.53    rillig again:
    225  1.53    rillig 	if (interactive) {
    226  1.53    rillig 		ask("Select move, (S)ave or (Q)uit.");
    227  1.53    rillig 		int s = get_coord();
    228  1.53    rillig 		if (s == SAVE) {
    229  1.53    rillig 			save_game();
    230  1.53    rillig 			goto again;
    231  1.53    rillig 		}
    232  1.53    rillig 		if (s != RESIGN && board[s].s_occ != EMPTY) {
    233  1.53    rillig 			beep();
    234  1.53    rillig 			goto again;
    235  1.53    rillig 		}
    236  1.53    rillig 		return s;
    237  1.53    rillig 	} else {
    238  1.53    rillig 		char buf[128];
    239  1.53    rillig 		if (!get_line(buf, sizeof(buf), NULL))
    240  1.53    rillig 			return RESIGN;
    241  1.53    rillig 		if (buf[0] == '\0')
    242  1.53    rillig 			goto again;
    243  1.53    rillig 		return ctos(buf);
    244  1.53    rillig 	}
    245  1.53    rillig }
    246  1.53    rillig 
    247  1.55    rillig static void
    248  1.55    rillig declare_winner(int outcome, const enum input_source *input, int color)
    249  1.55    rillig {
    250  1.55    rillig 
    251  1.55    rillig 	move(BSZ + 3, 0);
    252  1.55    rillig 	switch (outcome) {
    253  1.55    rillig 	case WIN:
    254  1.55    rillig 		if (input[color] == PROGRAM)
    255  1.55    rillig 			addstr("Ha ha, I won");
    256  1.55    rillig 		else if (input[0] == USER && input[1] == USER)
    257  1.55    rillig 			addstr("Well, you won (and lost)");
    258  1.55    rillig 		else
    259  1.55    rillig 			addstr("Rats! you won");
    260  1.55    rillig 		break;
    261  1.55    rillig 	case TIE:
    262  1.55    rillig 		addstr("Wow! It's a tie");
    263  1.55    rillig 		break;
    264  1.55    rillig 	case ILLEGAL:
    265  1.55    rillig 		addstr("Illegal move");
    266  1.55    rillig 		break;
    267  1.55    rillig 	}
    268  1.55    rillig 	clrtoeol();
    269  1.55    rillig 	bdisp();
    270  1.55    rillig }
    271  1.55    rillig 
    272  1.51    rillig int
    273  1.51    rillig main(int argc, char **argv)
    274  1.51    rillig {
    275  1.51    rillig 	char *user_name;
    276  1.51    rillig 	int color, curmove;
    277  1.51    rillig 	enum input_source input[2];
    278  1.51    rillig 
    279  1.51    rillig 	/* Revoke setgid privileges */
    280  1.51    rillig 	setgid(getgid());
    281  1.51    rillig 
    282  1.51    rillig 	setprogname(argv[0]);
    283  1.51    rillig 
    284  1.51    rillig 	user_name = getlogin();
    285  1.51    rillig 	strlcpy(user, user_name != NULL ? user_name : "you", sizeof(user));
    286  1.51    rillig 
    287  1.56    rillig 	curmove = 0;
    288  1.56    rillig 	color = BLACK;
    289  1.51    rillig 
    290  1.51    rillig 	parse_args(argc, argv);
    291   1.1       tls 
    292  1.33    rillig 	if (debug == 0)
    293  1.32    rillig 		srandom((unsigned int)time(0));
    294   1.1       tls 	if (interactive)
    295   1.1       tls 		cursinit();		/* initialize curses */
    296   1.1       tls again:
    297   1.1       tls 	bdinit(board);			/* initialize board contents */
    298   1.1       tls 
    299   1.1       tls 	if (interactive) {
    300   1.1       tls 		bdisp_init();		/* initialize display of board */
    301   1.1       tls #ifdef DEBUG
    302   1.1       tls 		signal(SIGINT, whatsup);
    303   1.1       tls #else
    304   1.4     lukem 		signal(SIGINT, quitsig);
    305   1.1       tls #endif
    306   1.1       tls 
    307  1.56    rillig 		if (inputfp == NULL && test == 0)
    308  1.54    rillig 			color = ask_user_color();
    309   1.1       tls 	} else {
    310  1.56    rillig 		setbuf(stdout, NULL);
    311  1.56    rillig 		color = read_color();
    312   1.1       tls 	}
    313   1.1       tls 
    314  1.33    rillig 	if (inputfp != NULL) {
    315   1.1       tls 		input[BLACK] = INPUTF;
    316   1.1       tls 		input[WHITE] = INPUTF;
    317   1.1       tls 	} else {
    318  1.52    rillig 		set_input_sources(input, color);
    319   1.1       tls 	}
    320   1.1       tls 	if (interactive) {
    321  1.23  dholland 		plyr[BLACK] = input[BLACK] == USER ? user : prog;
    322  1.23  dholland 		plyr[WHITE] = input[WHITE] == USER ? user : prog;
    323  1.43    rillig 		bdwho();
    324  1.43    rillig 		refresh();
    325   1.1       tls 	}
    326   1.1       tls 
    327  1.51    rillig 	int outcome;
    328  1.33    rillig 	for (color = BLACK; ; color = color != BLACK ? BLACK : WHITE) {
    329   1.1       tls 	top:
    330   1.1       tls 		switch (input[color]) {
    331   1.1       tls 		case INPUTF: /* input comes from a file */
    332   1.1       tls 			curmove = readinput(inputfp);
    333  1.49    rillig 			if (curmove != EOF)
    334   1.1       tls 				break;
    335  1.52    rillig 			set_input_sources(input, color);
    336  1.23  dholland 			plyr[BLACK] = input[BLACK] == USER ? user : prog;
    337  1.23  dholland 			plyr[WHITE] = input[WHITE] == USER ? user : prog;
    338  1.43    rillig 			bdwho();
    339  1.43    rillig 			refresh();
    340   1.1       tls 			goto top;
    341   1.1       tls 
    342   1.1       tls 		case USER: /* input comes from standard input */
    343  1.53    rillig 			curmove = read_move();
    344   1.1       tls 			break;
    345   1.1       tls 
    346   1.1       tls 		case PROGRAM: /* input comes from the program */
    347  1.23  dholland 			if (interactive)
    348  1.23  dholland 				ask("Thinking...");
    349   1.1       tls 			curmove = pickmove(color);
    350   1.1       tls 			break;
    351   1.1       tls 		}
    352  1.49    rillig 		if (interactive && curmove != ILLEGAL) {
    353  1.48    rillig 			misclog("%3d%*s%-6s", movenum,
    354  1.48    rillig 			    color == BLACK ? 2 : 9, "", stoc(curmove));
    355   1.1       tls 		}
    356  1.51    rillig 		if ((outcome = makemove(color, curmove)) != MOVEOK)
    357   1.1       tls 			break;
    358   1.1       tls 		if (interactive)
    359   1.1       tls 			bdisp();
    360   1.1       tls 	}
    361   1.1       tls 	if (interactive) {
    362  1.55    rillig 		declare_winner(outcome, input, color);
    363  1.51    rillig 		if (outcome != RESIGN) {
    364   1.1       tls 		replay:
    365  1.24  dholland 			ask("Play again? ");
    366  1.51    rillig 			int ch = get_key("YyNnQqSs");
    367  1.24  dholland 			if (ch == 'Y' || ch == 'y')
    368   1.1       tls 				goto again;
    369  1.24  dholland 			if (ch == 'S') {
    370  1.50    rillig 				save_game();
    371   1.1       tls 				goto replay;
    372   1.1       tls 			}
    373   1.1       tls 		}
    374   1.1       tls 	}
    375   1.1       tls 	quit();
    376   1.1       tls }
    377   1.1       tls 
    378  1.20  dholland static int
    379  1.15  dholland readinput(FILE *fp)
    380   1.1       tls {
    381   1.1       tls 	int c;
    382  1.17  dholland 	char buf[128];
    383  1.17  dholland 	size_t pos;
    384   1.1       tls 
    385  1.17  dholland 	pos = 0;
    386  1.17  dholland 	while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1)
    387  1.17  dholland 		buf[pos++] = c;
    388  1.17  dholland 	buf[pos] = '\0';
    389  1.49    rillig 	return c == EOF ? EOF : ctos(buf);
    390   1.1       tls }
    391   1.1       tls 
    392   1.1       tls #ifdef DEBUG
    393   1.1       tls /*
    394   1.1       tls  * Handle strange situations.
    395   1.1       tls  */
    396  1.35    rillig /* ARGSUSED */
    397   1.1       tls void
    398  1.15  dholland whatsup(int signum)
    399   1.1       tls {
    400  1.18  dholland 	int i, n, s1, s2, d1, d2;
    401   1.1       tls 	struct spotstr *sp;
    402   1.1       tls 	FILE *fp;
    403   1.1       tls 	char *str;
    404   1.1       tls 	struct elist *ep;
    405   1.1       tls 	struct combostr *cbp;
    406  1.18  dholland 	char input[128];
    407  1.18  dholland 	char tmp[128];
    408   1.1       tls 
    409   1.1       tls 	if (!interactive)
    410   1.1       tls 		quit();
    411   1.1       tls top:
    412  1.24  dholland 	ask("debug command: ");
    413  1.45    rillig 	if (!get_line(input, sizeof(input), NULL))
    414   1.1       tls 		quit();
    415  1.18  dholland 	switch (*input) {
    416   1.1       tls 	case '\0':
    417   1.1       tls 		goto top;
    418   1.1       tls 	case 'q':		/* conservative quit */
    419   1.1       tls 		quit();
    420  1.35    rillig 		/* NOTREACHED */
    421   1.1       tls 	case 'd':		/* set debug level */
    422  1.18  dholland 		debug = input[1] - '0';
    423  1.16  dholland 		debuglog("Debug set to %d", debug);
    424  1.44    rillig 		goto top;
    425   1.1       tls 	case 'c':
    426   1.1       tls 		break;
    427   1.1       tls 	case 'b':		/* back up a move */
    428   1.1       tls 		if (movenum > 1) {
    429   1.1       tls 			movenum--;
    430   1.1       tls 			board[movelog[movenum - 1]].s_occ = EMPTY;
    431   1.1       tls 			bdisp();
    432   1.1       tls 		}
    433   1.1       tls 		goto top;
    434   1.1       tls 	case 's':		/* suggest a move */
    435  1.18  dholland 		i = input[1] == 'b' ? BLACK : WHITE;
    436  1.16  dholland 		debuglog("suggest %c %s", i == BLACK ? 'B' : 'W',
    437   1.1       tls 			stoc(pickmove(i)));
    438   1.1       tls 		goto top;
    439   1.1       tls 	case 'f':		/* go forward a move */
    440  1.35    rillig 		board[movelog[movenum - 1]].s_occ =
    441  1.35    rillig 		    (movenum & 1) != 0 ? BLACK : WHITE;
    442   1.1       tls 		movenum++;
    443   1.1       tls 		bdisp();
    444   1.1       tls 		goto top;
    445   1.1       tls 	case 'l':		/* print move history */
    446  1.18  dholland 		if (input[1] == '\0') {
    447   1.1       tls 			for (i = 0; i < movenum - 1; i++)
    448  1.16  dholland 				debuglog("%s", stoc(movelog[i]));
    449   1.1       tls 			goto top;
    450   1.1       tls 		}
    451  1.18  dholland 		if ((fp = fopen(input + 1, "w")) == NULL)
    452   1.1       tls 			goto top;
    453   1.1       tls 		for (i = 0; i < movenum - 1; i++) {
    454   1.1       tls 			fprintf(fp, "%s", stoc(movelog[i]));
    455   1.1       tls 			if (++i < movenum - 1)
    456   1.1       tls 				fprintf(fp, " %s\n", stoc(movelog[i]));
    457   1.1       tls 			else
    458   1.1       tls 				fputc('\n', fp);
    459   1.1       tls 		}
    460   1.1       tls 		bdump(fp);
    461   1.1       tls 		fclose(fp);
    462   1.1       tls 		goto top;
    463   1.1       tls 	case 'o':
    464  1.18  dholland 		/* avoid use w/o initialization on invalid input */
    465  1.18  dholland 		d1 = s1 = 0;
    466  1.18  dholland 
    467   1.1       tls 		n = 0;
    468  1.35    rillig 		for (str = input + 1; *str != '\0'; str++)
    469   1.1       tls 			if (*str == ',') {
    470   1.1       tls 				for (d1 = 0; d1 < 4; d1++)
    471   1.1       tls 					if (str[-1] == pdir[d1])
    472   1.1       tls 						break;
    473   1.1       tls 				str[-1] = '\0';
    474  1.18  dholland 				sp = &board[s1 = ctos(input + 1)];
    475  1.36    rillig 				n = (int)(sp->s_frame[d1] - frames) * FAREA;
    476   1.1       tls 				*str++ = '\0';
    477   1.1       tls 				break;
    478   1.1       tls 			}
    479   1.1       tls 		sp = &board[s2 = ctos(str)];
    480  1.35    rillig 		while (*str != '\0')
    481   1.1       tls 			str++;
    482   1.1       tls 		for (d2 = 0; d2 < 4; d2++)
    483   1.1       tls 			if (str[-1] == pdir[d2])
    484   1.1       tls 				break;
    485  1.36    rillig 		n += (int)(sp->s_frame[d2] - frames);
    486  1.16  dholland 		debuglog("overlap %s%c,%s%c = %x", stoc(s1), pdir[d1],
    487  1.16  dholland 		    stoc(s2), pdir[d2], overlap[n]);
    488   1.1       tls 		goto top;
    489   1.1       tls 	case 'p':
    490  1.18  dholland 		sp = &board[i = ctos(input + 1)];
    491  1.16  dholland 		debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i),
    492   1.1       tls 			sp->s_combo[BLACK].s, sp->s_level[BLACK],
    493   1.1       tls 			sp->s_nforce[BLACK],
    494   1.1       tls 			sp->s_combo[WHITE].s, sp->s_level[WHITE],
    495  1.18  dholland 			sp->s_nforce[WHITE], sp->s_wval, sp->s_flags);
    496  1.16  dholland 		debuglog("FB %s %x %x %x %x", stoc(i),
    497   1.1       tls 			sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
    498   1.1       tls 			sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
    499  1.16  dholland 		debuglog("FW %s %x %x %x %x", stoc(i),
    500   1.1       tls 			sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s,
    501   1.1       tls 			sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s);
    502   1.1       tls 		goto top;
    503   1.1       tls 	case 'e':	/* e {b|w} [0-9] spot */
    504  1.18  dholland 		str = input + 1;
    505   1.1       tls 		if (*str >= '0' && *str <= '9')
    506   1.1       tls 			n = *str++ - '0';
    507   1.1       tls 		else
    508   1.1       tls 			n = 0;
    509   1.1       tls 		sp = &board[i = ctos(str)];
    510  1.35    rillig 		for (ep = sp->s_empty; ep != NULL; ep = ep->e_next) {
    511   1.1       tls 			cbp = ep->e_combo;
    512  1.35    rillig 			if (n != 0) {
    513   1.1       tls 				if (cbp->c_nframes > n)
    514   1.1       tls 					continue;
    515   1.1       tls 				if (cbp->c_nframes != n)
    516   1.1       tls 					break;
    517   1.1       tls 			}
    518  1.18  dholland 			printcombo(cbp, tmp, sizeof(tmp));
    519  1.18  dholland 			debuglog("%s", tmp);
    520   1.1       tls 		}
    521   1.1       tls 		goto top;
    522   1.1       tls 	default:
    523  1.16  dholland 		debuglog("Options are:");
    524  1.16  dholland 		debuglog("q    - quit");
    525  1.16  dholland 		debuglog("c    - continue");
    526  1.16  dholland 		debuglog("d#   - set debug level to #");
    527  1.16  dholland 		debuglog("p#   - print values at #");
    528   1.1       tls 		goto top;
    529   1.1       tls 	}
    530   1.1       tls }
    531   1.1       tls #endif /* DEBUG */
    532   1.1       tls 
    533   1.1       tls /*
    534   1.1       tls  * Display debug info.
    535   1.1       tls  */
    536   1.4     lukem void
    537  1.16  dholland debuglog(const char *fmt, ...)
    538   1.1       tls {
    539  1.16  dholland 	va_list ap;
    540  1.16  dholland 	char buf[128];
    541  1.16  dholland 
    542  1.16  dholland 	va_start(ap, fmt);
    543  1.16  dholland 	vsnprintf(buf, sizeof(buf), fmt, ap);
    544  1.16  dholland 	va_end(ap);
    545   1.1       tls 
    546  1.33    rillig 	if (debugfp != NULL)
    547  1.16  dholland 		fprintf(debugfp, "%s\n", buf);
    548   1.1       tls 	if (interactive)
    549  1.16  dholland 		dislog(buf);
    550   1.1       tls 	else
    551  1.16  dholland 		fprintf(stderr, "%s\n", buf);
    552   1.1       tls }
    553   1.1       tls 
    554  1.20  dholland static void
    555  1.16  dholland misclog(const char *fmt, ...)
    556   1.1       tls {
    557  1.16  dholland 	va_list ap;
    558  1.16  dholland 	char buf[128];
    559  1.16  dholland 
    560  1.16  dholland 	va_start(ap, fmt);
    561  1.16  dholland 	vsnprintf(buf, sizeof(buf), fmt, ap);
    562  1.16  dholland 	va_end(ap);
    563   1.1       tls 
    564  1.33    rillig 	if (debugfp != NULL)
    565  1.16  dholland 		fprintf(debugfp, "%s\n", buf);
    566   1.1       tls 	if (interactive)
    567  1.16  dholland 		dislog(buf);
    568   1.1       tls 	else
    569  1.16  dholland 		printf("%s\n", buf);
    570   1.1       tls }
    571   1.1       tls 
    572  1.20  dholland static void
    573  1.15  dholland quit(void)
    574   1.1       tls {
    575   1.1       tls 	if (interactive) {
    576   1.1       tls 		bdisp();		/* show final board */
    577   1.1       tls 		cursfini();
    578   1.1       tls 	}
    579   1.1       tls 	exit(0);
    580   1.1       tls }
    581   1.1       tls 
    582  1.35    rillig #if !defined(DEBUG)
    583  1.20  dholland static void
    584  1.15  dholland quitsig(int dummy __unused)
    585   1.4     lukem {
    586   1.4     lukem 	quit();
    587   1.4     lukem }
    588  1.35    rillig #endif
    589   1.4     lukem 
    590   1.1       tls /*
    591   1.1       tls  * Die gracefully.
    592   1.1       tls  */
    593   1.4     lukem void
    594  1.16  dholland panic(const char *fmt, ...)
    595   1.1       tls {
    596  1.16  dholland 	va_list ap;
    597  1.16  dholland 
    598  1.27  dholland 	if (interactive) {
    599  1.27  dholland 		bdisp();
    600  1.27  dholland 		cursfini();
    601  1.27  dholland 	}
    602  1.27  dholland 
    603  1.16  dholland 	fprintf(stderr, "%s: ", prog);
    604  1.16  dholland 	va_start(ap, fmt);
    605  1.16  dholland 	vfprintf(stderr, fmt, ap);
    606  1.16  dholland 	va_end(ap);
    607  1.16  dholland 	fprintf(stderr, "\n");
    608  1.16  dholland 
    609  1.27  dholland 	fputs("I resign\n", stdout);
    610  1.27  dholland 	exit(1);
    611   1.1       tls }
    612