2016-06-12 05:15:26 +03:00
|
|
|
/* $NetBSD: main.c,v 1.27 2016/06/12 02:15:26 dholland Exp $ */
|
1997-01-03 04:35:23 +03:00
|
|
|
|
1996-12-28 21:44:55 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Ralph Campbell.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:36:50 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1996-12-28 21:44:55 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-10 17:36:01 +04:00
|
|
|
#include <sys/cdefs.h>
|
1996-12-28 21:44:55 +03:00
|
|
|
#ifndef lint
|
2008-07-20 04:52:39 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1994\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1996-12-28 21:44:55 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-12-28 21:56:57 +03:00
|
|
|
#if 0
|
1996-12-28 21:44:55 +03:00
|
|
|
static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
|
1996-12-28 21:56:57 +03:00
|
|
|
#else
|
2016-06-12 05:15:26 +03:00
|
|
|
__RCSID("$NetBSD: main.c,v 1.27 2016/06/12 02:15:26 dholland Exp $");
|
1996-12-28 21:56:57 +03:00
|
|
|
#endif
|
1996-12-28 21:44:55 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <curses.h>
|
|
|
|
#include <err.h>
|
2010-03-29 06:46:05 +04:00
|
|
|
#include <limits.h>
|
1996-12-28 21:44:55 +03:00
|
|
|
#include <signal.h>
|
2009-06-04 10:27:47 +04:00
|
|
|
#include <stdarg.h>
|
1996-12-28 21:44:55 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-02-03 07:55:59 +03:00
|
|
|
#include <time.h>
|
1996-12-28 21:44:55 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gomoku.h"
|
|
|
|
|
|
|
|
#define USER 0 /* get input from standard input */
|
|
|
|
#define PROGRAM 1 /* get input from program */
|
|
|
|
#define INPUTF 2 /* get input from a file */
|
|
|
|
|
|
|
|
int interactive = 1; /* true if interactive */
|
|
|
|
int debug; /* true if debugging */
|
2009-08-12 10:19:17 +04:00
|
|
|
static int test; /* both moves come from 1: input, 2: computer */
|
|
|
|
static char *prog; /* name of program */
|
2010-03-29 07:51:55 +04:00
|
|
|
static char user[LOGIN_NAME_MAX]; /* name of player */
|
2009-08-12 10:19:17 +04:00
|
|
|
static FILE *debugfp; /* file for debug output */
|
|
|
|
static FILE *inputfp; /* file for debug input */
|
1996-12-28 21:44:55 +03:00
|
|
|
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
const char pdir[4] = "-\\|/";
|
1996-12-28 21:44:55 +03:00
|
|
|
|
|
|
|
struct spotstr board[BAREA]; /* info for board */
|
|
|
|
struct combostr frames[FAREA]; /* storage for all frames */
|
|
|
|
struct combostr *sortframes[2]; /* sorted list of non-empty frames */
|
|
|
|
u_char overlap[FAREA * FAREA]; /* true if frame [a][b] overlap */
|
|
|
|
short intersect[FAREA * FAREA]; /* frame [a][b] intersection */
|
|
|
|
int movelog[BSZ * BSZ]; /* log of all the moves */
|
|
|
|
int movenum; /* current move number */
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
const char *plyr[2]; /* who's who */
|
1996-12-28 21:44:55 +03:00
|
|
|
|
2009-08-12 10:19:17 +04:00
|
|
|
static int readinput(FILE *);
|
|
|
|
static void misclog(const char *, ...) __printflike(1, 2);
|
|
|
|
static void quit(void) __dead;
|
|
|
|
static void quitsig(int) __dead;
|
1996-12-28 21:44:55 +03:00
|
|
|
|
1997-10-10 17:36:01 +04:00
|
|
|
int
|
2009-06-04 09:27:04 +04:00
|
|
|
main(int argc, char **argv)
|
1996-12-28 21:44:55 +03:00
|
|
|
{
|
|
|
|
char buf[128];
|
2010-03-29 06:46:05 +04:00
|
|
|
char fname[PATH_MAX];
|
2010-03-29 07:51:55 +04:00
|
|
|
char *tmp;
|
1996-12-28 21:44:55 +03:00
|
|
|
int color, curmove, i, ch;
|
|
|
|
int input[2];
|
|
|
|
|
1999-09-12 13:02:20 +04:00
|
|
|
/* Revoke setgid privileges */
|
2000-05-08 11:55:59 +04:00
|
|
|
setgid(getgid());
|
1999-09-12 13:02:20 +04:00
|
|
|
|
2010-03-29 07:51:55 +04:00
|
|
|
tmp = getlogin();
|
|
|
|
if (tmp) {
|
|
|
|
strlcpy(user, tmp, sizeof(user));
|
|
|
|
} else {
|
|
|
|
strcpy(user, "you");
|
|
|
|
}
|
|
|
|
|
1997-10-10 17:36:01 +04:00
|
|
|
color = curmove = 0;
|
|
|
|
|
1996-12-28 21:44:55 +03:00
|
|
|
prog = strrchr(argv[0], '/');
|
|
|
|
if (prog)
|
|
|
|
prog++;
|
|
|
|
else
|
|
|
|
prog = argv[0];
|
|
|
|
|
1997-10-10 17:36:01 +04:00
|
|
|
while ((ch = getopt(argc, argv, "bcdD:u")) != -1) {
|
1996-12-28 21:44:55 +03:00
|
|
|
switch (ch) {
|
|
|
|
case 'b': /* background */
|
|
|
|
interactive = 0;
|
|
|
|
break;
|
|
|
|
case 'd': /* debugging */
|
|
|
|
debug++;
|
|
|
|
break;
|
|
|
|
case 'D': /* log debug output to file */
|
|
|
|
if ((debugfp = fopen(optarg, "w")) == NULL)
|
|
|
|
err(1, "%s", optarg);
|
|
|
|
break;
|
2010-03-29 06:48:17 +04:00
|
|
|
case 'u': /* testing: user versus user */
|
1996-12-28 21:44:55 +03:00
|
|
|
test = 1;
|
|
|
|
break;
|
2010-03-29 06:48:17 +04:00
|
|
|
case 'c': /* testing: computer versus computer */
|
1996-12-28 21:44:55 +03:00
|
|
|
test = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc) {
|
|
|
|
if ((inputfp = fopen(*argv, "r")) == NULL)
|
|
|
|
err(1, "%s", *argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!debug)
|
|
|
|
srandom(time(0));
|
|
|
|
if (interactive)
|
|
|
|
cursinit(); /* initialize curses */
|
|
|
|
again:
|
|
|
|
bdinit(board); /* initialize board contents */
|
|
|
|
|
|
|
|
if (interactive) {
|
|
|
|
plyr[BLACK] = plyr[WHITE] = "???";
|
|
|
|
bdisp_init(); /* initialize display of board */
|
|
|
|
#ifdef DEBUG
|
|
|
|
signal(SIGINT, whatsup);
|
|
|
|
#else
|
1997-10-10 17:36:01 +04:00
|
|
|
signal(SIGINT, quitsig);
|
1996-12-28 21:44:55 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (inputfp == NULL && test == 0) {
|
2010-03-29 08:28:47 +04:00
|
|
|
move(BSZ3, 0);
|
|
|
|
printw("Black moves first. ");
|
|
|
|
ask("(B)lack or (W)hite? ");
|
1996-12-28 21:44:55 +03:00
|
|
|
for (;;) {
|
2010-03-29 08:28:47 +04:00
|
|
|
ch = get_key(NULL);
|
2010-03-29 07:51:55 +04:00
|
|
|
if (ch == 'b' || ch == 'B') {
|
1996-12-28 21:44:55 +03:00
|
|
|
color = BLACK;
|
|
|
|
break;
|
|
|
|
}
|
2010-03-29 07:51:55 +04:00
|
|
|
if (ch == 'w' || ch == 'W') {
|
1996-12-28 21:44:55 +03:00
|
|
|
color = WHITE;
|
|
|
|
break;
|
|
|
|
}
|
2010-03-29 08:28:47 +04:00
|
|
|
if (ch == 'q' || ch == 'Q') {
|
|
|
|
quit();
|
|
|
|
}
|
|
|
|
beep();
|
|
|
|
ask("Please choose (B)lack or (W)hite: ");
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|
2010-03-29 07:51:55 +04:00
|
|
|
move(BSZ3, 0);
|
1996-12-28 21:44:55 +03:00
|
|
|
clrtoeol();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setbuf(stdout, 0);
|
2009-07-13 23:05:39 +04:00
|
|
|
get_line(buf, sizeof(buf));
|
1996-12-28 21:44:55 +03:00
|
|
|
if (strcmp(buf, "black") == 0)
|
|
|
|
color = BLACK;
|
|
|
|
else if (strcmp(buf, "white") == 0)
|
|
|
|
color = WHITE;
|
|
|
|
else {
|
2009-06-04 10:27:47 +04:00
|
|
|
panic("Huh? Expected `black' or `white', got `%s'\n",
|
1996-12-28 21:44:55 +03:00
|
|
|
buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputfp) {
|
|
|
|
input[BLACK] = INPUTF;
|
|
|
|
input[WHITE] = INPUTF;
|
|
|
|
} else {
|
|
|
|
switch (test) {
|
2010-03-29 06:48:17 +04:00
|
|
|
case 0: /* user versus program */
|
1996-12-28 21:44:55 +03:00
|
|
|
input[color] = USER;
|
|
|
|
input[!color] = PROGRAM;
|
|
|
|
break;
|
|
|
|
|
2010-03-29 06:48:17 +04:00
|
|
|
case 1: /* user versus user */
|
1996-12-28 21:44:55 +03:00
|
|
|
input[BLACK] = USER;
|
|
|
|
input[WHITE] = USER;
|
|
|
|
break;
|
|
|
|
|
2010-03-29 06:48:17 +04:00
|
|
|
case 2: /* program versus program */
|
1996-12-28 21:44:55 +03:00
|
|
|
input[BLACK] = PROGRAM;
|
|
|
|
input[WHITE] = PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (interactive) {
|
2010-03-29 07:51:55 +04:00
|
|
|
plyr[BLACK] = input[BLACK] == USER ? user : prog;
|
|
|
|
plyr[WHITE] = input[WHITE] == USER ? user : prog;
|
1996-12-28 21:44:55 +03:00
|
|
|
bdwho(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (color = BLACK; ; color = !color) {
|
|
|
|
top:
|
|
|
|
switch (input[color]) {
|
|
|
|
case INPUTF: /* input comes from a file */
|
|
|
|
curmove = readinput(inputfp);
|
|
|
|
if (curmove != ILLEGAL)
|
|
|
|
break;
|
|
|
|
switch (test) {
|
2010-03-29 06:48:17 +04:00
|
|
|
case 0: /* user versus program */
|
1996-12-28 21:44:55 +03:00
|
|
|
input[color] = USER;
|
|
|
|
input[!color] = PROGRAM;
|
|
|
|
break;
|
|
|
|
|
2010-03-29 06:48:17 +04:00
|
|
|
case 1: /* user versus user */
|
1996-12-28 21:44:55 +03:00
|
|
|
input[BLACK] = USER;
|
|
|
|
input[WHITE] = USER;
|
|
|
|
break;
|
|
|
|
|
2010-03-29 06:48:17 +04:00
|
|
|
case 2: /* program versus program */
|
1996-12-28 21:44:55 +03:00
|
|
|
input[BLACK] = PROGRAM;
|
|
|
|
input[WHITE] = PROGRAM;
|
|
|
|
break;
|
|
|
|
}
|
2010-03-29 07:51:55 +04:00
|
|
|
plyr[BLACK] = input[BLACK] == USER ? user : prog;
|
|
|
|
plyr[WHITE] = input[WHITE] == USER ? user : prog;
|
1996-12-28 21:44:55 +03:00
|
|
|
bdwho(1);
|
|
|
|
goto top;
|
|
|
|
|
|
|
|
case USER: /* input comes from standard input */
|
|
|
|
getinput:
|
|
|
|
if (interactive) {
|
2010-03-29 08:28:47 +04:00
|
|
|
ask("Select move, (S)ave or (Q)uit.");
|
2010-03-29 07:51:55 +04:00
|
|
|
curmove = get_coord();
|
1996-12-28 21:44:55 +03:00
|
|
|
if (curmove == SAVE) {
|
|
|
|
FILE *fp;
|
|
|
|
|
2010-03-29 08:28:47 +04:00
|
|
|
ask("Save file name? ");
|
2010-03-29 06:46:05 +04:00
|
|
|
(void)get_line(fname, sizeof(fname));
|
|
|
|
if ((fp = fopen(fname, "w")) == NULL) {
|
2009-06-04 10:27:47 +04:00
|
|
|
misclog("cannot create save file");
|
1996-12-28 21:44:55 +03:00
|
|
|
goto getinput;
|
|
|
|
}
|
|
|
|
for (i = 0; i < movenum - 1; i++)
|
|
|
|
fprintf(fp, "%s\n",
|
|
|
|
stoc(movelog[i]));
|
|
|
|
fclose(fp);
|
|
|
|
goto getinput;
|
|
|
|
}
|
|
|
|
if (curmove != RESIGN &&
|
|
|
|
board[curmove].s_occ != EMPTY) {
|
2010-03-29 07:51:55 +04:00
|
|
|
/*misclog("Illegal move");*/
|
|
|
|
beep();
|
1996-12-28 21:44:55 +03:00
|
|
|
goto getinput;
|
|
|
|
}
|
2010-03-29 07:51:55 +04:00
|
|
|
} else {
|
|
|
|
if (!get_line(buf, sizeof(buf))) {
|
|
|
|
curmove = RESIGN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (buf[0] == '\0')
|
|
|
|
goto getinput;
|
|
|
|
curmove = ctos(buf);
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROGRAM: /* input comes from the program */
|
2010-03-29 07:51:55 +04:00
|
|
|
if (interactive)
|
|
|
|
ask("Thinking...");
|
1996-12-28 21:44:55 +03:00
|
|
|
curmove = pickmove(color);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (interactive) {
|
2011-08-16 15:10:54 +04:00
|
|
|
misclog("%3d%s%-6s", movenum, color ? " " : " ",
|
|
|
|
stoc(curmove));
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|
|
|
|
if ((i = makemove(color, curmove)) != MOVEOK)
|
|
|
|
break;
|
|
|
|
if (interactive)
|
|
|
|
bdisp();
|
|
|
|
}
|
|
|
|
if (interactive) {
|
2010-03-29 07:51:55 +04:00
|
|
|
move(BSZ3, 0);
|
1996-12-28 21:44:55 +03:00
|
|
|
switch (i) {
|
|
|
|
case WIN:
|
|
|
|
if (input[color] == PROGRAM)
|
|
|
|
addstr("Ha ha, I won");
|
2010-03-29 07:51:55 +04:00
|
|
|
else if (input[0] == USER && input[1] == USER)
|
|
|
|
addstr("Well, you won (and lost)");
|
1996-12-28 21:44:55 +03:00
|
|
|
else
|
|
|
|
addstr("Rats! you won");
|
|
|
|
break;
|
|
|
|
case TIE:
|
2010-03-29 08:28:47 +04:00
|
|
|
addstr("Wow! It's a tie");
|
1996-12-28 21:44:55 +03:00
|
|
|
break;
|
|
|
|
case ILLEGAL:
|
|
|
|
addstr("Illegal move");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clrtoeol();
|
|
|
|
bdisp();
|
|
|
|
if (i != RESIGN) {
|
|
|
|
replay:
|
2010-03-29 08:28:47 +04:00
|
|
|
ask("Play again? ");
|
|
|
|
ch = get_key("YyNnQqSs");
|
|
|
|
if (ch == 'Y' || ch == 'y')
|
1996-12-28 21:44:55 +03:00
|
|
|
goto again;
|
2010-03-29 08:28:47 +04:00
|
|
|
if (ch == 'S') {
|
1996-12-28 21:44:55 +03:00
|
|
|
FILE *fp;
|
|
|
|
|
2010-03-29 08:28:47 +04:00
|
|
|
ask("Save file name? ");
|
2010-03-29 06:46:05 +04:00
|
|
|
(void)get_line(fname, sizeof(fname));
|
|
|
|
if ((fp = fopen(fname, "w")) == NULL) {
|
2009-06-04 10:27:47 +04:00
|
|
|
misclog("cannot create save file");
|
1996-12-28 21:44:55 +03:00
|
|
|
goto replay;
|
|
|
|
}
|
|
|
|
for (i = 0; i < movenum - 1; i++)
|
|
|
|
fprintf(fp, "%s\n",
|
|
|
|
stoc(movelog[i]));
|
|
|
|
fclose(fp);
|
|
|
|
goto replay;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
quit();
|
1997-10-10 17:36:01 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
return(0);
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|
|
|
|
|
2009-08-12 10:19:17 +04:00
|
|
|
static int
|
2009-06-04 09:27:04 +04:00
|
|
|
readinput(FILE *fp)
|
1996-12-28 21:44:55 +03:00
|
|
|
{
|
|
|
|
int c;
|
2009-06-04 10:47:36 +04:00
|
|
|
char buf[128];
|
|
|
|
size_t pos;
|
1996-12-28 21:44:55 +03:00
|
|
|
|
2009-06-04 10:47:36 +04:00
|
|
|
pos = 0;
|
|
|
|
while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1)
|
|
|
|
buf[pos++] = c;
|
|
|
|
buf[pos] = '\0';
|
|
|
|
return ctos(buf);
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/*
|
|
|
|
* Handle strange situations.
|
|
|
|
*/
|
|
|
|
void
|
2009-06-04 09:27:04 +04:00
|
|
|
whatsup(int signum)
|
1996-12-28 21:44:55 +03:00
|
|
|
{
|
2009-06-04 11:01:16 +04:00
|
|
|
int i, n, s1, s2, d1, d2;
|
1996-12-28 21:44:55 +03:00
|
|
|
struct spotstr *sp;
|
|
|
|
FILE *fp;
|
|
|
|
char *str;
|
|
|
|
struct elist *ep;
|
|
|
|
struct combostr *cbp;
|
2009-06-04 11:01:16 +04:00
|
|
|
char input[128];
|
|
|
|
char tmp[128];
|
1996-12-28 21:44:55 +03:00
|
|
|
|
|
|
|
if (!interactive)
|
|
|
|
quit();
|
|
|
|
top:
|
2010-03-29 08:28:47 +04:00
|
|
|
ask("debug command: ");
|
2009-07-13 23:05:39 +04:00
|
|
|
if (!get_line(input, sizeof(input)))
|
1996-12-28 21:44:55 +03:00
|
|
|
quit();
|
2009-06-04 11:01:16 +04:00
|
|
|
switch (*input) {
|
1996-12-28 21:44:55 +03:00
|
|
|
case '\0':
|
|
|
|
goto top;
|
|
|
|
case 'q': /* conservative quit */
|
|
|
|
quit();
|
|
|
|
case 'd': /* set debug level */
|
2009-06-04 11:01:16 +04:00
|
|
|
debug = input[1] - '0';
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog("Debug set to %d", debug);
|
1996-12-28 21:44:55 +03:00
|
|
|
sleep(1);
|
|
|
|
case 'c':
|
|
|
|
break;
|
|
|
|
case 'b': /* back up a move */
|
|
|
|
if (movenum > 1) {
|
|
|
|
movenum--;
|
|
|
|
board[movelog[movenum - 1]].s_occ = EMPTY;
|
|
|
|
bdisp();
|
|
|
|
}
|
|
|
|
goto top;
|
|
|
|
case 's': /* suggest a move */
|
2009-06-04 11:01:16 +04:00
|
|
|
i = input[1] == 'b' ? BLACK : WHITE;
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog("suggest %c %s", i == BLACK ? 'B' : 'W',
|
1996-12-28 21:44:55 +03:00
|
|
|
stoc(pickmove(i)));
|
|
|
|
goto top;
|
|
|
|
case 'f': /* go forward a move */
|
|
|
|
board[movelog[movenum - 1]].s_occ = movenum & 1 ? BLACK : WHITE;
|
|
|
|
movenum++;
|
|
|
|
bdisp();
|
|
|
|
goto top;
|
|
|
|
case 'l': /* print move history */
|
2009-06-04 11:01:16 +04:00
|
|
|
if (input[1] == '\0') {
|
1996-12-28 21:44:55 +03:00
|
|
|
for (i = 0; i < movenum - 1; i++)
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog("%s", stoc(movelog[i]));
|
1996-12-28 21:44:55 +03:00
|
|
|
goto top;
|
|
|
|
}
|
2009-06-04 11:01:16 +04:00
|
|
|
if ((fp = fopen(input + 1, "w")) == NULL)
|
1996-12-28 21:44:55 +03:00
|
|
|
goto top;
|
|
|
|
for (i = 0; i < movenum - 1; i++) {
|
|
|
|
fprintf(fp, "%s", stoc(movelog[i]));
|
|
|
|
if (++i < movenum - 1)
|
|
|
|
fprintf(fp, " %s\n", stoc(movelog[i]));
|
|
|
|
else
|
|
|
|
fputc('\n', fp);
|
|
|
|
}
|
|
|
|
bdump(fp);
|
|
|
|
fclose(fp);
|
|
|
|
goto top;
|
|
|
|
case 'o':
|
2009-06-04 11:01:16 +04:00
|
|
|
/* avoid use w/o initialization on invalid input */
|
|
|
|
d1 = s1 = 0;
|
|
|
|
|
1996-12-28 21:44:55 +03:00
|
|
|
n = 0;
|
2009-06-04 11:01:16 +04:00
|
|
|
for (str = input + 1; *str; str++)
|
1996-12-28 21:44:55 +03:00
|
|
|
if (*str == ',') {
|
|
|
|
for (d1 = 0; d1 < 4; d1++)
|
|
|
|
if (str[-1] == pdir[d1])
|
|
|
|
break;
|
|
|
|
str[-1] = '\0';
|
2009-06-04 11:01:16 +04:00
|
|
|
sp = &board[s1 = ctos(input + 1)];
|
1996-12-28 21:44:55 +03:00
|
|
|
n = (sp->s_frame[d1] - frames) * FAREA;
|
|
|
|
*str++ = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sp = &board[s2 = ctos(str)];
|
|
|
|
while (*str)
|
|
|
|
str++;
|
|
|
|
for (d2 = 0; d2 < 4; d2++)
|
|
|
|
if (str[-1] == pdir[d2])
|
|
|
|
break;
|
|
|
|
n += sp->s_frame[d2] - frames;
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog("overlap %s%c,%s%c = %x", stoc(s1), pdir[d1],
|
|
|
|
stoc(s2), pdir[d2], overlap[n]);
|
1996-12-28 21:44:55 +03:00
|
|
|
goto top;
|
|
|
|
case 'p':
|
2009-06-04 11:01:16 +04:00
|
|
|
sp = &board[i = ctos(input + 1)];
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i),
|
1996-12-28 21:44:55 +03:00
|
|
|
sp->s_combo[BLACK].s, sp->s_level[BLACK],
|
|
|
|
sp->s_nforce[BLACK],
|
|
|
|
sp->s_combo[WHITE].s, sp->s_level[WHITE],
|
2009-06-04 11:01:16 +04:00
|
|
|
sp->s_nforce[WHITE], sp->s_wval, sp->s_flags);
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog("FB %s %x %x %x %x", stoc(i),
|
1996-12-28 21:44:55 +03:00
|
|
|
sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
|
|
|
|
sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog("FW %s %x %x %x %x", stoc(i),
|
1996-12-28 21:44:55 +03:00
|
|
|
sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s,
|
|
|
|
sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s);
|
|
|
|
goto top;
|
|
|
|
case 'e': /* e {b|w} [0-9] spot */
|
2009-06-04 11:01:16 +04:00
|
|
|
str = input + 1;
|
1996-12-28 21:44:55 +03:00
|
|
|
if (*str >= '0' && *str <= '9')
|
|
|
|
n = *str++ - '0';
|
|
|
|
else
|
|
|
|
n = 0;
|
|
|
|
sp = &board[i = ctos(str)];
|
|
|
|
for (ep = sp->s_empty; ep; ep = ep->e_next) {
|
|
|
|
cbp = ep->e_combo;
|
|
|
|
if (n) {
|
|
|
|
if (cbp->c_nframes > n)
|
|
|
|
continue;
|
|
|
|
if (cbp->c_nframes != n)
|
|
|
|
break;
|
|
|
|
}
|
2009-06-04 11:01:16 +04:00
|
|
|
printcombo(cbp, tmp, sizeof(tmp));
|
|
|
|
debuglog("%s", tmp);
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|
|
|
|
goto top;
|
|
|
|
default:
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog("Options are:");
|
|
|
|
debuglog("q - quit");
|
|
|
|
debuglog("c - continue");
|
|
|
|
debuglog("d# - set debug level to #");
|
|
|
|
debuglog("p# - print values at #");
|
1996-12-28 21:44:55 +03:00
|
|
|
goto top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display debug info.
|
|
|
|
*/
|
1997-10-10 17:36:01 +04:00
|
|
|
void
|
2009-06-04 10:27:47 +04:00
|
|
|
debuglog(const char *fmt, ...)
|
1996-12-28 21:44:55 +03:00
|
|
|
{
|
2009-06-04 10:27:47 +04:00
|
|
|
va_list ap;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
1996-12-28 21:44:55 +03:00
|
|
|
|
|
|
|
if (debugfp)
|
2009-06-04 10:27:47 +04:00
|
|
|
fprintf(debugfp, "%s\n", buf);
|
1996-12-28 21:44:55 +03:00
|
|
|
if (interactive)
|
2009-06-04 10:27:47 +04:00
|
|
|
dislog(buf);
|
1996-12-28 21:44:55 +03:00
|
|
|
else
|
2009-06-04 10:27:47 +04:00
|
|
|
fprintf(stderr, "%s\n", buf);
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|
|
|
|
|
2009-08-12 10:19:17 +04:00
|
|
|
static void
|
2009-06-04 10:27:47 +04:00
|
|
|
misclog(const char *fmt, ...)
|
1996-12-28 21:44:55 +03:00
|
|
|
{
|
2009-06-04 10:27:47 +04:00
|
|
|
va_list ap;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
va_end(ap);
|
1996-12-28 21:44:55 +03:00
|
|
|
|
|
|
|
if (debugfp)
|
2009-06-04 10:27:47 +04:00
|
|
|
fprintf(debugfp, "%s\n", buf);
|
1996-12-28 21:44:55 +03:00
|
|
|
if (interactive)
|
2009-06-04 10:27:47 +04:00
|
|
|
dislog(buf);
|
1996-12-28 21:44:55 +03:00
|
|
|
else
|
2009-06-04 10:27:47 +04:00
|
|
|
printf("%s\n", buf);
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|
|
|
|
|
2009-08-12 10:19:17 +04:00
|
|
|
static void
|
2009-06-04 09:27:04 +04:00
|
|
|
quit(void)
|
1996-12-28 21:44:55 +03:00
|
|
|
{
|
|
|
|
if (interactive) {
|
|
|
|
bdisp(); /* show final board */
|
|
|
|
cursfini();
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2009-08-12 10:19:17 +04:00
|
|
|
static void
|
2009-06-04 09:27:04 +04:00
|
|
|
quitsig(int dummy __unused)
|
1997-10-10 17:36:01 +04:00
|
|
|
{
|
|
|
|
quit();
|
|
|
|
}
|
|
|
|
|
1996-12-28 21:44:55 +03:00
|
|
|
/*
|
|
|
|
* Die gracefully.
|
|
|
|
*/
|
1997-10-10 17:36:01 +04:00
|
|
|
void
|
2009-06-04 10:27:47 +04:00
|
|
|
panic(const char *fmt, ...)
|
1996-12-28 21:44:55 +03:00
|
|
|
{
|
2009-06-04 10:27:47 +04:00
|
|
|
va_list ap;
|
|
|
|
|
2016-06-12 05:15:26 +03:00
|
|
|
if (interactive) {
|
|
|
|
bdisp();
|
|
|
|
cursfini();
|
|
|
|
}
|
|
|
|
|
2009-06-04 10:27:47 +04:00
|
|
|
fprintf(stderr, "%s: ", prog);
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2016-06-12 05:15:26 +03:00
|
|
|
fputs("I resign\n", stdout);
|
|
|
|
exit(1);
|
1996-12-28 21:44:55 +03:00
|
|
|
}
|