]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/main.c
1 /* $NetBSD: main.c,v 1.10 2000/05/08 07:56:03 mycroft 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
41 __COPYRIGHT("@(#) Copyright (c) 1994\n\
42 The Regents of the University of California. All rights reserved.\n");
47 static char sccsid
[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
49 __RCSID("$NetBSD: main.c,v 1.10 2000/05/08 07:56:03 mycroft Exp $");
63 #define USER 0 /* get input from standard input */
64 #define PROGRAM 1 /* get input from program */
65 #define INPUTF 2 /* get input from a file */
67 int interactive
= 1; /* true if interactive */
68 int debug
; /* true if debugging */
69 int test
; /* both moves come from 1: input, 2: computer */
70 char *prog
; /* name of program */
71 FILE *debugfp
; /* file for debug output */
72 FILE *inputfp
; /* file for debug input */
74 const char pdir
[4] = "-\\|/";
77 struct spotstr board
[BAREA
]; /* info for board */
78 struct combostr frames
[FAREA
]; /* storage for all frames */
79 struct combostr
*sortframes
[2]; /* sorted list of non-empty frames */
80 u_char overlap
[FAREA
* FAREA
]; /* true if frame [a][b] overlap */
81 short intersect
[FAREA
* FAREA
]; /* frame [a][b] intersection */
82 int movelog
[BSZ
* BSZ
]; /* log of all the moves */
83 int movenum
; /* current move number */
84 const char *plyr
[2]; /* who's who */
86 int main
__P((int, char *[]));
94 int color
, curmove
, i
, ch
;
96 static const char *const fmt
[2] = {
101 /* Revoke setgid privileges */
106 prog
= strrchr(argv
[0], '/');
112 while ((ch
= getopt(argc
, argv
, "bcdD:u")) != -1) {
114 case 'b': /* background */
117 case 'd': /* debugging */
120 case 'D': /* log debug output to file */
121 if ((debugfp
= fopen(optarg
, "w")) == NULL
)
122 err(1, "%s", optarg
);
124 case 'u': /* testing: user verses user */
127 case 'c': /* testing: computer verses computer */
135 if ((inputfp
= fopen(*argv
, "r")) == NULL
)
146 cursinit(); /* initialize curses */
148 bdinit(board
); /* initialize board contents */
151 plyr
[BLACK
] = plyr
[WHITE
] = "???";
152 bdisp_init(); /* initialize display of board */
154 signal(SIGINT
, whatsup
);
156 signal(SIGINT
, quitsig
);
159 if (inputfp
== NULL
&& test
== 0) {
161 ask("black or white? ");
162 getline(buf
, sizeof(buf
));
163 if (buf
[0] == 'b' || buf
[0] == 'B') {
167 if (buf
[0] == 'w' || buf
[0] == 'W') {
172 printw("Black moves first. Please enter `black' or `white'\n");
179 getline(buf
, sizeof(buf
));
180 if (strcmp(buf
, "black") == 0)
182 else if (strcmp(buf
, "white") == 0)
186 "Huh? Expected `black' or `white', got `%s'\n",
193 input
[BLACK
] = INPUTF
;
194 input
[WHITE
] = INPUTF
;
197 case 0: /* user verses program */
199 input
[!color
] = PROGRAM
;
202 case 1: /* user verses user */
207 case 2: /* program verses program */
208 input
[BLACK
] = PROGRAM
;
209 input
[WHITE
] = PROGRAM
;
214 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
215 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
219 for (color
= BLACK
; ; color
= !color
) {
221 switch (input
[color
]) {
222 case INPUTF
: /* input comes from a file */
223 curmove
= readinput(inputfp
);
224 if (curmove
!= ILLEGAL
)
227 case 0: /* user verses program */
229 input
[!color
] = PROGRAM
;
232 case 1: /* user verses user */
237 case 2: /* program verses program */
238 input
[BLACK
] = PROGRAM
;
239 input
[WHITE
] = PROGRAM
;
242 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
243 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
247 case USER
: /* input comes from standard input */
251 if (!getline(buf
, sizeof(buf
))) {
259 if (curmove
== SAVE
) {
262 ask("save file name? ");
263 (void)getline(buf
, sizeof(buf
));
264 if ((fp
= fopen(buf
, "w")) == NULL
) {
265 glog("cannot create save file");
268 for (i
= 0; i
< movenum
- 1; i
++)
274 if (curmove
!= RESIGN
&&
275 board
[curmove
].s_occ
!= EMPTY
) {
276 glog("Illegal move");
282 case PROGRAM
: /* input comes from the program */
283 curmove
= pickmove(color
);
287 sprintf(fmtbuf
, fmt
[color
], movenum
, stoc(curmove
));
290 if ((i
= makemove(color
, curmove
)) != MOVEOK
)
299 if (input
[color
] == PROGRAM
)
300 addstr("Ha ha, I won");
302 addstr("Rats! you won");
305 addstr("Wow! its a tie");
308 addstr("Illegal move");
316 if (getline(buf
, sizeof(buf
)) &&
317 (buf
[0] == 'y' || buf
[0] == 'Y'))
319 if (strcmp(buf
, "save") == 0) {
322 ask("save file name? ");
323 (void)getline(buf
, sizeof(buf
));
324 if ((fp
= fopen(buf
, "w")) == NULL
) {
325 glog("cannot create save file");
328 for (i
= 0; i
< movenum
- 1; i
++)
349 while ((c
= getc(fp
)) != EOF
&& c
!= '\n')
352 return (ctos(fmtbuf
));
357 * Handle strange situations.
363 int i
, pnum
, n
, s1
, s2
, d1
, d2
;
368 struct combostr
*cbp
;
374 if (!getline(fmtbuf
, sizeof(fmtbuf
)))
379 case 'q': /* conservative quit */
381 case 'd': /* set debug level */
382 debug
= fmtbuf
[1] - '0';
383 sprintf(fmtbuf
, "Debug set to %d", debug
);
388 case 'b': /* back up a move */
391 board
[movelog
[movenum
- 1]].s_occ
= EMPTY
;
395 case 's': /* suggest a move */
396 i
= fmtbuf
[1] == 'b' ? BLACK
: WHITE
;
397 sprintf(fmtbuf
, "suggest %c %s", i
== BLACK
? 'B' : 'W',
401 case 'f': /* go forward a move */
402 board
[movelog
[movenum
- 1]].s_occ
= movenum
& 1 ? BLACK
: WHITE
;
406 case 'l': /* print move history */
407 if (fmtbuf
[1] == '\0') {
408 for (i
= 0; i
< movenum
- 1; i
++)
409 dlog(stoc(movelog
[i
]));
412 if ((fp
= fopen(fmtbuf
+ 1, "w")) == NULL
)
414 for (i
= 0; i
< movenum
- 1; i
++) {
415 fprintf(fp
, "%s", stoc(movelog
[i
]));
416 if (++i
< movenum
- 1)
417 fprintf(fp
, " %s\n", stoc(movelog
[i
]));
426 for (str
= fmtbuf
+ 1; *str
; str
++)
428 for (d1
= 0; d1
< 4; d1
++)
429 if (str
[-1] == pdir
[d1
])
432 sp
= &board
[s1
= ctos(fmtbuf
+ 1)];
433 n
= (sp
->s_frame
[d1
] - frames
) * FAREA
;
437 sp
= &board
[s2
= ctos(str
)];
440 for (d2
= 0; d2
< 4; d2
++)
441 if (str
[-1] == pdir
[d2
])
443 n
+= sp
->s_frame
[d2
] - frames
;
445 sprintf(str
, "overlap %s%c,", stoc(s1
), pdir
[d1
]);
447 sprintf(str
, "%s%c = %x", stoc(s2
), pdir
[d2
], overlap
[n
]);
451 sp
= &board
[i
= ctos(fmtbuf
+ 1)];
452 sprintf(fmtbuf
, "V %s %x/%d %d %x/%d %d %d %x", stoc(i
),
453 sp
->s_combo
[BLACK
].s
, sp
->s_level
[BLACK
],
455 sp
->s_combo
[WHITE
].s
, sp
->s_level
[WHITE
],
456 sp
->s_nforce
[WHITE
], sp
->s_wval
, sp
->s_flg
);
458 sprintf(fmtbuf
, "FB %s %x %x %x %x", stoc(i
),
459 sp
->s_fval
[BLACK
][0].s
, sp
->s_fval
[BLACK
][1].s
,
460 sp
->s_fval
[BLACK
][2].s
, sp
->s_fval
[BLACK
][3].s
);
462 sprintf(fmtbuf
, "FW %s %x %x %x %x", stoc(i
),
463 sp
->s_fval
[WHITE
][0].s
, sp
->s_fval
[WHITE
][1].s
,
464 sp
->s_fval
[WHITE
][2].s
, sp
->s_fval
[WHITE
][3].s
);
467 case 'e': /* e {b|w} [0-9] spot */
469 if (*str
>= '0' && *str
<= '9')
473 sp
= &board
[i
= ctos(str
)];
474 for (ep
= sp
->s_empty
; ep
; ep
= ep
->e_next
) {
477 if (cbp
->c_nframes
> n
)
479 if (cbp
->c_nframes
!= n
)
482 printcombo(cbp
, fmtbuf
);
488 dlog("Options are:");
490 dlog("c - continue");
491 dlog("d# - set debug level to #");
492 dlog("p# - print values at #");
499 * Display debug info.
507 fprintf(debugfp
, "%s\n", str
);
511 fprintf(stderr
, "%s\n", str
);
520 fprintf(debugfp
, "%s\n", str
);
531 bdisp(); /* show final board */
539 int dummy
__attribute__((__unused__
));
551 fprintf(stderr
, "%s: %s\n", prog
, str
);
552 fputs("resign\n", stdout
);