#include <sys/param.h>
#include <curses.h>
#include <err.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "gomoku.h"
#define USER 0
#define PROGRAM 1
#define INPUTF 2
int interactive = 1;
int debug;
static int test;
static char *prog;
static char user[MAXLOGNAME];
static FILE *debugfp;
static FILE *inputfp;
const char pdir[4] = {'-', '\\', '|', '/'};
struct spotstr board[BAREA];
struct combostr frames[FAREA];
struct combostr *sortframes[2];
u_char overlap[FAREA * FAREA];
short intersect[FAREA * FAREA];
int movelog[BSZ * BSZ];
int movenum;
const char *plyr[2];
static int readinput(FILE *);
static void misclog(const char *, ...) __printflike(1, 2);
static void quit(void) __dead2;
static void quitsig(int) __dead2;
int
main(int argc, char **argv)
{
char buf[128];
char fname[PATH_MAX];
char *tmp;
int color, curmove, i, ch;
int input[2];
setgid(getgid());
tmp = getlogin();
if (tmp) {
strlcpy(user, tmp, sizeof(user));
} else {
strcpy(user, "you");
}
color = curmove = 0;
prog = strrchr(argv[0], '/');
if (prog)
prog++;
else
prog = argv[0];
while ((ch = getopt(argc, argv, "bcdD:u")) != -1) {
switch (ch) {
case 'b':
interactive = 0;
break;
case 'd':
debug++;
break;
case 'D':
if ((debugfp = fopen(optarg, "w")) == NULL)
err(1, "%s", optarg);
break;
case 'u':
test = 1;
break;
case 'c':
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();
again:
bdinit(board);
if (interactive) {
plyr[BLACK] = plyr[WHITE] = "???";
bdisp_init();
#ifdef DEBUG
signal(SIGINT, whatsup);
#else
signal(SIGINT, quitsig);
#endif
if (inputfp == NULL && test == 0) {
move(BSZ3, 0);
printw("Black moves first. ");
ask("(B)lack or (W)hite? ");
for (;;) {
ch = get_key(NULL);
if (ch == 'b' || ch == 'B') {
color = BLACK;
break;
}
if (ch == 'w' || ch == 'W') {
color = WHITE;
break;
}
if (ch == 'q' || ch == 'Q') {
quit();
}
beep();
ask("Please choose (B)lack or (W)hite: ");
}
move(BSZ3, 0);
clrtoeol();
}
} else {
setbuf(stdout, 0);
get_line(buf, sizeof(buf));
if (strcmp(buf, "black") == 0)
color = BLACK;
else if (strcmp(buf, "white") == 0)
color = WHITE;
else {
panic("Huh? Expected `black' or `white', got `%s'\n",
buf);
}
}
if (inputfp) {
input[BLACK] = INPUTF;
input[WHITE] = INPUTF;
} else {
switch (test) {
case 0:
input[color] = USER;
input[!color] = PROGRAM;
break;
case 1:
input[BLACK] = USER;
input[WHITE] = USER;
break;
case 2:
input[BLACK] = PROGRAM;
input[WHITE] = PROGRAM;
break;
}
}
if (interactive) {
plyr[BLACK] = input[BLACK] == USER ? user : prog;
plyr[WHITE] = input[WHITE] == USER ? user : prog;
bdwho(1);
}
for (color = BLACK; ; color = !color) {
top:
switch (input[color]) {
case INPUTF:
curmove = readinput(inputfp);
if (curmove != ILLEGAL)
break;
switch (test) {
case 0:
input[color] = USER;
input[!color] = PROGRAM;
break;
case 1:
input[BLACK] = USER;
input[WHITE] = USER;
break;
case 2:
input[BLACK] = PROGRAM;
input[WHITE] = PROGRAM;
break;
}
plyr[BLACK] = input[BLACK] == USER ? user : prog;
plyr[WHITE] = input[WHITE] == USER ? user : prog;
bdwho(1);
goto top;
case USER:
getinput:
if (interactive) {
ask("Select move, (S)ave or (Q)uit.");
curmove = get_coord();
if (curmove == SAVE) {
FILE *fp;
ask("Save file name? ");
(void)get_line(fname, sizeof(fname));
if ((fp = fopen(fname, "w")) == NULL) {
misclog("cannot create save file");
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) {
beep();
goto getinput;
}
} else {
if (!get_line(buf, sizeof(buf))) {
curmove = RESIGN;
break;
}
if (buf[0] == '\0')
goto getinput;
curmove = ctos(buf);
}
break;
case PROGRAM:
if (interactive)
ask("Thinking...");
curmove = pickmove(color);
break;
}
if (interactive) {
misclog("%3d%s%-6s", movenum, color ? " " : " ",
stoc(curmove));
}
if ((i = makemove(color, curmove)) != MOVEOK)
break;
if (interactive)
bdisp();
}
if (interactive) {
move(BSZ3, 0);
switch (i) {
case WIN:
if (input[color] == PROGRAM)
addstr("Ha ha, I won");
else if (input[0] == USER && input[1] == USER)
addstr("Well, you won (and lost)");
else
addstr("Rats! you won");
break;
case TIE:
addstr("Wow! It's a tie");
break;
case ILLEGAL:
addstr("Illegal move");
break;
}
clrtoeol();
bdisp();
if (i != RESIGN) {
replay:
ask("Play again? ");
ch = get_key("YyNnQqSs");
if (ch == 'Y' || ch == 'y')
goto again;
if (ch == 'S') {
FILE *fp;
ask("Save file name? ");
(void)get_line(fname, sizeof(fname));
if ((fp = fopen(fname, "w")) == NULL) {
misclog("cannot create save file");
goto replay;
}
for (i = 0; i < movenum - 1; i++)
fprintf(fp, "%s\n",
stoc(movelog[i]));
fclose(fp);
goto replay;
}
}
}
quit();
return(0);
}
static int
readinput(FILE *fp)
{
int c;
char buf[128];
size_t pos;
pos = 0;
while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1)
buf[pos++] = c;
buf[pos] = '\0';
return ctos(buf);
}
#ifdef DEBUG
void
whatsup(int signum)
{
int i, n, s1, s2, d1, d2;
struct spotstr *sp;
FILE *fp;
char *str;
struct elist *ep;
struct combostr *cbp;
char input[128];
char tmp[128];
if (!interactive)
quit();
top:
ask("debug command: ");
if (!get_line(input, sizeof(input)))
quit();
switch (*input) {
case '\0':
goto top;
case 'q':
quit();
case 'd':
debug = input[1] - '0';
debuglog("Debug set to %d", debug);
sleep(1);
case 'c':
break;
case 'b':
if (movenum > 1) {
movenum--;
board[movelog[movenum - 1]].s_occ = EMPTY;
bdisp();
}
goto top;
case 's':
i = input[1] == 'b' ? BLACK : WHITE;
debuglog("suggest %c %s", i == BLACK ? 'B' : 'W',
stoc(pickmove(i)));
goto top;
case 'f':
board[movelog[movenum - 1]].s_occ = movenum & 1 ? BLACK : WHITE;
movenum++;
bdisp();
goto top;
case 'l':
if (input[1] == '\0') {
for (i = 0; i < movenum - 1; i++)
debuglog("%s", stoc(movelog[i]));
goto top;
}
if ((fp = fopen(input + 1, "w")) == NULL)
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':
d1 = s1 = 0;
n = 0;
for (str = input + 1; *str; str++)
if (*str == ',') {
for (d1 = 0; d1 < 4; d1++)
if (str[-1] == pdir[d1])
break;
str[-1] = '\0';
sp = &board[s1 = ctos(input + 1)];
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;
debuglog("overlap %s%c,%s%c = %x", stoc(s1), pdir[d1],
stoc(s2), pdir[d2], overlap[n]);
goto top;
case 'p':
sp = &board[i = ctos(input + 1)];
debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i),
sp->s_combo[BLACK].s, sp->s_level[BLACK],
sp->s_nforce[BLACK],
sp->s_combo[WHITE].s, sp->s_level[WHITE],
sp->s_nforce[WHITE], sp->s_wval, sp->s_flags);
debuglog("FB %s %x %x %x %x", stoc(i),
sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
debuglog("FW %s %x %x %x %x", stoc(i),
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':
str = input + 1;
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;
}
printcombo(cbp, tmp, sizeof(tmp));
debuglog("%s", tmp);
}
goto top;
default:
debuglog("Options are:");
debuglog("q - quit");
debuglog("c - continue");
debuglog("d# - set debug level to #");
debuglog("p# - print values at #");
goto top;
}
}
#endif
void
debuglog(const char *fmt, ...)
{
va_list ap;
char buf[128];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (debugfp)
fprintf(debugfp, "%s\n", buf);
if (interactive)
dislog(buf);
else
fprintf(stderr, "%s\n", buf);
}
static void
misclog(const char *fmt, ...)
{
va_list ap;
char buf[128];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (debugfp)
fprintf(debugfp, "%s\n", buf);
if (interactive)
dislog(buf);
else
printf("%s\n", buf);
}
static void
quit(void)
{
if (interactive) {
bdisp();
cursfini();
}
exit(0);
}
static void
quitsig(int dummy __unused)
{
quit();
}
void
panic(const char *fmt, ...)
{
va_list ap;
if (interactive) {
bdisp();
cursfini();
}
fprintf(stderr, "%s: ", prog);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
fputs("I resign\n", stdout);
exit(1);
}