]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/main.c
1 /* $NetBSD: main.c,v 1.19 2009/07/13 19:05:40 roy Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1994\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: main.c,v 1.19 2009/07/13 19:05:40 roy Exp $");
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 */
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 */
71 const char pdir
[4] = "-\\|/";
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 */
82 int main(int, char *[]);
85 main(int argc
, char **argv
)
88 int color
, curmove
, i
, ch
;
90 static const char *const fmt
[2] = {
95 /* Revoke setgid privileges */
100 prog
= strrchr(argv
[0], '/');
106 while ((ch
= getopt(argc
, argv
, "bcdD:u")) != -1) {
108 case 'b': /* background */
111 case 'd': /* debugging */
114 case 'D': /* log debug output to file */
115 if ((debugfp
= fopen(optarg
, "w")) == NULL
)
116 err(1, "%s", optarg
);
118 case 'u': /* testing: user verses user */
121 case 'c': /* testing: computer verses computer */
129 if ((inputfp
= fopen(*argv
, "r")) == NULL
)
140 cursinit(); /* initialize curses */
142 bdinit(board
); /* initialize board contents */
145 plyr
[BLACK
] = plyr
[WHITE
] = "???";
146 bdisp_init(); /* initialize display of board */
148 signal(SIGINT
, whatsup
);
150 signal(SIGINT
, quitsig
);
153 if (inputfp
== NULL
&& test
== 0) {
155 ask("black or white? ");
156 get_line(buf
, sizeof(buf
));
157 if (buf
[0] == 'b' || buf
[0] == 'B') {
161 if (buf
[0] == 'w' || buf
[0] == 'W') {
166 printw("Black moves first. Please enter `black' or `white'\n");
173 get_line(buf
, sizeof(buf
));
174 if (strcmp(buf
, "black") == 0)
176 else if (strcmp(buf
, "white") == 0)
179 panic("Huh? Expected `black' or `white', got `%s'\n",
185 input
[BLACK
] = INPUTF
;
186 input
[WHITE
] = INPUTF
;
189 case 0: /* user verses program */
191 input
[!color
] = PROGRAM
;
194 case 1: /* user verses user */
199 case 2: /* program verses program */
200 input
[BLACK
] = PROGRAM
;
201 input
[WHITE
] = PROGRAM
;
206 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
207 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
211 for (color
= BLACK
; ; color
= !color
) {
213 switch (input
[color
]) {
214 case INPUTF
: /* input comes from a file */
215 curmove
= readinput(inputfp
);
216 if (curmove
!= ILLEGAL
)
219 case 0: /* user verses program */
221 input
[!color
] = PROGRAM
;
224 case 1: /* user verses user */
229 case 2: /* program verses program */
230 input
[BLACK
] = PROGRAM
;
231 input
[WHITE
] = PROGRAM
;
234 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
235 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
239 case USER
: /* input comes from standard input */
243 if (!get_line(buf
, sizeof(buf
))) {
251 if (curmove
== SAVE
) {
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");
260 for (i
= 0; i
< movenum
- 1; i
++)
266 if (curmove
!= RESIGN
&&
267 board
[curmove
].s_occ
!= EMPTY
) {
268 misclog("Illegal move");
274 case PROGRAM
: /* input comes from the program */
275 curmove
= pickmove(color
);
279 misclog(fmt
[color
], movenum
, stoc(curmove
));
281 if ((i
= makemove(color
, curmove
)) != MOVEOK
)
290 if (input
[color
] == PROGRAM
)
291 addstr("Ha ha, I won");
293 addstr("Rats! you won");
296 addstr("Wow! its a tie");
299 addstr("Illegal move");
307 if (get_line(buf
, sizeof(buf
)) &&
308 (buf
[0] == 'y' || buf
[0] == 'Y'))
310 if (strcmp(buf
, "save") == 0) {
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");
319 for (i
= 0; i
< movenum
- 1; i
++)
340 while ((c
= getc(fp
)) != EOF
&& c
!= '\n' && pos
< sizeof(buf
) - 1)
348 * Handle strange situations.
353 int i
, n
, s1
, s2
, d1
, d2
;
358 struct combostr
*cbp
;
366 if (!get_line(input
, sizeof(input
)))
371 case 'q': /* conservative quit */
373 case 'd': /* set debug level */
374 debug
= input
[1] - '0';
375 debuglog("Debug set to %d", debug
);
379 case 'b': /* back up a move */
382 board
[movelog
[movenum
- 1]].s_occ
= EMPTY
;
386 case 's': /* suggest a move */
387 i
= input
[1] == 'b' ? BLACK
: WHITE
;
388 debuglog("suggest %c %s", i
== BLACK
? 'B' : 'W',
391 case 'f': /* go forward a move */
392 board
[movelog
[movenum
- 1]].s_occ
= movenum
& 1 ? BLACK
: WHITE
;
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
]));
402 if ((fp
= fopen(input
+ 1, "w")) == NULL
)
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
]));
415 /* avoid use w/o initialization on invalid input */
419 for (str
= input
+ 1; *str
; str
++)
421 for (d1
= 0; d1
< 4; d1
++)
422 if (str
[-1] == pdir
[d1
])
425 sp
= &board
[s1
= ctos(input
+ 1)];
426 n
= (sp
->s_frame
[d1
] - frames
) * FAREA
;
430 sp
= &board
[s2
= ctos(str
)];
433 for (d2
= 0; d2
< 4; d2
++)
434 if (str
[-1] == pdir
[d2
])
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
]);
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
],
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
);
454 case 'e': /* e {b|w} [0-9] spot */
456 if (*str
>= '0' && *str
<= '9')
460 sp
= &board
[i
= ctos(str
)];
461 for (ep
= sp
->s_empty
; ep
; ep
= ep
->e_next
) {
464 if (cbp
->c_nframes
> n
)
466 if (cbp
->c_nframes
!= n
)
469 printcombo(cbp
, tmp
, sizeof(tmp
));
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 #");
485 * Display debug info.
488 debuglog(const char *fmt
, ...)
494 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
498 fprintf(debugfp
, "%s\n", buf
);
502 fprintf(stderr
, "%s\n", buf
);
506 misclog(const char *fmt
, ...)
512 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
516 fprintf(debugfp
, "%s\n", buf
);
527 bdisp(); /* show final board */
534 quitsig(int dummy __unused
)
543 panic(const char *fmt
, ...)
547 fprintf(stderr
, "%s: ", prog
);
549 vfprintf(stderr
, fmt
, ap
);
551 fprintf(stderr
, "\n");
553 fputs("resign\n", stdout
);