]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/main.c
1 /* $NetBSD: main.c,v 1.21 2010/03/29 02:46:05 dholland 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.21 2010/03/29 02:46:05 dholland Exp $");
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 */
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 */
72 const char pdir
[4] = "-\\|/";
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 */
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
;
89 main(int argc
, char **argv
)
93 int color
, curmove
, i
, ch
;
95 static const char *const fmt
[2] = {
100 /* Revoke setgid privileges */
105 prog
= strrchr(argv
[0], '/');
111 while ((ch
= getopt(argc
, argv
, "bcdD:u")) != -1) {
113 case 'b': /* background */
116 case 'd': /* debugging */
119 case 'D': /* log debug output to file */
120 if ((debugfp
= fopen(optarg
, "w")) == NULL
)
121 err(1, "%s", optarg
);
123 case 'u': /* testing: user verses user */
126 case 'c': /* testing: computer verses computer */
134 if ((inputfp
= fopen(*argv
, "r")) == NULL
)
145 cursinit(); /* initialize curses */
147 bdinit(board
); /* initialize board contents */
150 plyr
[BLACK
] = plyr
[WHITE
] = "???";
151 bdisp_init(); /* initialize display of board */
153 signal(SIGINT
, whatsup
);
155 signal(SIGINT
, quitsig
);
158 if (inputfp
== NULL
&& test
== 0) {
160 ask("black or white? ");
161 get_line(buf
, sizeof(buf
));
162 if (buf
[0] == 'b' || buf
[0] == 'B') {
166 if (buf
[0] == 'w' || buf
[0] == 'W') {
171 printw("Black moves first. Please enter `black' or `white'\n");
178 get_line(buf
, sizeof(buf
));
179 if (strcmp(buf
, "black") == 0)
181 else if (strcmp(buf
, "white") == 0)
184 panic("Huh? Expected `black' or `white', got `%s'\n",
190 input
[BLACK
] = INPUTF
;
191 input
[WHITE
] = INPUTF
;
194 case 0: /* user verses program */
196 input
[!color
] = PROGRAM
;
199 case 1: /* user verses user */
204 case 2: /* program verses program */
205 input
[BLACK
] = PROGRAM
;
206 input
[WHITE
] = PROGRAM
;
211 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
212 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
216 for (color
= BLACK
; ; color
= !color
) {
218 switch (input
[color
]) {
219 case INPUTF
: /* input comes from a file */
220 curmove
= readinput(inputfp
);
221 if (curmove
!= ILLEGAL
)
224 case 0: /* user verses program */
226 input
[!color
] = PROGRAM
;
229 case 1: /* user verses user */
234 case 2: /* program verses program */
235 input
[BLACK
] = PROGRAM
;
236 input
[WHITE
] = PROGRAM
;
239 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
240 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
244 case USER
: /* input comes from standard input */
248 if (!get_line(buf
, sizeof(buf
))) {
256 if (curmove
== SAVE
) {
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");
265 for (i
= 0; i
< movenum
- 1; i
++)
271 if (curmove
!= RESIGN
&&
272 board
[curmove
].s_occ
!= EMPTY
) {
273 misclog("Illegal move");
279 case PROGRAM
: /* input comes from the program */
280 curmove
= pickmove(color
);
284 misclog(fmt
[color
], movenum
, stoc(curmove
));
286 if ((i
= makemove(color
, curmove
)) != MOVEOK
)
295 if (input
[color
] == PROGRAM
)
296 addstr("Ha ha, I won");
298 addstr("Rats! you won");
301 addstr("Wow! its a tie");
304 addstr("Illegal move");
312 if (get_line(buf
, sizeof(buf
)) &&
313 (buf
[0] == 'y' || buf
[0] == 'Y'))
315 if (strcmp(buf
, "save") == 0) {
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");
324 for (i
= 0; i
< movenum
- 1; i
++)
345 while ((c
= getc(fp
)) != EOF
&& c
!= '\n' && pos
< sizeof(buf
) - 1)
353 * Handle strange situations.
358 int i
, n
, s1
, s2
, d1
, d2
;
363 struct combostr
*cbp
;
371 if (!get_line(input
, sizeof(input
)))
376 case 'q': /* conservative quit */
378 case 'd': /* set debug level */
379 debug
= input
[1] - '0';
380 debuglog("Debug set to %d", debug
);
384 case 'b': /* back up a move */
387 board
[movelog
[movenum
- 1]].s_occ
= EMPTY
;
391 case 's': /* suggest a move */
392 i
= input
[1] == 'b' ? BLACK
: WHITE
;
393 debuglog("suggest %c %s", i
== BLACK
? 'B' : 'W',
396 case 'f': /* go forward a move */
397 board
[movelog
[movenum
- 1]].s_occ
= movenum
& 1 ? BLACK
: WHITE
;
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
]));
407 if ((fp
= fopen(input
+ 1, "w")) == NULL
)
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
]));
420 /* avoid use w/o initialization on invalid input */
424 for (str
= input
+ 1; *str
; str
++)
426 for (d1
= 0; d1
< 4; d1
++)
427 if (str
[-1] == pdir
[d1
])
430 sp
= &board
[s1
= ctos(input
+ 1)];
431 n
= (sp
->s_frame
[d1
] - frames
) * FAREA
;
435 sp
= &board
[s2
= ctos(str
)];
438 for (d2
= 0; d2
< 4; d2
++)
439 if (str
[-1] == pdir
[d2
])
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
]);
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
],
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
);
459 case 'e': /* e {b|w} [0-9] spot */
461 if (*str
>= '0' && *str
<= '9')
465 sp
= &board
[i
= ctos(str
)];
466 for (ep
= sp
->s_empty
; ep
; ep
= ep
->e_next
) {
469 if (cbp
->c_nframes
> n
)
471 if (cbp
->c_nframes
!= n
)
474 printcombo(cbp
, tmp
, sizeof(tmp
));
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 #");
490 * Display debug info.
493 debuglog(const char *fmt
, ...)
499 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
503 fprintf(debugfp
, "%s\n", buf
);
507 fprintf(stderr
, "%s\n", buf
);
511 misclog(const char *fmt
, ...)
517 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
521 fprintf(debugfp
, "%s\n", buf
);
532 bdisp(); /* show final board */
539 quitsig(int dummy __unused
)
548 panic(const char *fmt
, ...)
552 fprintf(stderr
, "%s: ", prog
);
554 vfprintf(stderr
, fmt
, ap
);
556 fprintf(stderr
, "\n");
558 fputs("resign\n", stdout
);