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