]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - gomoku/main.c
1 /* $NetBSD: main.c,v 1.2 1996/12/28 18:57:01 tls 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
40 static char copyright
[] =
41 "@(#) 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 static char rcsid
[] = "$NetBSD: main.c,v 1.2 1996/12/28 18:57:01 tls 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 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 char *plyr
[2]; /* who's who */
88 extern void whatsup();
96 int color
, curmove
, i
, ch
;
98 static char *fmt
[2] = {
103 prog
= strrchr(argv
[0], '/');
109 while ((ch
= getopt(argc
, argv
, "bcdD:u")) != EOF
) {
111 case 'b': /* background */
114 case 'd': /* debugging */
117 case 'D': /* log debug output to file */
118 if ((debugfp
= fopen(optarg
, "w")) == NULL
)
119 err(1, "%s", optarg
);
121 case 'u': /* testing: user verses user */
124 case 'c': /* testing: computer verses computer */
132 if ((inputfp
= fopen(*argv
, "r")) == NULL
)
143 cursinit(); /* initialize curses */
145 bdinit(board
); /* initialize board contents */
148 plyr
[BLACK
] = plyr
[WHITE
] = "???";
149 bdisp_init(); /* initialize display of board */
151 signal(SIGINT
, whatsup
);
153 signal(SIGINT
, quit
);
156 if (inputfp
== NULL
&& test
== 0) {
158 ask("black or white? ");
159 getline(buf
, sizeof(buf
));
160 if (buf
[0] == 'b' || buf
[0] == 'B') {
164 if (buf
[0] == 'w' || buf
[0] == 'W') {
169 printw("Black moves first. Please enter `black' or `white'\n");
176 getline(buf
, sizeof(buf
));
177 if (strcmp(buf
, "black") == 0)
179 else if (strcmp(buf
, "white") == 0)
183 "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 (!getline(buf
, sizeof(buf
))) {
256 if (curmove
== SAVE
) {
259 ask("save file name? ");
260 (void)getline(buf
, sizeof(buf
));
261 if ((fp
= fopen(buf
, "w")) == NULL
) {
262 log("cannot create save file");
265 for (i
= 0; i
< movenum
- 1; i
++)
271 if (curmove
!= RESIGN
&&
272 board
[curmove
].s_occ
!= EMPTY
) {
279 case PROGRAM
: /* input comes from the program */
280 curmove
= pickmove(color
);
284 sprintf(fmtbuf
, fmt
[color
], movenum
, stoc(curmove
));
287 if ((i
= makemove(color
, curmove
)) != MOVEOK
)
296 if (input
[color
] == PROGRAM
)
297 addstr("Ha ha, I won");
299 addstr("Rats! you won");
302 addstr("Wow! its a tie");
305 addstr("Illegal move");
313 if (getline(buf
, sizeof(buf
)) &&
314 buf
[0] == 'y' || buf
[0] == 'Y')
316 if (strcmp(buf
, "save") == 0) {
319 ask("save file name? ");
320 (void)getline(buf
, sizeof(buf
));
321 if ((fp
= fopen(buf
, "w")) == NULL
) {
322 log("cannot create save file");
325 for (i
= 0; i
< movenum
- 1; i
++)
343 while ((c
= getc(fp
)) != EOF
&& c
!= '\n')
346 return (ctos(fmtbuf
));
351 * Handle strange situations.
357 int i
, pnum
, n
, s1
, s2
, d1
, d2
;
362 struct combostr
*cbp
;
368 if (!getline(fmtbuf
, sizeof(fmtbuf
)))
373 case 'q': /* conservative quit */
375 case 'd': /* set debug level */
376 debug
= fmtbuf
[1] - '0';
377 sprintf(fmtbuf
, "Debug set to %d", debug
);
382 case 'b': /* back up a move */
385 board
[movelog
[movenum
- 1]].s_occ
= EMPTY
;
389 case 's': /* suggest a move */
390 i
= fmtbuf
[1] == 'b' ? BLACK
: WHITE
;
391 sprintf(fmtbuf
, "suggest %c %s", i
== BLACK
? 'B' : 'W',
395 case 'f': /* go forward a move */
396 board
[movelog
[movenum
- 1]].s_occ
= movenum
& 1 ? BLACK
: WHITE
;
400 case 'l': /* print move history */
401 if (fmtbuf
[1] == '\0') {
402 for (i
= 0; i
< movenum
- 1; i
++)
403 dlog(stoc(movelog
[i
]));
406 if ((fp
= fopen(fmtbuf
+ 1, "w")) == NULL
)
408 for (i
= 0; i
< movenum
- 1; i
++) {
409 fprintf(fp
, "%s", stoc(movelog
[i
]));
410 if (++i
< movenum
- 1)
411 fprintf(fp
, " %s\n", stoc(movelog
[i
]));
420 for (str
= fmtbuf
+ 1; *str
; str
++)
422 for (d1
= 0; d1
< 4; d1
++)
423 if (str
[-1] == pdir
[d1
])
426 sp
= &board
[s1
= ctos(fmtbuf
+ 1)];
427 n
= (sp
->s_frame
[d1
] - frames
) * FAREA
;
431 sp
= &board
[s2
= ctos(str
)];
434 for (d2
= 0; d2
< 4; d2
++)
435 if (str
[-1] == pdir
[d2
])
437 n
+= sp
->s_frame
[d2
] - frames
;
439 sprintf(str
, "overlap %s%c,", stoc(s1
), pdir
[d1
]);
441 sprintf(str
, "%s%c = %x", stoc(s2
), pdir
[d2
], overlap
[n
]);
445 sp
= &board
[i
= ctos(fmtbuf
+ 1)];
446 sprintf(fmtbuf
, "V %s %x/%d %d %x/%d %d %d %x", stoc(i
),
447 sp
->s_combo
[BLACK
].s
, sp
->s_level
[BLACK
],
449 sp
->s_combo
[WHITE
].s
, sp
->s_level
[WHITE
],
450 sp
->s_nforce
[WHITE
], sp
->s_wval
, sp
->s_flg
);
452 sprintf(fmtbuf
, "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
);
456 sprintf(fmtbuf
, "FW %s %x %x %x %x", stoc(i
),
457 sp
->s_fval
[WHITE
][0].s
, sp
->s_fval
[WHITE
][1].s
,
458 sp
->s_fval
[WHITE
][2].s
, sp
->s_fval
[WHITE
][3].s
);
461 case 'e': /* e {b|w} [0-9] spot */
463 if (*str
>= '0' && *str
<= '9')
467 sp
= &board
[i
= ctos(str
)];
468 for (ep
= sp
->s_empty
; ep
; ep
= ep
->e_next
) {
471 if (cbp
->c_nframes
> n
)
473 if (cbp
->c_nframes
!= n
)
476 printcombo(cbp
, fmtbuf
);
482 dlog("Options are:");
484 dlog("c - continue");
485 dlog("d# - set debug level to #");
486 dlog("p# - print values at #");
493 * Display debug info.
500 fprintf(debugfp
, "%s\n", str
);
504 fprintf(stderr
, "%s\n", str
);
512 fprintf(debugfp
, "%s\n", str
);
523 bdisp(); /* show final board */
535 fprintf(stderr
, "%s: %s\n", prog
, str
);
536 fputs("resign\n", stdout
);