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