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