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