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