]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/main.c
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static char copyright
[] =
39 "@(#) Copyright (c) 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
44 static char sccsid
[] = "@(#)main.c 8.4 (Berkeley) 5/4/95";
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 */
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 */
68 char pdir
[4] = "-\\|/";
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 */
82 extern void whatsup();
90 int color
, curmove
, i
, ch
;
92 static char *fmt
[2] = {
97 prog
= strrchr(argv
[0], '/');
103 while ((ch
= getopt(argc
, argv
, "bcdD:u")) != EOF
) {
105 case 'b': /* background */
108 case 'd': /* debugging */
111 case 'D': /* log debug output to file */
112 if ((debugfp
= fopen(optarg
, "w")) == NULL
)
113 err(1, "%s", optarg
);
115 case 'u': /* testing: user verses user */
118 case 'c': /* testing: computer verses computer */
126 if ((inputfp
= fopen(*argv
, "r")) == NULL
)
137 cursinit(); /* initialize curses */
139 bdinit(board
); /* initialize board contents */
142 plyr
[BLACK
] = plyr
[WHITE
] = "???";
143 bdisp_init(); /* initialize display of board */
145 signal(SIGINT
, whatsup
);
147 signal(SIGINT
, quit
);
150 if (inputfp
== NULL
&& test
== 0) {
152 ask("black or white? ");
153 getline(buf
, sizeof(buf
));
154 if (buf
[0] == 'b' || buf
[0] == 'B') {
158 if (buf
[0] == 'w' || buf
[0] == 'W') {
163 printw("Black moves first. Please enter `black' or `white'\n");
170 getline(buf
, sizeof(buf
));
171 if (strcmp(buf
, "black") == 0)
173 else if (strcmp(buf
, "white") == 0)
177 "Huh? Expected `black' or `white', got `%s'\n",
184 input
[BLACK
] = INPUTF
;
185 input
[WHITE
] = INPUTF
;
188 case 0: /* user verses program */
190 input
[!color
] = PROGRAM
;
193 case 1: /* user verses user */
198 case 2: /* program verses program */
199 input
[BLACK
] = PROGRAM
;
200 input
[WHITE
] = PROGRAM
;
205 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
206 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
210 for (color
= BLACK
; ; color
= !color
) {
212 switch (input
[color
]) {
213 case INPUTF
: /* input comes from a file */
214 curmove
= readinput(inputfp
);
215 if (curmove
!= ILLEGAL
)
218 case 0: /* user verses program */
220 input
[!color
] = PROGRAM
;
223 case 1: /* user verses user */
228 case 2: /* program verses program */
229 input
[BLACK
] = PROGRAM
;
230 input
[WHITE
] = PROGRAM
;
233 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
234 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
238 case USER
: /* input comes from standard input */
242 if (!getline(buf
, sizeof(buf
))) {
250 if (curmove
== SAVE
) {
253 ask("save file name? ");
254 (void)getline(buf
, sizeof(buf
));
255 if ((fp
= fopen(buf
, "w")) == NULL
) {
256 log("cannot create save file");
259 for (i
= 0; i
< movenum
- 1; i
++)
265 if (curmove
!= RESIGN
&&
266 board
[curmove
].s_occ
!= EMPTY
) {
273 case PROGRAM
: /* input comes from the program */
274 curmove
= pickmove(color
);
278 sprintf(fmtbuf
, 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 (getline(buf
, sizeof(buf
)) &&
308 buf
[0] == 'y' || buf
[0] == 'Y')
310 if (strcmp(buf
, "save") == 0) {
313 ask("save file name? ");
314 (void)getline(buf
, sizeof(buf
));
315 if ((fp
= fopen(buf
, "w")) == NULL
) {
316 log("cannot create save file");
319 for (i
= 0; i
< movenum
- 1; i
++)
337 while ((c
= getc(fp
)) != EOF
&& c
!= '\n')
340 return (ctos(fmtbuf
));
345 * Handle strange situations.
351 int i
, pnum
, n
, s1
, s2
, d1
, d2
;
356 struct combostr
*cbp
;
362 if (!getline(fmtbuf
, sizeof(fmtbuf
)))
367 case 'q': /* conservative quit */
369 case 'd': /* set debug level */
370 debug
= fmtbuf
[1] - '0';
371 sprintf(fmtbuf
, "Debug set to %d", debug
);
376 case 'b': /* back up a move */
379 board
[movelog
[movenum
- 1]].s_occ
= EMPTY
;
383 case 's': /* suggest a move */
384 i
= fmtbuf
[1] == 'b' ? BLACK
: WHITE
;
385 sprintf(fmtbuf
, "suggest %c %s", i
== BLACK
? 'B' : 'W',
389 case 'f': /* go forward a move */
390 board
[movelog
[movenum
- 1]].s_occ
= movenum
& 1 ? BLACK
: WHITE
;
394 case 'l': /* print move history */
395 if (fmtbuf
[1] == '\0') {
396 for (i
= 0; i
< movenum
- 1; i
++)
397 dlog(stoc(movelog
[i
]));
400 if ((fp
= fopen(fmtbuf
+ 1, "w")) == NULL
)
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
]));
414 for (str
= fmtbuf
+ 1; *str
; str
++)
416 for (d1
= 0; d1
< 4; d1
++)
417 if (str
[-1] == pdir
[d1
])
420 sp
= &board
[s1
= ctos(fmtbuf
+ 1)];
421 n
= (sp
->s_frame
[d1
] - frames
) * FAREA
;
425 sp
= &board
[s2
= ctos(str
)];
428 for (d2
= 0; d2
< 4; d2
++)
429 if (str
[-1] == pdir
[d2
])
431 n
+= sp
->s_frame
[d2
] - frames
;
433 sprintf(str
, "overlap %s%c,", stoc(s1
), pdir
[d1
]);
435 sprintf(str
, "%s%c = %x", stoc(s2
), pdir
[d2
], overlap
[n
]);
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
],
443 sp
->s_combo
[WHITE
].s
, sp
->s_level
[WHITE
],
444 sp
->s_nforce
[WHITE
], sp
->s_wval
, sp
->s_flg
);
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
);
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
);
455 case 'e': /* e {b|w} [0-9] spot */
457 if (*str
>= '0' && *str
<= '9')
461 sp
= &board
[i
= ctos(str
)];
462 for (ep
= sp
->s_empty
; ep
; ep
= ep
->e_next
) {
465 if (cbp
->c_nframes
> n
)
467 if (cbp
->c_nframes
!= n
)
470 printcombo(cbp
, fmtbuf
);
476 dlog("Options are:");
478 dlog("c - continue");
479 dlog("d# - set debug level to #");
480 dlog("p# - print values at #");
487 * Display debug info.
494 fprintf(debugfp
, "%s\n", str
);
498 fprintf(stderr
, "%s\n", str
);
506 fprintf(debugfp
, "%s\n", str
);
517 bdisp(); /* show final board */
529 fprintf(stderr
, "%s: %s\n", prog
, str
);
530 fputs("resign\n", stdout
);