]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/main.c
1 /* $NetBSD: main.c,v 1.13 2007/12/15 19:44:40 perry 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\n\
38 The Regents of the University of California. All rights reserved.\n");
43 static char sccsid
[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: main.c,v 1.13 2007/12/15 19:44:40 perry Exp $");
59 #define USER 0 /* get input from standard input */
60 #define PROGRAM 1 /* get input from program */
61 #define INPUTF 2 /* get input from a file */
63 int interactive
= 1; /* true if interactive */
64 int debug
; /* true if debugging */
65 int test
; /* both moves come from 1: input, 2: computer */
66 char *prog
; /* name of program */
67 FILE *debugfp
; /* file for debug output */
68 FILE *inputfp
; /* file for debug input */
70 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 *[]);
90 int color
, curmove
, i
, ch
;
92 static const char *const fmt
[2] = {
97 /* Revoke setgid privileges */
102 prog
= strrchr(argv
[0], '/');
108 while ((ch
= getopt(argc
, argv
, "bcdD:u")) != -1) {
110 case 'b': /* background */
113 case 'd': /* debugging */
116 case 'D': /* log debug output to file */
117 if ((debugfp
= fopen(optarg
, "w")) == NULL
)
118 err(1, "%s", optarg
);
120 case 'u': /* testing: user verses user */
123 case 'c': /* testing: computer verses computer */
131 if ((inputfp
= fopen(*argv
, "r")) == NULL
)
142 cursinit(); /* initialize curses */
144 bdinit(board
); /* initialize board contents */
147 plyr
[BLACK
] = plyr
[WHITE
] = "???";
148 bdisp_init(); /* initialize display of board */
150 signal(SIGINT
, whatsup
);
152 signal(SIGINT
, quitsig
);
155 if (inputfp
== NULL
&& test
== 0) {
157 ask("black or white? ");
158 getline(buf
, sizeof(buf
));
159 if (buf
[0] == 'b' || buf
[0] == 'B') {
163 if (buf
[0] == 'w' || buf
[0] == 'W') {
168 printw("Black moves first. Please enter `black' or `white'\n");
175 getline(buf
, sizeof(buf
));
176 if (strcmp(buf
, "black") == 0)
178 else if (strcmp(buf
, "white") == 0)
182 "Huh? Expected `black' or `white', got `%s'\n",
189 input
[BLACK
] = INPUTF
;
190 input
[WHITE
] = INPUTF
;
193 case 0: /* user verses program */
195 input
[!color
] = PROGRAM
;
198 case 1: /* user verses user */
203 case 2: /* program verses program */
204 input
[BLACK
] = PROGRAM
;
205 input
[WHITE
] = PROGRAM
;
210 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
211 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
215 for (color
= BLACK
; ; color
= !color
) {
217 switch (input
[color
]) {
218 case INPUTF
: /* input comes from a file */
219 curmove
= readinput(inputfp
);
220 if (curmove
!= ILLEGAL
)
223 case 0: /* user verses program */
225 input
[!color
] = PROGRAM
;
228 case 1: /* user verses user */
233 case 2: /* program verses program */
234 input
[BLACK
] = PROGRAM
;
235 input
[WHITE
] = PROGRAM
;
238 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
239 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
243 case USER
: /* input comes from standard input */
247 if (!getline(buf
, sizeof(buf
))) {
255 if (curmove
== SAVE
) {
258 ask("save file name? ");
259 (void)getline(buf
, sizeof(buf
));
260 if ((fp
= fopen(buf
, "w")) == NULL
) {
261 glog("cannot create save file");
264 for (i
= 0; i
< movenum
- 1; i
++)
270 if (curmove
!= RESIGN
&&
271 board
[curmove
].s_occ
!= EMPTY
) {
272 glog("Illegal move");
278 case PROGRAM
: /* input comes from the program */
279 curmove
= pickmove(color
);
283 sprintf(fmtbuf
, 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 (getline(buf
, sizeof(buf
)) &&
313 (buf
[0] == 'y' || buf
[0] == 'Y'))
315 if (strcmp(buf
, "save") == 0) {
318 ask("save file name? ");
319 (void)getline(buf
, sizeof(buf
));
320 if ((fp
= fopen(buf
, "w")) == NULL
) {
321 glog("cannot create save file");
324 for (i
= 0; i
< movenum
- 1; i
++)
345 while ((c
= getc(fp
)) != EOF
&& c
!= '\n')
348 return (ctos(fmtbuf
));
353 * Handle strange situations.
359 int i
, pnum
, n
, s1
, s2
, d1
, d2
;
364 struct combostr
*cbp
;
370 if (!getline(fmtbuf
, sizeof(fmtbuf
)))
375 case 'q': /* conservative quit */
377 case 'd': /* set debug level */
378 debug
= fmtbuf
[1] - '0';
379 sprintf(fmtbuf
, "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
= fmtbuf
[1] == 'b' ? BLACK
: WHITE
;
393 sprintf(fmtbuf
, "suggest %c %s", i
== BLACK
? 'B' : 'W',
397 case 'f': /* go forward a move */
398 board
[movelog
[movenum
- 1]].s_occ
= movenum
& 1 ? BLACK
: WHITE
;
402 case 'l': /* print move history */
403 if (fmtbuf
[1] == '\0') {
404 for (i
= 0; i
< movenum
- 1; i
++)
405 dlog(stoc(movelog
[i
]));
408 if ((fp
= fopen(fmtbuf
+ 1, "w")) == NULL
)
410 for (i
= 0; i
< movenum
- 1; i
++) {
411 fprintf(fp
, "%s", stoc(movelog
[i
]));
412 if (++i
< movenum
- 1)
413 fprintf(fp
, " %s\n", stoc(movelog
[i
]));
422 for (str
= fmtbuf
+ 1; *str
; str
++)
424 for (d1
= 0; d1
< 4; d1
++)
425 if (str
[-1] == pdir
[d1
])
428 sp
= &board
[s1
= ctos(fmtbuf
+ 1)];
429 n
= (sp
->s_frame
[d1
] - frames
) * FAREA
;
433 sp
= &board
[s2
= ctos(str
)];
436 for (d2
= 0; d2
< 4; d2
++)
437 if (str
[-1] == pdir
[d2
])
439 n
+= sp
->s_frame
[d2
] - frames
;
441 sprintf(str
, "overlap %s%c,", stoc(s1
), pdir
[d1
]);
443 sprintf(str
, "%s%c = %x", stoc(s2
), pdir
[d2
], overlap
[n
]);
447 sp
= &board
[i
= ctos(fmtbuf
+ 1)];
448 sprintf(fmtbuf
, "V %s %x/%d %d %x/%d %d %d %x", stoc(i
),
449 sp
->s_combo
[BLACK
].s
, sp
->s_level
[BLACK
],
451 sp
->s_combo
[WHITE
].s
, sp
->s_level
[WHITE
],
452 sp
->s_nforce
[WHITE
], sp
->s_wval
, sp
->s_flg
);
454 sprintf(fmtbuf
, "FB %s %x %x %x %x", stoc(i
),
455 sp
->s_fval
[BLACK
][0].s
, sp
->s_fval
[BLACK
][1].s
,
456 sp
->s_fval
[BLACK
][2].s
, sp
->s_fval
[BLACK
][3].s
);
458 sprintf(fmtbuf
, "FW %s %x %x %x %x", stoc(i
),
459 sp
->s_fval
[WHITE
][0].s
, sp
->s_fval
[WHITE
][1].s
,
460 sp
->s_fval
[WHITE
][2].s
, sp
->s_fval
[WHITE
][3].s
);
463 case 'e': /* e {b|w} [0-9] spot */
465 if (*str
>= '0' && *str
<= '9')
469 sp
= &board
[i
= ctos(str
)];
470 for (ep
= sp
->s_empty
; ep
; ep
= ep
->e_next
) {
473 if (cbp
->c_nframes
> n
)
475 if (cbp
->c_nframes
!= n
)
478 printcombo(cbp
, fmtbuf
);
484 dlog("Options are:");
486 dlog("c - continue");
487 dlog("d# - set debug level to #");
488 dlog("p# - print values at #");
495 * Display debug info.
503 fprintf(debugfp
, "%s\n", str
);
507 fprintf(stderr
, "%s\n", str
);
516 fprintf(debugfp
, "%s\n", str
);
527 bdisp(); /* show final board */
547 fprintf(stderr
, "%s: %s\n", prog
, str
);
548 fputs("resign\n", stdout
);