1 /* $NetBSD: main.c,v 1.19 2009/07/13 19:05:40 roy 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.19 2009/07/13 19:05:40 roy Exp $");
60 #define USER 0 /* get input from standard input */
61 #define PROGRAM 1 /* get input from program */
62 #define INPUTF 2 /* get input from a file */
64 int interactive
= 1; /* true if interactive */
65 int debug
; /* true if debugging */
66 static int test
; /* both moves come from 1: input, 2: computer */
67 static char *prog
; /* name of program */
68 static FILE *debugfp
; /* file for debug output */
69 static FILE *inputfp
; /* file for debug input */
71 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 static int readinput(FILE *);
83 static void misclog(const char *, ...) __printflike(1, 2);
84 static void quit(void) __dead
;
85 static void quitsig(int) __dead
;
88 main(int argc
, char **argv
)
91 int color
, curmove
, i
, ch
;
93 static const char *const fmt
[2] = {
98 /* Revoke setgid privileges */
103 prog
= strrchr(argv
[0], '/');
109 while ((ch
= getopt(argc
, argv
, "bcdD:u")) != -1) {
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
, quitsig
);
156 if (inputfp
== NULL
&& test
== 0) {
158 ask("black or white? ");
159 get_line(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 get_line(buf
, sizeof(buf
));
177 if (strcmp(buf
, "black") == 0)
179 else if (strcmp(buf
, "white") == 0)
182 panic("Huh? Expected `black' or `white', got `%s'\n",
188 input
[BLACK
] = INPUTF
;
189 input
[WHITE
] = INPUTF
;
192 case 0: /* user verses program */
194 input
[!color
] = PROGRAM
;
197 case 1: /* user verses user */
202 case 2: /* program verses program */
203 input
[BLACK
] = PROGRAM
;
204 input
[WHITE
] = PROGRAM
;
209 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
210 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
214 for (color
= BLACK
; ; color
= !color
) {
216 switch (input
[color
]) {
217 case INPUTF
: /* input comes from a file */
218 curmove
= readinput(inputfp
);
219 if (curmove
!= ILLEGAL
)
222 case 0: /* user verses program */
224 input
[!color
] = PROGRAM
;
227 case 1: /* user verses user */
232 case 2: /* program verses program */
233 input
[BLACK
] = PROGRAM
;
234 input
[WHITE
] = PROGRAM
;
237 plyr
[BLACK
] = input
[BLACK
] == USER
? "you" : prog
;
238 plyr
[WHITE
] = input
[WHITE
] == USER
? "you" : prog
;
242 case USER
: /* input comes from standard input */
246 if (!get_line(buf
, sizeof(buf
))) {
254 if (curmove
== SAVE
) {
257 ask("save file name? ");
258 (void)get_line(buf
, sizeof(buf
));
259 if ((fp
= fopen(buf
, "w")) == NULL
) {
260 misclog("cannot create save file");
263 for (i
= 0; i
< movenum
- 1; i
++)
269 if (curmove
!= RESIGN
&&
270 board
[curmove
].s_occ
!= EMPTY
) {
271 misclog("Illegal move");
277 case PROGRAM
: /* input comes from the program */
278 curmove
= pickmove(color
);
282 misclog(fmt
[color
], movenum
, stoc(curmove
));
284 if ((i
= makemove(color
, curmove
)) != MOVEOK
)
293 if (input
[color
] == PROGRAM
)
294 addstr("Ha ha, I won");
296 addstr("Rats! you won");
299 addstr("Wow! its a tie");
302 addstr("Illegal move");
310 if (get_line(buf
, sizeof(buf
)) &&
311 (buf
[0] == 'y' || buf
[0] == 'Y'))
313 if (strcmp(buf
, "save") == 0) {
316 ask("save file name? ");
317 (void)get_line(buf
, sizeof(buf
));
318 if ((fp
= fopen(buf
, "w")) == NULL
) {
319 misclog("cannot create save file");
322 for (i
= 0; i
< movenum
- 1; i
++)
343 while ((c
= getc(fp
)) != EOF
&& c
!= '\n' && pos
< sizeof(buf
) - 1)
351 * Handle strange situations.
356 int i
, n
, s1
, s2
, d1
, d2
;
361 struct combostr
*cbp
;
369 if (!get_line(input
, sizeof(input
)))
374 case 'q': /* conservative quit */
376 case 'd': /* set debug level */
377 debug
= input
[1] - '0';
378 debuglog("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
= input
[1] == 'b' ? BLACK
: WHITE
;
391 debuglog("suggest %c %s", i
== BLACK
? 'B' : 'W',
394 case 'f': /* go forward a move */
395 board
[movelog
[movenum
- 1]].s_occ
= movenum
& 1 ? BLACK
: WHITE
;
399 case 'l': /* print move history */
400 if (input
[1] == '\0') {
401 for (i
= 0; i
< movenum
- 1; i
++)
402 debuglog("%s", stoc(movelog
[i
]));
405 if ((fp
= fopen(input
+ 1, "w")) == NULL
)
407 for (i
= 0; i
< movenum
- 1; i
++) {
408 fprintf(fp
, "%s", stoc(movelog
[i
]));
409 if (++i
< movenum
- 1)
410 fprintf(fp
, " %s\n", stoc(movelog
[i
]));
418 /* avoid use w/o initialization on invalid input */
422 for (str
= input
+ 1; *str
; str
++)
424 for (d1
= 0; d1
< 4; d1
++)
425 if (str
[-1] == pdir
[d1
])
428 sp
= &board
[s1
= ctos(input
+ 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
;
440 debuglog("overlap %s%c,%s%c = %x", stoc(s1
), pdir
[d1
],
441 stoc(s2
), pdir
[d2
], overlap
[n
]);
444 sp
= &board
[i
= ctos(input
+ 1)];
445 debuglog("V %s %x/%d %d %x/%d %d %d %x", stoc(i
),
446 sp
->s_combo
[BLACK
].s
, sp
->s_level
[BLACK
],
448 sp
->s_combo
[WHITE
].s
, sp
->s_level
[WHITE
],
449 sp
->s_nforce
[WHITE
], sp
->s_wval
, sp
->s_flags
);
450 debuglog("FB %s %x %x %x %x", stoc(i
),
451 sp
->s_fval
[BLACK
][0].s
, sp
->s_fval
[BLACK
][1].s
,
452 sp
->s_fval
[BLACK
][2].s
, sp
->s_fval
[BLACK
][3].s
);
453 debuglog("FW %s %x %x %x %x", stoc(i
),
454 sp
->s_fval
[WHITE
][0].s
, sp
->s_fval
[WHITE
][1].s
,
455 sp
->s_fval
[WHITE
][2].s
, sp
->s_fval
[WHITE
][3].s
);
457 case 'e': /* e {b|w} [0-9] spot */
459 if (*str
>= '0' && *str
<= '9')
463 sp
= &board
[i
= ctos(str
)];
464 for (ep
= sp
->s_empty
; ep
; ep
= ep
->e_next
) {
467 if (cbp
->c_nframes
> n
)
469 if (cbp
->c_nframes
!= n
)
472 printcombo(cbp
, tmp
, sizeof(tmp
));
477 debuglog("Options are:");
478 debuglog("q - quit");
479 debuglog("c - continue");
480 debuglog("d# - set debug level to #");
481 debuglog("p# - print values at #");
488 * Display debug info.
491 debuglog(const char *fmt
, ...)
497 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
501 fprintf(debugfp
, "%s\n", buf
);
505 fprintf(stderr
, "%s\n", buf
);
509 misclog(const char *fmt
, ...)
515 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
519 fprintf(debugfp
, "%s\n", buf
);
530 bdisp(); /* show final board */
537 quitsig(int dummy __unused
)
546 panic(const char *fmt
, ...)
550 fprintf(stderr
, "%s: ", prog
);
552 vfprintf(stderr
, fmt
, ap
);
554 fprintf(stderr
, "\n");
556 fputs("resign\n", stdout
);