main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /* $NetBSD: main.c,v 1.12 2004/01/27 20:30:29 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Ralph Campbell.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. #ifndef lint
  35. __COPYRIGHT("@(#) Copyright (c) 1994\n\
  36. The Regents of the University of California. All rights reserved.\n");
  37. #endif /* not lint */
  38. #ifndef lint
  39. #if 0
  40. static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
  41. #else
  42. __RCSID("$NetBSD: main.c,v 1.12 2004/01/27 20:30:29 jsm Exp $");
  43. #endif
  44. #endif /* not lint */
  45. #include <curses.h>
  46. #include <err.h>
  47. #include <signal.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <time.h>
  51. #include <unistd.h>
  52. #include "gomoku.h"
  53. #define USER 0 /* get input from standard input */
  54. #define PROGRAM 1 /* get input from program */
  55. #define INPUTF 2 /* get input from a file */
  56. int interactive = 1; /* true if interactive */
  57. int debug; /* true if debugging */
  58. int test; /* both moves come from 1: input, 2: computer */
  59. char *prog; /* name of program */
  60. FILE *debugfp; /* file for debug output */
  61. FILE *inputfp; /* file for debug input */
  62. const char pdir[4] = "-\\|/";
  63. char fmtbuf[128];
  64. struct spotstr board[BAREA]; /* info for board */
  65. struct combostr frames[FAREA]; /* storage for all frames */
  66. struct combostr *sortframes[2]; /* sorted list of non-empty frames */
  67. u_char overlap[FAREA * FAREA]; /* true if frame [a][b] overlap */
  68. short intersect[FAREA * FAREA]; /* frame [a][b] intersection */
  69. int movelog[BSZ * BSZ]; /* log of all the moves */
  70. int movenum; /* current move number */
  71. const char *plyr[2]; /* who's who */
  72. int main(int, char *[]);
  73. int
  74. main(argc, argv)
  75. int argc;
  76. char **argv;
  77. {
  78. char buf[128];
  79. int color, curmove, i, ch;
  80. int input[2];
  81. static const char *const fmt[2] = {
  82. "%3d %-6s",
  83. "%3d %-6s"
  84. };
  85. /* Revoke setgid privileges */
  86. setregid(getgid(), getgid());
  87. color = curmove = 0;
  88. prog = strrchr(argv[0], '/');
  89. if (prog)
  90. prog++;
  91. else
  92. prog = argv[0];
  93. while ((ch = getopt(argc, argv, "bcdD:u")) != -1) {
  94. switch (ch) {
  95. case 'b': /* background */
  96. interactive = 0;
  97. break;
  98. case 'd': /* debugging */
  99. debug++;
  100. break;
  101. case 'D': /* log debug output to file */
  102. if ((debugfp = fopen(optarg, "w")) == NULL)
  103. err(1, "%s", optarg);
  104. break;
  105. case 'u': /* testing: user verses user */
  106. test = 1;
  107. break;
  108. case 'c': /* testing: computer verses computer */
  109. test = 2;
  110. break;
  111. }
  112. }
  113. argc -= optind;
  114. argv += optind;
  115. if (argc) {
  116. if ((inputfp = fopen(*argv, "r")) == NULL)
  117. err(1, "%s", *argv);
  118. }
  119. if (!debug)
  120. #ifdef SVR4
  121. srand(time(0));
  122. #else
  123. srandom(time(0));
  124. #endif
  125. if (interactive)
  126. cursinit(); /* initialize curses */
  127. again:
  128. bdinit(board); /* initialize board contents */
  129. if (interactive) {
  130. plyr[BLACK] = plyr[WHITE] = "???";
  131. bdisp_init(); /* initialize display of board */
  132. #ifdef DEBUG
  133. signal(SIGINT, whatsup);
  134. #else
  135. signal(SIGINT, quitsig);
  136. #endif
  137. if (inputfp == NULL && test == 0) {
  138. for (;;) {
  139. ask("black or white? ");
  140. getline(buf, sizeof(buf));
  141. if (buf[0] == 'b' || buf[0] == 'B') {
  142. color = BLACK;
  143. break;
  144. }
  145. if (buf[0] == 'w' || buf[0] == 'W') {
  146. color = WHITE;
  147. break;
  148. }
  149. move(22, 0);
  150. printw("Black moves first. Please enter `black' or `white'\n");
  151. }
  152. move(22, 0);
  153. clrtoeol();
  154. }
  155. } else {
  156. setbuf(stdout, 0);
  157. getline(buf, sizeof(buf));
  158. if (strcmp(buf, "black") == 0)
  159. color = BLACK;
  160. else if (strcmp(buf, "white") == 0)
  161. color = WHITE;
  162. else {
  163. sprintf(fmtbuf,
  164. "Huh? Expected `black' or `white', got `%s'\n",
  165. buf);
  166. panic(fmtbuf);
  167. }
  168. }
  169. if (inputfp) {
  170. input[BLACK] = INPUTF;
  171. input[WHITE] = INPUTF;
  172. } else {
  173. switch (test) {
  174. case 0: /* user verses program */
  175. input[color] = USER;
  176. input[!color] = PROGRAM;
  177. break;
  178. case 1: /* user verses user */
  179. input[BLACK] = USER;
  180. input[WHITE] = USER;
  181. break;
  182. case 2: /* program verses program */
  183. input[BLACK] = PROGRAM;
  184. input[WHITE] = PROGRAM;
  185. break;
  186. }
  187. }
  188. if (interactive) {
  189. plyr[BLACK] = input[BLACK] == USER ? "you" : prog;
  190. plyr[WHITE] = input[WHITE] == USER ? "you" : prog;
  191. bdwho(1);
  192. }
  193. for (color = BLACK; ; color = !color) {
  194. top:
  195. switch (input[color]) {
  196. case INPUTF: /* input comes from a file */
  197. curmove = readinput(inputfp);
  198. if (curmove != ILLEGAL)
  199. break;
  200. switch (test) {
  201. case 0: /* user verses program */
  202. input[color] = USER;
  203. input[!color] = PROGRAM;
  204. break;
  205. case 1: /* user verses user */
  206. input[BLACK] = USER;
  207. input[WHITE] = USER;
  208. break;
  209. case 2: /* program verses program */
  210. input[BLACK] = PROGRAM;
  211. input[WHITE] = PROGRAM;
  212. break;
  213. }
  214. plyr[BLACK] = input[BLACK] == USER ? "you" : prog;
  215. plyr[WHITE] = input[WHITE] == USER ? "you" : prog;
  216. bdwho(1);
  217. goto top;
  218. case USER: /* input comes from standard input */
  219. getinput:
  220. if (interactive)
  221. ask("move? ");
  222. if (!getline(buf, sizeof(buf))) {
  223. curmove = RESIGN;
  224. break;
  225. }
  226. if (buf[0] == '\0')
  227. goto getinput;
  228. curmove = ctos(buf);
  229. if (interactive) {
  230. if (curmove == SAVE) {
  231. FILE *fp;
  232. ask("save file name? ");
  233. (void)getline(buf, sizeof(buf));
  234. if ((fp = fopen(buf, "w")) == NULL) {
  235. glog("cannot create save file");
  236. goto getinput;
  237. }
  238. for (i = 0; i < movenum - 1; i++)
  239. fprintf(fp, "%s\n",
  240. stoc(movelog[i]));
  241. fclose(fp);
  242. goto getinput;
  243. }
  244. if (curmove != RESIGN &&
  245. board[curmove].s_occ != EMPTY) {
  246. glog("Illegal move");
  247. goto getinput;
  248. }
  249. }
  250. break;
  251. case PROGRAM: /* input comes from the program */
  252. curmove = pickmove(color);
  253. break;
  254. }
  255. if (interactive) {
  256. sprintf(fmtbuf, fmt[color], movenum, stoc(curmove));
  257. glog(fmtbuf);
  258. }
  259. if ((i = makemove(color, curmove)) != MOVEOK)
  260. break;
  261. if (interactive)
  262. bdisp();
  263. }
  264. if (interactive) {
  265. move(22, 0);
  266. switch (i) {
  267. case WIN:
  268. if (input[color] == PROGRAM)
  269. addstr("Ha ha, I won");
  270. else
  271. addstr("Rats! you won");
  272. break;
  273. case TIE:
  274. addstr("Wow! its a tie");
  275. break;
  276. case ILLEGAL:
  277. addstr("Illegal move");
  278. break;
  279. }
  280. clrtoeol();
  281. bdisp();
  282. if (i != RESIGN) {
  283. replay:
  284. ask("replay? ");
  285. if (getline(buf, sizeof(buf)) &&
  286. (buf[0] == 'y' || buf[0] == 'Y'))
  287. goto again;
  288. if (strcmp(buf, "save") == 0) {
  289. FILE *fp;
  290. ask("save file name? ");
  291. (void)getline(buf, sizeof(buf));
  292. if ((fp = fopen(buf, "w")) == NULL) {
  293. glog("cannot create save file");
  294. goto replay;
  295. }
  296. for (i = 0; i < movenum - 1; i++)
  297. fprintf(fp, "%s\n",
  298. stoc(movelog[i]));
  299. fclose(fp);
  300. goto replay;
  301. }
  302. }
  303. }
  304. quit();
  305. /* NOTREACHED */
  306. return(0);
  307. }
  308. int
  309. readinput(fp)
  310. FILE *fp;
  311. {
  312. char *cp;
  313. int c;
  314. cp = fmtbuf;
  315. while ((c = getc(fp)) != EOF && c != '\n')
  316. *cp++ = c;
  317. *cp = '\0';
  318. return (ctos(fmtbuf));
  319. }
  320. #ifdef DEBUG
  321. /*
  322. * Handle strange situations.
  323. */
  324. void
  325. whatsup(signum)
  326. int signum;
  327. {
  328. int i, pnum, n, s1, s2, d1, d2;
  329. struct spotstr *sp;
  330. FILE *fp;
  331. char *str;
  332. struct elist *ep;
  333. struct combostr *cbp;
  334. if (!interactive)
  335. quit();
  336. top:
  337. ask("cmd? ");
  338. if (!getline(fmtbuf, sizeof(fmtbuf)))
  339. quit();
  340. switch (*fmtbuf) {
  341. case '\0':
  342. goto top;
  343. case 'q': /* conservative quit */
  344. quit();
  345. case 'd': /* set debug level */
  346. debug = fmtbuf[1] - '0';
  347. sprintf(fmtbuf, "Debug set to %d", debug);
  348. dlog(fmtbuf);
  349. sleep(1);
  350. case 'c':
  351. break;
  352. case 'b': /* back up a move */
  353. if (movenum > 1) {
  354. movenum--;
  355. board[movelog[movenum - 1]].s_occ = EMPTY;
  356. bdisp();
  357. }
  358. goto top;
  359. case 's': /* suggest a move */
  360. i = fmtbuf[1] == 'b' ? BLACK : WHITE;
  361. sprintf(fmtbuf, "suggest %c %s", i == BLACK ? 'B' : 'W',
  362. stoc(pickmove(i)));
  363. dlog(fmtbuf);
  364. goto top;
  365. case 'f': /* go forward a move */
  366. board[movelog[movenum - 1]].s_occ = movenum & 1 ? BLACK : WHITE;
  367. movenum++;
  368. bdisp();
  369. goto top;
  370. case 'l': /* print move history */
  371. if (fmtbuf[1] == '\0') {
  372. for (i = 0; i < movenum - 1; i++)
  373. dlog(stoc(movelog[i]));
  374. goto top;
  375. }
  376. if ((fp = fopen(fmtbuf + 1, "w")) == NULL)
  377. goto top;
  378. for (i = 0; i < movenum - 1; i++) {
  379. fprintf(fp, "%s", stoc(movelog[i]));
  380. if (++i < movenum - 1)
  381. fprintf(fp, " %s\n", stoc(movelog[i]));
  382. else
  383. fputc('\n', fp);
  384. }
  385. bdump(fp);
  386. fclose(fp);
  387. goto top;
  388. case 'o':
  389. n = 0;
  390. for (str = fmtbuf + 1; *str; str++)
  391. if (*str == ',') {
  392. for (d1 = 0; d1 < 4; d1++)
  393. if (str[-1] == pdir[d1])
  394. break;
  395. str[-1] = '\0';
  396. sp = &board[s1 = ctos(fmtbuf + 1)];
  397. n = (sp->s_frame[d1] - frames) * FAREA;
  398. *str++ = '\0';
  399. break;
  400. }
  401. sp = &board[s2 = ctos(str)];
  402. while (*str)
  403. str++;
  404. for (d2 = 0; d2 < 4; d2++)
  405. if (str[-1] == pdir[d2])
  406. break;
  407. n += sp->s_frame[d2] - frames;
  408. str = fmtbuf;
  409. sprintf(str, "overlap %s%c,", stoc(s1), pdir[d1]);
  410. str += strlen(str);
  411. sprintf(str, "%s%c = %x", stoc(s2), pdir[d2], overlap[n]);
  412. dlog(fmtbuf);
  413. goto top;
  414. case 'p':
  415. sp = &board[i = ctos(fmtbuf + 1)];
  416. sprintf(fmtbuf, "V %s %x/%d %d %x/%d %d %d %x", stoc(i),
  417. sp->s_combo[BLACK].s, sp->s_level[BLACK],
  418. sp->s_nforce[BLACK],
  419. sp->s_combo[WHITE].s, sp->s_level[WHITE],
  420. sp->s_nforce[WHITE], sp->s_wval, sp->s_flg);
  421. dlog(fmtbuf);
  422. sprintf(fmtbuf, "FB %s %x %x %x %x", stoc(i),
  423. sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
  424. sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
  425. dlog(fmtbuf);
  426. sprintf(fmtbuf, "FW %s %x %x %x %x", stoc(i),
  427. sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s,
  428. sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s);
  429. dlog(fmtbuf);
  430. goto top;
  431. case 'e': /* e {b|w} [0-9] spot */
  432. str = fmtbuf + 1;
  433. if (*str >= '0' && *str <= '9')
  434. n = *str++ - '0';
  435. else
  436. n = 0;
  437. sp = &board[i = ctos(str)];
  438. for (ep = sp->s_empty; ep; ep = ep->e_next) {
  439. cbp = ep->e_combo;
  440. if (n) {
  441. if (cbp->c_nframes > n)
  442. continue;
  443. if (cbp->c_nframes != n)
  444. break;
  445. }
  446. printcombo(cbp, fmtbuf);
  447. dlog(fmtbuf);
  448. }
  449. goto top;
  450. default:
  451. syntax:
  452. dlog("Options are:");
  453. dlog("q - quit");
  454. dlog("c - continue");
  455. dlog("d# - set debug level to #");
  456. dlog("p# - print values at #");
  457. goto top;
  458. }
  459. }
  460. #endif /* DEBUG */
  461. /*
  462. * Display debug info.
  463. */
  464. void
  465. dlog(str)
  466. const char *str;
  467. {
  468. if (debugfp)
  469. fprintf(debugfp, "%s\n", str);
  470. if (interactive)
  471. dislog(str);
  472. else
  473. fprintf(stderr, "%s\n", str);
  474. }
  475. void
  476. glog(str)
  477. const char *str;
  478. {
  479. if (debugfp)
  480. fprintf(debugfp, "%s\n", str);
  481. if (interactive)
  482. dislog(str);
  483. else
  484. printf("%s\n", str);
  485. }
  486. void
  487. quit()
  488. {
  489. if (interactive) {
  490. bdisp(); /* show final board */
  491. cursfini();
  492. }
  493. exit(0);
  494. }
  495. void
  496. quitsig(dummy)
  497. int dummy __attribute__((__unused__));
  498. {
  499. quit();
  500. }
  501. /*
  502. * Die gracefully.
  503. */
  504. void
  505. panic(str)
  506. const char *str;
  507. {
  508. fprintf(stderr, "%s: %s\n", prog, str);
  509. fputs("resign\n", stdout);
  510. quit();
  511. }