]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/main.c
Allow saved game filenames up to PATH_MAX. From OpenBSD.
[bsdgames-darwin.git] / gomoku / main.c
1 /* $NetBSD: main.c,v 1.21 2010/03/29 02:46:05 dholland 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.21 2010/03/29 02:46:05 dholland 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 = 1; /* true if interactive */
66 int debug; /* true if debugging */
67 static int test; /* both moves come from 1: input, 2: computer */
68 static char *prog; /* name of program */
69 static FILE *debugfp; /* file for debug output */
70 static FILE *inputfp; /* file for debug input */
71
72 const char pdir[4] = "-\\|/";
73
74 struct spotstr board[BAREA]; /* info for board */
75 struct combostr frames[FAREA]; /* storage for all frames */
76 struct combostr *sortframes[2]; /* sorted list of non-empty frames */
77 u_char overlap[FAREA * FAREA]; /* true if frame [a][b] overlap */
78 short intersect[FAREA * FAREA]; /* frame [a][b] intersection */
79 int movelog[BSZ * BSZ]; /* log of all the moves */
80 int movenum; /* current move number */
81 const char *plyr[2]; /* who's who */
82
83 static int readinput(FILE *);
84 static void misclog(const char *, ...) __printflike(1, 2);
85 static void quit(void) __dead;
86 static void quitsig(int) __dead;
87
88 int
89 main(int argc, char **argv)
90 {
91 char buf[128];
92 char fname[PATH_MAX];
93 int color, curmove, i, ch;
94 int input[2];
95 static const char *const fmt[2] = {
96 "%3d %-6s",
97 "%3d %-6s"
98 };
99
100 /* Revoke setgid privileges */
101 setgid(getgid());
102
103 color = curmove = 0;
104
105 prog = strrchr(argv[0], '/');
106 if (prog)
107 prog++;
108 else
109 prog = argv[0];
110
111 while ((ch = getopt(argc, argv, "bcdD:u")) != -1) {
112 switch (ch) {
113 case 'b': /* background */
114 interactive = 0;
115 break;
116 case 'd': /* debugging */
117 debug++;
118 break;
119 case 'D': /* log debug output to file */
120 if ((debugfp = fopen(optarg, "w")) == NULL)
121 err(1, "%s", optarg);
122 break;
123 case 'u': /* testing: user verses user */
124 test = 1;
125 break;
126 case 'c': /* testing: computer verses computer */
127 test = 2;
128 break;
129 }
130 }
131 argc -= optind;
132 argv += optind;
133 if (argc) {
134 if ((inputfp = fopen(*argv, "r")) == NULL)
135 err(1, "%s", *argv);
136 }
137
138 if (!debug)
139 #ifdef SVR4
140 srand(time(0));
141 #else
142 srandom(time(0));
143 #endif
144 if (interactive)
145 cursinit(); /* initialize curses */
146 again:
147 bdinit(board); /* initialize board contents */
148
149 if (interactive) {
150 plyr[BLACK] = plyr[WHITE] = "???";
151 bdisp_init(); /* initialize display of board */
152 #ifdef DEBUG
153 signal(SIGINT, whatsup);
154 #else
155 signal(SIGINT, quitsig);
156 #endif
157
158 if (inputfp == NULL && test == 0) {
159 for (;;) {
160 ask("black or white? ");
161 get_line(buf, sizeof(buf));
162 if (buf[0] == 'b' || buf[0] == 'B') {
163 color = BLACK;
164 break;
165 }
166 if (buf[0] == 'w' || buf[0] == 'W') {
167 color = WHITE;
168 break;
169 }
170 move(22, 0);
171 printw("Black moves first. Please enter `black' or `white'\n");
172 }
173 move(22, 0);
174 clrtoeol();
175 }
176 } else {
177 setbuf(stdout, 0);
178 get_line(buf, sizeof(buf));
179 if (strcmp(buf, "black") == 0)
180 color = BLACK;
181 else if (strcmp(buf, "white") == 0)
182 color = WHITE;
183 else {
184 panic("Huh? Expected `black' or `white', got `%s'\n",
185 buf);
186 }
187 }
188
189 if (inputfp) {
190 input[BLACK] = INPUTF;
191 input[WHITE] = INPUTF;
192 } else {
193 switch (test) {
194 case 0: /* user verses program */
195 input[color] = USER;
196 input[!color] = PROGRAM;
197 break;
198
199 case 1: /* user verses user */
200 input[BLACK] = USER;
201 input[WHITE] = USER;
202 break;
203
204 case 2: /* program verses program */
205 input[BLACK] = PROGRAM;
206 input[WHITE] = PROGRAM;
207 break;
208 }
209 }
210 if (interactive) {
211 plyr[BLACK] = input[BLACK] == USER ? "you" : prog;
212 plyr[WHITE] = input[WHITE] == USER ? "you" : prog;
213 bdwho(1);
214 }
215
216 for (color = BLACK; ; color = !color) {
217 top:
218 switch (input[color]) {
219 case INPUTF: /* input comes from a file */
220 curmove = readinput(inputfp);
221 if (curmove != ILLEGAL)
222 break;
223 switch (test) {
224 case 0: /* user verses program */
225 input[color] = USER;
226 input[!color] = PROGRAM;
227 break;
228
229 case 1: /* user verses user */
230 input[BLACK] = USER;
231 input[WHITE] = USER;
232 break;
233
234 case 2: /* program verses program */
235 input[BLACK] = PROGRAM;
236 input[WHITE] = PROGRAM;
237 break;
238 }
239 plyr[BLACK] = input[BLACK] == USER ? "you" : prog;
240 plyr[WHITE] = input[WHITE] == USER ? "you" : prog;
241 bdwho(1);
242 goto top;
243
244 case USER: /* input comes from standard input */
245 getinput:
246 if (interactive)
247 ask("move? ");
248 if (!get_line(buf, sizeof(buf))) {
249 curmove = RESIGN;
250 break;
251 }
252 if (buf[0] == '\0')
253 goto getinput;
254 curmove = ctos(buf);
255 if (interactive) {
256 if (curmove == SAVE) {
257 FILE *fp;
258
259 ask("save file name? ");
260 (void)get_line(fname, sizeof(fname));
261 if ((fp = fopen(fname, "w")) == NULL) {
262 misclog("cannot create save file");
263 goto getinput;
264 }
265 for (i = 0; i < movenum - 1; i++)
266 fprintf(fp, "%s\n",
267 stoc(movelog[i]));
268 fclose(fp);
269 goto getinput;
270 }
271 if (curmove != RESIGN &&
272 board[curmove].s_occ != EMPTY) {
273 misclog("Illegal move");
274 goto getinput;
275 }
276 }
277 break;
278
279 case PROGRAM: /* input comes from the program */
280 curmove = pickmove(color);
281 break;
282 }
283 if (interactive) {
284 misclog(fmt[color], movenum, stoc(curmove));
285 }
286 if ((i = makemove(color, curmove)) != MOVEOK)
287 break;
288 if (interactive)
289 bdisp();
290 }
291 if (interactive) {
292 move(22, 0);
293 switch (i) {
294 case WIN:
295 if (input[color] == PROGRAM)
296 addstr("Ha ha, I won");
297 else
298 addstr("Rats! you won");
299 break;
300 case TIE:
301 addstr("Wow! its a tie");
302 break;
303 case ILLEGAL:
304 addstr("Illegal move");
305 break;
306 }
307 clrtoeol();
308 bdisp();
309 if (i != RESIGN) {
310 replay:
311 ask("replay? ");
312 if (get_line(buf, sizeof(buf)) &&
313 (buf[0] == 'y' || buf[0] == 'Y'))
314 goto again;
315 if (strcmp(buf, "save") == 0) {
316 FILE *fp;
317
318 ask("save file name? ");
319 (void)get_line(fname, sizeof(fname));
320 if ((fp = fopen(fname, "w")) == NULL) {
321 misclog("cannot create save file");
322 goto replay;
323 }
324 for (i = 0; i < movenum - 1; i++)
325 fprintf(fp, "%s\n",
326 stoc(movelog[i]));
327 fclose(fp);
328 goto replay;
329 }
330 }
331 }
332 quit();
333 /* NOTREACHED */
334 return(0);
335 }
336
337 static int
338 readinput(FILE *fp)
339 {
340 int c;
341 char buf[128];
342 size_t pos;
343
344 pos = 0;
345 while ((c = getc(fp)) != EOF && c != '\n' && pos < sizeof(buf) - 1)
346 buf[pos++] = c;
347 buf[pos] = '\0';
348 return ctos(buf);
349 }
350
351 #ifdef DEBUG
352 /*
353 * Handle strange situations.
354 */
355 void
356 whatsup(int signum)
357 {
358 int i, n, s1, s2, d1, d2;
359 struct spotstr *sp;
360 FILE *fp;
361 char *str;
362 struct elist *ep;
363 struct combostr *cbp;
364 char input[128];
365 char tmp[128];
366
367 if (!interactive)
368 quit();
369 top:
370 ask("cmd? ");
371 if (!get_line(input, sizeof(input)))
372 quit();
373 switch (*input) {
374 case '\0':
375 goto top;
376 case 'q': /* conservative quit */
377 quit();
378 case 'd': /* set debug level */
379 debug = input[1] - '0';
380 debuglog("Debug set to %d", debug);
381 sleep(1);
382 case 'c':
383 break;
384 case 'b': /* back up a move */
385 if (movenum > 1) {
386 movenum--;
387 board[movelog[movenum - 1]].s_occ = EMPTY;
388 bdisp();
389 }
390 goto top;
391 case 's': /* suggest a move */
392 i = input[1] == 'b' ? BLACK : WHITE;
393 debuglog("suggest %c %s", i == BLACK ? 'B' : 'W',
394 stoc(pickmove(i)));
395 goto top;
396 case 'f': /* go forward a move */
397 board[movelog[movenum - 1]].s_occ = movenum & 1 ? BLACK : WHITE;
398 movenum++;
399 bdisp();
400 goto top;
401 case 'l': /* print move history */
402 if (input[1] == '\0') {
403 for (i = 0; i < movenum - 1; i++)
404 debuglog("%s", stoc(movelog[i]));
405 goto top;
406 }
407 if ((fp = fopen(input + 1, "w")) == NULL)
408 goto top;
409 for (i = 0; i < movenum - 1; i++) {
410 fprintf(fp, "%s", stoc(movelog[i]));
411 if (++i < movenum - 1)
412 fprintf(fp, " %s\n", stoc(movelog[i]));
413 else
414 fputc('\n', fp);
415 }
416 bdump(fp);
417 fclose(fp);
418 goto top;
419 case 'o':
420 /* avoid use w/o initialization on invalid input */
421 d1 = s1 = 0;
422
423 n = 0;
424 for (str = input + 1; *str; str++)
425 if (*str == ',') {
426 for (d1 = 0; d1 < 4; d1++)
427 if (str[-1] == pdir[d1])
428 break;
429 str[-1] = '\0';
430 sp = &board[s1 = ctos(input + 1)];
431 n = (sp->s_frame[d1] - frames) * FAREA;
432 *str++ = '\0';
433 break;
434 }
435 sp = &board[s2 = ctos(str)];
436 while (*str)
437 str++;
438 for (d2 = 0; d2 < 4; d2++)
439 if (str[-1] == pdir[d2])
440 break;
441 n += sp->s_frame[d2] - frames;
442 debuglog("overlap %s%c,%s%c = %x", stoc(s1), pdir[d1],
443 stoc(s2), pdir[d2], overlap[n]);
444 goto top;
445 case 'p':
446 sp = &board[i = ctos(input + 1)];
447 debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i),
448 sp->s_combo[BLACK].s, sp->s_level[BLACK],
449 sp->s_nforce[BLACK],
450 sp->s_combo[WHITE].s, sp->s_level[WHITE],
451 sp->s_nforce[WHITE], sp->s_wval, sp->s_flags);
452 debuglog("FB %s %x %x %x %x", stoc(i),
453 sp->s_fval[BLACK][0].s, sp->s_fval[BLACK][1].s,
454 sp->s_fval[BLACK][2].s, sp->s_fval[BLACK][3].s);
455 debuglog("FW %s %x %x %x %x", stoc(i),
456 sp->s_fval[WHITE][0].s, sp->s_fval[WHITE][1].s,
457 sp->s_fval[WHITE][2].s, sp->s_fval[WHITE][3].s);
458 goto top;
459 case 'e': /* e {b|w} [0-9] spot */
460 str = input + 1;
461 if (*str >= '0' && *str <= '9')
462 n = *str++ - '0';
463 else
464 n = 0;
465 sp = &board[i = ctos(str)];
466 for (ep = sp->s_empty; ep; ep = ep->e_next) {
467 cbp = ep->e_combo;
468 if (n) {
469 if (cbp->c_nframes > n)
470 continue;
471 if (cbp->c_nframes != n)
472 break;
473 }
474 printcombo(cbp, tmp, sizeof(tmp));
475 debuglog("%s", tmp);
476 }
477 goto top;
478 default:
479 debuglog("Options are:");
480 debuglog("q - quit");
481 debuglog("c - continue");
482 debuglog("d# - set debug level to #");
483 debuglog("p# - print values at #");
484 goto top;
485 }
486 }
487 #endif /* DEBUG */
488
489 /*
490 * Display debug info.
491 */
492 void
493 debuglog(const char *fmt, ...)
494 {
495 va_list ap;
496 char buf[128];
497
498 va_start(ap, fmt);
499 vsnprintf(buf, sizeof(buf), fmt, ap);
500 va_end(ap);
501
502 if (debugfp)
503 fprintf(debugfp, "%s\n", buf);
504 if (interactive)
505 dislog(buf);
506 else
507 fprintf(stderr, "%s\n", buf);
508 }
509
510 static void
511 misclog(const char *fmt, ...)
512 {
513 va_list ap;
514 char buf[128];
515
516 va_start(ap, fmt);
517 vsnprintf(buf, sizeof(buf), fmt, ap);
518 va_end(ap);
519
520 if (debugfp)
521 fprintf(debugfp, "%s\n", buf);
522 if (interactive)
523 dislog(buf);
524 else
525 printf("%s\n", buf);
526 }
527
528 static void
529 quit(void)
530 {
531 if (interactive) {
532 bdisp(); /* show final board */
533 cursfini();
534 }
535 exit(0);
536 }
537
538 static void
539 quitsig(int dummy __unused)
540 {
541 quit();
542 }
543
544 /*
545 * Die gracefully.
546 */
547 void
548 panic(const char *fmt, ...)
549 {
550 va_list ap;
551
552 fprintf(stderr, "%s: ", prog);
553 va_start(ap, fmt);
554 vfprintf(stderr, fmt, ap);
555 va_end(ap);
556 fprintf(stderr, "\n");
557
558 fputs("resign\n", stdout);
559 quit();
560 }