Home | History | Annotate | Line # | Download | only in gomoku
main.c revision 1.24
      1  1.24  dholland /*	$NetBSD: main.c,v 1.24 2010/03/29 04:28:47 dholland 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.1       tls #ifndef lint
     37  1.14     lukem __COPYRIGHT("@(#) Copyright (c) 1994\
     38  1.14     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       tls #endif /* not lint */
     40   1.1       tls 
     41   1.1       tls #ifndef lint
     42   1.2       tls #if 0
     43   1.1       tls static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 5/4/95";
     44   1.2       tls #else
     45  1.24  dholland __RCSID("$NetBSD: main.c,v 1.24 2010/03/29 04:28:47 dholland Exp $");
     46   1.2       tls #endif
     47   1.1       tls #endif /* not lint */
     48   1.1       tls 
     49   1.1       tls #include <curses.h>
     50   1.1       tls #include <err.h>
     51  1.21  dholland #include <limits.h>
     52   1.1       tls #include <signal.h>
     53  1.16  dholland #include <stdarg.h>
     54   1.1       tls #include <stdlib.h>
     55   1.1       tls #include <string.h>
     56   1.5     perry #include <time.h>
     57   1.1       tls #include <unistd.h>
     58   1.1       tls 
     59   1.1       tls #include "gomoku.h"
     60   1.1       tls 
     61   1.1       tls #define USER	0		/* get input from standard input */
     62   1.1       tls #define PROGRAM	1		/* get input from program */
     63   1.1       tls #define INPUTF	2		/* get input from a file */
     64   1.1       tls 
     65   1.1       tls int	interactive = 1;	/* true if interactive */
     66   1.1       tls int	debug;			/* true if debugging */
     67  1.20  dholland static int test;		/* both moves come from 1: input, 2: computer */
     68  1.20  dholland static char *prog;		/* name of program */
     69  1.23  dholland static char user[LOGIN_NAME_MAX]; /* name of player */
     70  1.20  dholland static FILE *debugfp;		/* file for debug output */
     71  1.20  dholland static FILE *inputfp;		/* file for debug input */
     72   1.1       tls 
     73   1.6       jsm const char	pdir[4]		= "-\\|/";
     74   1.1       tls 
     75   1.1       tls struct	spotstr	board[BAREA];		/* info for board */
     76   1.1       tls struct	combostr frames[FAREA];		/* storage for all frames */
     77   1.1       tls struct	combostr *sortframes[2];	/* sorted list of non-empty frames */
     78   1.1       tls u_char	overlap[FAREA * FAREA];		/* true if frame [a][b] overlap */
     79   1.1       tls short	intersect[FAREA * FAREA];	/* frame [a][b] intersection */
     80   1.1       tls int	movelog[BSZ * BSZ];		/* log of all the moves */
     81   1.1       tls int	movenum;			/* current move number */
     82   1.6       jsm const char	*plyr[2];			/* who's who */
     83   1.1       tls 
     84  1.20  dholland static int readinput(FILE *);
     85  1.20  dholland static void misclog(const char *, ...) __printflike(1, 2);
     86  1.20  dholland static void quit(void) __dead;
     87  1.20  dholland static void quitsig(int) __dead;
     88   1.1       tls 
     89   1.4     lukem int
     90  1.15  dholland main(int argc, char **argv)
     91   1.1       tls {
     92   1.1       tls 	char buf[128];
     93  1.21  dholland 	char fname[PATH_MAX];
     94  1.23  dholland 	char *tmp;
     95   1.1       tls 	int color, curmove, i, ch;
     96   1.1       tls 	int input[2];
     97   1.6       jsm 	static const char *const fmt[2] = {
     98   1.1       tls 		"%3d %-6s",
     99   1.1       tls 		"%3d        %-6s"
    100   1.1       tls 	};
    101   1.8       jsm 
    102   1.8       jsm 	/* Revoke setgid privileges */
    103  1.10   mycroft 	setgid(getgid());
    104   1.1       tls 
    105  1.23  dholland 	tmp = getlogin();
    106  1.23  dholland 	if (tmp) {
    107  1.23  dholland 		strlcpy(user, tmp, sizeof(user));
    108  1.23  dholland 	} else {
    109  1.23  dholland 		strcpy(user, "you");
    110  1.23  dholland 	}
    111  1.23  dholland 
    112   1.4     lukem 	color = curmove = 0;
    113   1.4     lukem 
    114   1.1       tls 	prog = strrchr(argv[0], '/');
    115   1.1       tls 	if (prog)
    116   1.1       tls 		prog++;
    117   1.1       tls 	else
    118   1.1       tls 		prog = 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.1       tls 			interactive = 0;
    124   1.1       tls 			break;
    125   1.1       tls 		case 'd':	/* debugging */
    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.1       tls 		}
    139   1.1       tls 	}
    140   1.1       tls 	argc -= optind;
    141   1.1       tls 	argv += optind;
    142   1.1       tls 	if (argc) {
    143   1.1       tls 		if ((inputfp = fopen(*argv, "r")) == NULL)
    144   1.1       tls 			err(1, "%s", *argv);
    145   1.1       tls 	}
    146   1.1       tls 
    147   1.1       tls 	if (!debug)
    148   1.1       tls #ifdef SVR4
    149   1.1       tls 		srand(time(0));
    150   1.1       tls #else
    151   1.1       tls 		srandom(time(0));
    152   1.1       tls #endif
    153   1.1       tls 	if (interactive)
    154   1.1       tls 		cursinit();		/* initialize curses */
    155   1.1       tls again:
    156   1.1       tls 	bdinit(board);			/* initialize board contents */
    157   1.1       tls 
    158   1.1       tls 	if (interactive) {
    159   1.1       tls 		plyr[BLACK] = plyr[WHITE] = "???";
    160   1.1       tls 		bdisp_init();		/* initialize display of board */
    161   1.1       tls #ifdef DEBUG
    162   1.1       tls 		signal(SIGINT, whatsup);
    163   1.1       tls #else
    164   1.4     lukem 		signal(SIGINT, quitsig);
    165   1.1       tls #endif
    166   1.1       tls 
    167   1.1       tls 		if (inputfp == NULL && test == 0) {
    168  1.24  dholland 			move(BSZ3, 0);
    169  1.24  dholland 			printw("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.23  dholland 			move(BSZ3, 0);
    188   1.1       tls 			clrtoeol();
    189   1.1       tls 		}
    190   1.1       tls 	} else {
    191   1.1       tls 		setbuf(stdout, 0);
    192  1.19       roy 		get_line(buf, sizeof(buf));
    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.1       tls 	if (inputfp) {
    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.1       tls 			input[!color] = 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.1       tls 		bdwho(1);
    228   1.1       tls 	}
    229   1.1       tls 
    230   1.1       tls 	for (color = BLACK; ; color = !color) {
    231   1.1       tls 	top:
    232   1.1       tls 		switch (input[color]) {
    233   1.1       tls 		case INPUTF: /* input comes from a file */
    234   1.1       tls 			curmove = readinput(inputfp);
    235   1.1       tls 			if (curmove != ILLEGAL)
    236   1.1       tls 				break;
    237   1.1       tls 			switch (test) {
    238  1.22  dholland 			case 0: /* user versus program */
    239   1.1       tls 				input[color] = USER;
    240   1.1       tls 				input[!color] = PROGRAM;
    241   1.1       tls 				break;
    242   1.1       tls 
    243  1.22  dholland 			case 1: /* user versus user */
    244   1.1       tls 				input[BLACK] = USER;
    245   1.1       tls 				input[WHITE] = USER;
    246   1.1       tls 				break;
    247   1.1       tls 
    248  1.22  dholland 			case 2: /* program versus program */
    249   1.1       tls 				input[BLACK] = PROGRAM;
    250   1.1       tls 				input[WHITE] = PROGRAM;
    251   1.1       tls 				break;
    252   1.1       tls 			}
    253  1.23  dholland 			plyr[BLACK] = input[BLACK] == USER ? user : prog;
    254  1.23  dholland 			plyr[WHITE] = input[WHITE] == USER ? user : prog;
    255   1.1       tls 			bdwho(1);
    256   1.1       tls 			goto top;
    257   1.1       tls 
    258   1.1       tls 		case USER: /* input comes from standard input */
    259   1.1       tls 		getinput:
    260  1.23  dholland 			if (interactive) {
    261  1.24  dholland 				ask("Select move, (S)ave or (Q)uit.");
    262  1.23  dholland 				curmove = get_coord();
    263   1.1       tls 				if (curmove == SAVE) {
    264   1.1       tls 					FILE *fp;
    265   1.1       tls 
    266  1.24  dholland 					ask("Save file name? ");
    267  1.21  dholland 					(void)get_line(fname, sizeof(fname));
    268  1.21  dholland 					if ((fp = fopen(fname, "w")) == NULL) {
    269  1.16  dholland 						misclog("cannot create save file");
    270   1.1       tls 						goto getinput;
    271   1.1       tls 					}
    272   1.1       tls 					for (i = 0; i < movenum - 1; i++)
    273   1.1       tls 						fprintf(fp, "%s\n",
    274   1.1       tls 							stoc(movelog[i]));
    275   1.1       tls 					fclose(fp);
    276   1.1       tls 					goto getinput;
    277   1.1       tls 				}
    278   1.1       tls 				if (curmove != RESIGN &&
    279   1.1       tls 				    board[curmove].s_occ != EMPTY) {
    280  1.23  dholland 					/*misclog("Illegal move");*/
    281  1.23  dholland 					beep();
    282   1.1       tls 					goto getinput;
    283   1.1       tls 				}
    284  1.23  dholland 			} else {
    285  1.23  dholland 				if (!get_line(buf, sizeof(buf))) {
    286  1.23  dholland 					curmove = RESIGN;
    287  1.23  dholland 					break;
    288  1.23  dholland 				}
    289  1.23  dholland 				if (buf[0] == '\0')
    290  1.23  dholland 					goto getinput;
    291  1.23  dholland 				curmove = ctos(buf);
    292   1.1       tls 			}
    293   1.1       tls 			break;
    294   1.1       tls 
    295   1.1       tls 		case PROGRAM: /* input comes from the program */
    296  1.23  dholland 			if (interactive)
    297  1.23  dholland 				ask("Thinking...");
    298   1.1       tls 			curmove = pickmove(color);
    299   1.1       tls 			break;
    300   1.1       tls 		}
    301   1.1       tls 		if (interactive) {
    302  1.16  dholland 			misclog(fmt[color], movenum, stoc(curmove));
    303   1.1       tls 		}
    304   1.1       tls 		if ((i = makemove(color, curmove)) != MOVEOK)
    305   1.1       tls 			break;
    306   1.1       tls 		if (interactive)
    307   1.1       tls 			bdisp();
    308   1.1       tls 	}
    309   1.1       tls 	if (interactive) {
    310  1.23  dholland 		move(BSZ3, 0);
    311   1.1       tls 		switch (i) {
    312   1.1       tls 		case WIN:
    313   1.1       tls 			if (input[color] == PROGRAM)
    314   1.1       tls 				addstr("Ha ha, I won");
    315  1.23  dholland 			else if (input[0] == USER && input[1] == USER)
    316  1.23  dholland 				addstr("Well, you won (and lost)");
    317   1.1       tls 			else
    318   1.1       tls 				addstr("Rats! you won");
    319   1.1       tls 			break;
    320   1.1       tls 		case TIE:
    321  1.24  dholland 			addstr("Wow! It's a tie");
    322   1.1       tls 			break;
    323   1.1       tls 		case ILLEGAL:
    324   1.1       tls 			addstr("Illegal move");
    325   1.1       tls 			break;
    326   1.1       tls 		}
    327   1.1       tls 		clrtoeol();
    328   1.1       tls 		bdisp();
    329   1.1       tls 		if (i != RESIGN) {
    330   1.1       tls 		replay:
    331  1.24  dholland 			ask("Play again? ");
    332  1.24  dholland 			ch = get_key("YyNnQqSs");
    333  1.24  dholland 			if (ch == 'Y' || ch == 'y')
    334   1.1       tls 				goto again;
    335  1.24  dholland 			if (ch == 'S') {
    336   1.1       tls 				FILE *fp;
    337   1.1       tls 
    338  1.24  dholland 				ask("Save file name? ");
    339  1.21  dholland 				(void)get_line(fname, sizeof(fname));
    340  1.21  dholland 				if ((fp = fopen(fname, "w")) == NULL) {
    341  1.16  dholland 					misclog("cannot create save file");
    342   1.1       tls 					goto replay;
    343   1.1       tls 				}
    344   1.1       tls 				for (i = 0; i < movenum - 1; i++)
    345   1.1       tls 					fprintf(fp, "%s\n",
    346   1.1       tls 						stoc(movelog[i]));
    347   1.1       tls 				fclose(fp);
    348   1.1       tls 				goto replay;
    349   1.1       tls 			}
    350   1.1       tls 		}
    351   1.1       tls 	}
    352   1.1       tls 	quit();
    353   1.4     lukem 	/* NOTREACHED */
    354   1.4     lukem 	return(0);
    355   1.1       tls }
    356   1.1       tls 
    357  1.20  dholland static int
    358  1.15  dholland readinput(FILE *fp)
    359   1.1       tls {
    360   1.1       tls 	int c;
    361  1.17  dholland 	char buf[128];
    362  1.17  dholland 	size_t pos;
    363   1.1       tls 
    364  1.17  dholland 	pos = 0;
    365  1.17  dholland 	while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1)
    366  1.17  dholland 		buf[pos++] = c;
    367  1.17  dholland 	buf[pos] = '\0';
    368  1.17  dholland 	return ctos(buf);
    369   1.1       tls }
    370   1.1       tls 
    371   1.1       tls #ifdef DEBUG
    372   1.1       tls /*
    373   1.1       tls  * Handle strange situations.
    374   1.1       tls  */
    375   1.1       tls void
    376  1.15  dholland whatsup(int signum)
    377   1.1       tls {
    378  1.18  dholland 	int i, n, s1, s2, d1, d2;
    379   1.1       tls 	struct spotstr *sp;
    380   1.1       tls 	FILE *fp;
    381   1.1       tls 	char *str;
    382   1.1       tls 	struct elist *ep;
    383   1.1       tls 	struct combostr *cbp;
    384  1.18  dholland 	char input[128];
    385  1.18  dholland 	char tmp[128];
    386   1.1       tls 
    387   1.1       tls 	if (!interactive)
    388   1.1       tls 		quit();
    389   1.1       tls top:
    390  1.24  dholland 	ask("debug command: ");
    391  1.19       roy 	if (!get_line(input, sizeof(input)))
    392   1.1       tls 		quit();
    393  1.18  dholland 	switch (*input) {
    394   1.1       tls 	case '\0':
    395   1.1       tls 		goto top;
    396   1.1       tls 	case 'q':		/* conservative quit */
    397   1.1       tls 		quit();
    398   1.1       tls 	case 'd':		/* set debug level */
    399  1.18  dholland 		debug = input[1] - '0';
    400  1.16  dholland 		debuglog("Debug set to %d", debug);
    401   1.1       tls 		sleep(1);
    402   1.1       tls 	case 'c':
    403   1.1       tls 		break;
    404   1.1       tls 	case 'b':		/* back up a move */
    405   1.1       tls 		if (movenum > 1) {
    406   1.1       tls 			movenum--;
    407   1.1       tls 			board[movelog[movenum - 1]].s_occ = EMPTY;
    408   1.1       tls 			bdisp();
    409   1.1       tls 		}
    410   1.1       tls 		goto top;
    411   1.1       tls 	case 's':		/* suggest a move */
    412  1.18  dholland 		i = input[1] == 'b' ? BLACK : WHITE;
    413  1.16  dholland 		debuglog("suggest %c %s", i == BLACK ? 'B' : 'W',
    414   1.1       tls 			stoc(pickmove(i)));
    415   1.1       tls 		goto top;
    416   1.1       tls 	case 'f':		/* go forward a move */
    417   1.1       tls 		board[movelog[movenum - 1]].s_occ = movenum & 1 ? BLACK : WHITE;
    418   1.1       tls 		movenum++;
    419   1.1       tls 		bdisp();
    420   1.1       tls 		goto top;
    421   1.1       tls 	case 'l':		/* print move history */
    422  1.18  dholland 		if (input[1] == '\0') {
    423   1.1       tls 			for (i = 0; i < movenum - 1; i++)
    424  1.16  dholland 				debuglog("%s", stoc(movelog[i]));
    425   1.1       tls 			goto top;
    426   1.1       tls 		}
    427  1.18  dholland 		if ((fp = fopen(input + 1, "w")) == NULL)
    428   1.1       tls 			goto top;
    429   1.1       tls 		for (i = 0; i < movenum - 1; i++) {
    430   1.1       tls 			fprintf(fp, "%s", stoc(movelog[i]));
    431   1.1       tls 			if (++i < movenum - 1)
    432   1.1       tls 				fprintf(fp, " %s\n", stoc(movelog[i]));
    433   1.1       tls 			else
    434   1.1       tls 				fputc('\n', fp);
    435   1.1       tls 		}
    436   1.1       tls 		bdump(fp);
    437   1.1       tls 		fclose(fp);
    438   1.1       tls 		goto top;
    439   1.1       tls 	case 'o':
    440  1.18  dholland 		/* avoid use w/o initialization on invalid input */
    441  1.18  dholland 		d1 = s1 = 0;
    442  1.18  dholland 
    443   1.1       tls 		n = 0;
    444  1.18  dholland 		for (str = input + 1; *str; str++)
    445   1.1       tls 			if (*str == ',') {
    446   1.1       tls 				for (d1 = 0; d1 < 4; d1++)
    447   1.1       tls 					if (str[-1] == pdir[d1])
    448   1.1       tls 						break;
    449   1.1       tls 				str[-1] = '\0';
    450  1.18  dholland 				sp = &board[s1 = ctos(input + 1)];
    451   1.1       tls 				n = (sp->s_frame[d1] - frames) * FAREA;
    452   1.1       tls 				*str++ = '\0';
    453   1.1       tls 				break;
    454   1.1       tls 			}
    455   1.1       tls 		sp = &board[s2 = ctos(str)];
    456   1.1       tls 		while (*str)
    457   1.1       tls 			str++;
    458   1.1       tls 		for (d2 = 0; d2 < 4; d2++)
    459   1.1       tls 			if (str[-1] == pdir[d2])
    460   1.1       tls 				break;
    461   1.1       tls 		n += sp->s_frame[d2] - frames;
    462  1.16  dholland 		debuglog("overlap %s%c,%s%c = %x", stoc(s1), pdir[d1],
    463  1.16  dholland 		    stoc(s2), pdir[d2], overlap[n]);
    464   1.1       tls 		goto top;
    465   1.1       tls 	case 'p':
    466  1.18  dholland 		sp = &board[i = ctos(input + 1)];
    467  1.16  dholland 		debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i),
    468   1.1       tls 			sp->s_combo[BLACK].s, sp->s_level[BLACK],
    469   1.1       tls 			sp->s_nforce[BLACK],
    470   1.1       tls 			sp->s_combo[WHITE].s, sp->s_level[WHITE],
    471  1.18  dholland 			sp->s_nforce[WHITE], sp->s_wval, sp->s_flags);
    472  1.16  dholland 		debuglog("FB %s %x %x %x %x", stoc(i),
    473   1.1       tls 			sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
    474   1.1       tls 			sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
    475  1.16  dholland 		debuglog("FW %s %x %x %x %x", stoc(i),
    476   1.1       tls 			sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s,
    477   1.1       tls 			sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s);
    478   1.1       tls 		goto top;
    479   1.1       tls 	case 'e':	/* e {b|w} [0-9] spot */
    480  1.18  dholland 		str = input + 1;
    481   1.1       tls 		if (*str >= '0' && *str <= '9')
    482   1.1       tls 			n = *str++ - '0';
    483   1.1       tls 		else
    484   1.1       tls 			n = 0;
    485   1.1       tls 		sp = &board[i = ctos(str)];
    486   1.1       tls 		for (ep = sp->s_empty; ep; ep = ep->e_next) {
    487   1.1       tls 			cbp = ep->e_combo;
    488   1.1       tls 			if (n) {
    489   1.1       tls 				if (cbp->c_nframes > n)
    490   1.1       tls 					continue;
    491   1.1       tls 				if (cbp->c_nframes != n)
    492   1.1       tls 					break;
    493   1.1       tls 			}
    494  1.18  dholland 			printcombo(cbp, tmp, sizeof(tmp));
    495  1.18  dholland 			debuglog("%s", tmp);
    496   1.1       tls 		}
    497   1.1       tls 		goto top;
    498   1.1       tls 	default:
    499  1.16  dholland 		debuglog("Options are:");
    500  1.16  dholland 		debuglog("q    - quit");
    501  1.16  dholland 		debuglog("c    - continue");
    502  1.16  dholland 		debuglog("d#   - set debug level to #");
    503  1.16  dholland 		debuglog("p#   - print values at #");
    504   1.1       tls 		goto top;
    505   1.1       tls 	}
    506   1.1       tls }
    507   1.1       tls #endif /* DEBUG */
    508   1.1       tls 
    509   1.1       tls /*
    510   1.1       tls  * Display debug info.
    511   1.1       tls  */
    512   1.4     lukem void
    513  1.16  dholland debuglog(const char *fmt, ...)
    514   1.1       tls {
    515  1.16  dholland 	va_list ap;
    516  1.16  dholland 	char buf[128];
    517  1.16  dholland 
    518  1.16  dholland 	va_start(ap, fmt);
    519  1.16  dholland 	vsnprintf(buf, sizeof(buf), fmt, ap);
    520  1.16  dholland 	va_end(ap);
    521   1.1       tls 
    522   1.1       tls 	if (debugfp)
    523  1.16  dholland 		fprintf(debugfp, "%s\n", buf);
    524   1.1       tls 	if (interactive)
    525  1.16  dholland 		dislog(buf);
    526   1.1       tls 	else
    527  1.16  dholland 		fprintf(stderr, "%s\n", buf);
    528   1.1       tls }
    529   1.1       tls 
    530  1.20  dholland static void
    531  1.16  dholland misclog(const char *fmt, ...)
    532   1.1       tls {
    533  1.16  dholland 	va_list ap;
    534  1.16  dholland 	char buf[128];
    535  1.16  dholland 
    536  1.16  dholland 	va_start(ap, fmt);
    537  1.16  dholland 	vsnprintf(buf, sizeof(buf), fmt, ap);
    538  1.16  dholland 	va_end(ap);
    539   1.1       tls 
    540   1.1       tls 	if (debugfp)
    541  1.16  dholland 		fprintf(debugfp, "%s\n", buf);
    542   1.1       tls 	if (interactive)
    543  1.16  dholland 		dislog(buf);
    544   1.1       tls 	else
    545  1.16  dholland 		printf("%s\n", buf);
    546   1.1       tls }
    547   1.1       tls 
    548  1.20  dholland static void
    549  1.15  dholland quit(void)
    550   1.1       tls {
    551   1.1       tls 	if (interactive) {
    552   1.1       tls 		bdisp();		/* show final board */
    553   1.1       tls 		cursfini();
    554   1.1       tls 	}
    555   1.1       tls 	exit(0);
    556   1.1       tls }
    557   1.1       tls 
    558  1.20  dholland static void
    559  1.15  dholland quitsig(int dummy __unused)
    560   1.4     lukem {
    561   1.4     lukem 	quit();
    562   1.4     lukem }
    563   1.4     lukem 
    564   1.1       tls /*
    565   1.1       tls  * Die gracefully.
    566   1.1       tls  */
    567   1.4     lukem void
    568  1.16  dholland panic(const char *fmt, ...)
    569   1.1       tls {
    570  1.16  dholland 	va_list ap;
    571  1.16  dholland 
    572  1.16  dholland 	fprintf(stderr, "%s: ", prog);
    573  1.16  dholland 	va_start(ap, fmt);
    574  1.16  dholland 	vfprintf(stderr, fmt, ap);
    575  1.16  dholland 	va_end(ap);
    576  1.16  dholland 	fprintf(stderr, "\n");
    577  1.16  dholland 
    578   1.1       tls 	fputs("resign\n", stdout);
    579   1.1       tls 	quit();
    580   1.1       tls }
    581