games: Massive style(9) cleanup commit. Reduces differences to NetBSD.
[dragonfly.git] / games / bs / bs.c
blob14aa2d8515d2a19da48d38a69189631f9c7ba679
1 /*-
2 * bs.c - original author: Bruce Holloway
3 * salvo option by: Chuck A DeGaul
4 * with improved user interface, autoconfiguration and code cleanup
5 * by Eric S. Raymond <esr@snark.thyrsus.com>
6 * v1.2 with color support and minor portability fixes, November 1990
7 * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
9 * $FreeBSD: src/games/bs/bs.c,v 1.9 2000/02/21 03:07:31 billf Exp $
10 * $DragonFly: src/games/bs/bs.c,v 1.7 2007/04/18 18:32:12 swildner Exp $
13 #include <assert.h>
14 #include <ctype.h>
15 #include <ncurses.h>
16 #include <signal.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #include <unistd.h>
23 * Constants for tuning the random-fire algorithm. It prefers moves that
24 * diagonal-stripe the board with a stripe separation of srchstep. If
25 * no such preferred moves are found, srchstep is decremented.
27 #define BEGINSTEP 3 /* initial value of srchstep */
29 /* miscellaneous constants */
30 #define SHIPTYPES 5
31 #define OTHER (1-turn)
32 #define PLAYER 0
33 #define COMPUTER 1
34 #define MARK_HIT 'H'
35 #define MARK_MISS 'o'
36 #define CTRLC '\003' /* used as terminate command */
37 #define FF '\014' /* used as redraw command */
39 /* coordinate handling */
40 #define BWIDTH 10
41 #define BDEPTH 10
43 /* display symbols */
44 #define SHOWHIT '*'
45 #define SHOWSPLASH ' '
46 #define IS_SHIP(c) isupper(c)
48 /* how to position us on player board */
49 #define PYBASE 3
50 #define PXBASE 3
51 #define PY(y) (PYBASE + (y))
52 #define PX(x) (PXBASE + (x)*3)
53 #define pgoto(y, x) move(PY(y), PX(x))
55 /* how to position us on cpu board */
56 #define CYBASE 3
57 #define CXBASE 48
58 #define CY(y) (CYBASE + (y))
59 #define CX(x) (CXBASE + (x)*3)
60 #define cgoto(y, x) move(CY(y), CX(x))
62 #define ONBOARD(x, y) (x >= 0 && x < BWIDTH && y >= 0 && y < BDEPTH)
64 /* other board locations */
65 #define COLWIDTH 80
66 #define PROMPTLINE 21 /* prompt line */
67 #define SYBASE CYBASE + BDEPTH + 3 /* move key diagram */
68 #define SXBASE 63
69 #define MYBASE SYBASE - 1 /* diagram caption */
70 #define MXBASE 64
71 #define HYBASE SYBASE - 1 /* help area */
72 #define HXBASE 0
74 /* this will need to be changed if BWIDTH changes */
75 static char numbers[] = " 0 1 2 3 4 5 6 7 8 9";
77 static char carrier[] = "Aircraft Carrier";
78 static char battle[] = "Battleship";
79 static char sub[] = "Submarine";
80 static char destroy[] = "Destroyer";
81 static char ptboat[] = "PT Boat";
83 static char name[40];
84 static char dftname[] = "stranger";
86 /* direction constants */
87 enum directions { E, SE, S, SW, W, NW, N, NE };
88 static int xincr[8] = {1, 1, 0, -1, -1, -1, 0, 1};
89 static int yincr[8] = {0, 1, 1, 1, 0, -1, -1, -1};
91 /* current ship position and direction */
92 static int curx = (BWIDTH / 2);
93 static int cury = (BDEPTH / 2);
95 typedef struct {
96 char *name; /* name of the ship type */
97 char hits; /* how many times has this ship been hit? */
98 char symbol; /* symbol for game purposes */
99 char length; /* length of ship */
100 char x, y; /* coordinates of ship start point */
101 enum directions dir; /* direction of `bow' */
102 bool placed; /* has it been placed on the board? */
104 ship_t;
106 ship_t plyship[SHIPTYPES] =
108 { carrier, 0, 'A', 5, 0, 0, E, FALSE},
109 { battle, 0, 'B', 4, 0, 0, E, FALSE},
110 { destroy, 0, 'D', 3, 0, 0, E, FALSE},
111 { sub, 0, 'S', 3, 0, 0, E, FALSE},
112 { ptboat, 0, 'P', 2, 0, 0, E, FALSE},
115 ship_t cpuship[SHIPTYPES] =
117 { carrier, 0, 'A', 5, 0, 0, E, FALSE},
118 { battle, 0, 'B', 4, 0, 0, E, FALSE},
119 { destroy, 0, 'D', 3, 0, 0, E, FALSE},
120 { sub, 0, 'S', 3, 0, 0, E, FALSE},
121 { ptboat, 0, 'P', 2, 0, 0, E, FALSE},
124 /* "Hits" board, and main board. */
125 static char hits[2][BWIDTH][BDEPTH], board[2][BWIDTH][BDEPTH];
127 static int turn; /* 0=player, 1=computer */
128 static int plywon=0, cpuwon=0; /* How many games has each won? */
130 static int salvo, blitz, closepack;
132 #define PR addstr
134 static bool checkplace (int, ship_t *, int);
135 static int getcoord (int);
136 int playagain (void);
138 /* end the game, either normally or due to signal */
139 static void
140 uninitgame(void)
142 clear();
143 refresh();
144 resetterm();
145 echo();
146 endwin();
147 exit(0);
150 static void
151 sighandler(__unused int sig)
153 uninitgame();
156 /* announce which game options are enabled */
157 static void
158 announceopts(void)
160 printw("Playing %s game (", (salvo || blitz || closepack) ?
161 "optional" : "standard");
163 if (salvo)
164 printw("salvo, ");
165 else
166 printw("nosalvo, ");
168 if (blitz)
169 printw("blitz, ");
170 else
171 printw("noblitz, ");
173 if (closepack)
174 printw("closepack)");
175 else
176 printw("noclosepack)");
179 static void
180 intro(void)
182 char *tmpname;
184 srandomdev();
186 tmpname = getlogin();
187 signal(SIGINT,sighandler);
188 signal(SIGINT,sighandler);
189 signal(SIGIOT,sighandler); /* for assert(3) */
190 if(signal(SIGQUIT,SIG_IGN) != SIG_IGN)
191 signal(SIGQUIT,sighandler);
193 if(tmpname != '\0') {
194 strcpy(name,tmpname);
195 name[0] = toupper(name[0]);
196 } else {
197 strcpy(name,dftname);
200 initscr();
201 #ifdef KEY_MIN
202 keypad(stdscr, TRUE);
203 #endif /* KEY_MIN */
204 saveterm();
205 nonl();
206 cbreak();
207 noecho();
209 #ifdef PENGUIN
210 clear();
211 mvaddstr(4,29,"Welcome to Battleship!");
212 move(8,0);
213 PR(" \\\n");
214 PR(" \\ \\ \\\n");
215 PR(" \\ \\ \\ \\ \\_____________\n");
216 PR(" \\ \\ \\_____________ \\ \\/ |\n");
217 PR(" \\ \\/ \\ \\/ |\n");
218 PR(" \\/ \\_____/ |__\n");
219 PR(" ________________/ |\n");
220 PR(" \\ S.S. Penguin |\n");
221 PR(" \\ /\n");
222 PR(" \\___________________________________________________/\n");
224 mvaddstr(22,27,"Hit any key to continue..."); refresh();
225 getch();
226 #endif /* PENGUIN */
228 #ifdef A_COLOR
229 start_color();
231 init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
232 init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
233 init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
234 init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
235 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
236 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
237 init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
238 init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
239 #endif /* A_COLOR */
243 /* print a message at the prompt line */
244 static void
245 prompt(int n, const char *f, ...)
247 char buf[COLWIDTH + 1];
248 va_list ap;
250 va_start(ap, f);
251 move(PROMPTLINE + n, 0);
252 clrtoeol();
253 vsnprintf(buf, COLWIDTH + 1, f, ap);
254 printw("%s", buf);
255 refresh();
256 va_end(ap);
259 static void
260 error(const char *s)
262 move(PROMPTLINE + 2, 0);
263 clrtoeol();
264 if (s) {
265 addstr(s);
266 beep();
270 static void
271 placeship(int b, ship_t *ss, int vis)
273 int l;
275 for(l = 0; l < ss->length; ++l) {
276 int newx = ss->x + l * xincr[ss->dir];
277 int newy = ss->y + l * yincr[ss->dir];
279 board[b][newx][newy] = ss->symbol;
280 if (vis) {
281 pgoto(newy, newx);
282 addch((chtype)ss->symbol);
285 ss->hits = 0;
288 static int
289 rnd(int n)
291 return(random() % n);
294 /* generate a valid random ship placement into px,py */
295 static void
296 randomplace(int b, ship_t *ss)
298 do {
299 ss->y = rnd(BDEPTH);
300 ss->x = rnd(BWIDTH);
301 ss->dir = rnd(2) ? E : S;
302 } while (!checkplace(b, ss, FALSE));
305 static void
306 initgame(void)
308 int i, j, unplaced;
309 ship_t *ss;
311 clear();
312 mvaddstr(0,35,"BATTLESHIPS");
313 move(PROMPTLINE + 2, 0);
314 announceopts();
316 bzero(board, sizeof(char) * BWIDTH * BDEPTH * 2);
317 bzero(hits, sizeof(char) * BWIDTH * BDEPTH * 2);
318 for (i = 0; i < SHIPTYPES; i++) {
319 ss = cpuship + i;
320 ss->x = ss->y = ss->dir = ss->hits = ss->placed = 0;
321 ss = plyship + i;
322 ss->x = ss->y = ss->dir = ss->hits = ss->placed = 0;
325 /* draw empty boards */
326 mvaddstr(PYBASE - 2, PXBASE + 5, "Main Board");
327 mvaddstr(PYBASE - 1, PXBASE - 3,numbers);
328 for(i=0; i < BDEPTH; ++i)
330 mvaddch(PYBASE + i, PXBASE - 3, i + 'A');
331 #ifdef A_COLOR
332 if (has_colors())
333 attron(COLOR_PAIR(COLOR_BLUE));
334 #endif /* A_COLOR */
335 addch(' ');
336 for (j = 0; j < BWIDTH; j++)
337 addstr(" . ");
338 #ifdef A_COLOR
339 attrset(0);
340 #endif /* A_COLOR */
341 addch(' ');
342 addch(i + 'A');
344 mvaddstr(PYBASE + BDEPTH, PXBASE - 3,numbers);
345 mvaddstr(CYBASE - 2, CXBASE + 7,"Hit/Miss Board");
346 mvaddstr(CYBASE - 1, CXBASE - 3, numbers);
347 for(i=0; i < BDEPTH; ++i)
349 mvaddch(CYBASE + i, CXBASE - 3, i + 'A');
350 #ifdef A_COLOR
351 if (has_colors())
352 attron(COLOR_PAIR(COLOR_BLUE));
353 #endif /* A_COLOR */
354 addch(' ');
355 for (j = 0; j < BWIDTH; j++)
356 addstr(" . ");
357 #ifdef A_COLOR
358 attrset(0);
359 #endif /* A_COLOR */
360 addch(' ');
361 addch(i + 'A');
364 mvaddstr(CYBASE + BDEPTH,CXBASE - 3,numbers);
366 mvprintw(HYBASE, HXBASE,
367 "To position your ships: move the cursor to a spot, then");
368 mvprintw(HYBASE+1,HXBASE,
369 "type the first letter of a ship type to select it, then");
370 mvprintw(HYBASE+2,HXBASE,
371 "type a direction ([hjkl] or [4862]), indicating how the");
372 mvprintw(HYBASE+3,HXBASE,
373 "ship should be pointed. You may also type a ship letter");
374 mvprintw(HYBASE+4,HXBASE,
375 "followed by `r' to position it randomly, or type `R' to");
376 mvprintw(HYBASE+5,HXBASE,
377 "place all remaining ships randomly.");
379 mvaddstr(MYBASE, MXBASE, "Aiming keys:");
380 mvaddstr(SYBASE, SXBASE, "y k u 7 8 9");
381 mvaddstr(SYBASE+1, SXBASE, " \\|/ \\|/ ");
382 mvaddstr(SYBASE+2, SXBASE, "h-+-l 4-+-6");
383 mvaddstr(SYBASE+3, SXBASE, " /|\\ /|\\ ");
384 mvaddstr(SYBASE+4, SXBASE, "b j n 1 2 3");
386 /* have the computer place ships */
387 for(ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
389 randomplace(COMPUTER, ss);
390 placeship(COMPUTER, ss, FALSE);
393 ss = NULL;
394 do {
395 char c, docked[SHIPTYPES + 2], *cp = docked;
397 /* figure which ships still wait to be placed */
398 *cp++ = 'R';
399 for (i = 0; i < SHIPTYPES; i++)
400 if (!plyship[i].placed)
401 *cp++ = plyship[i].symbol;
402 *cp = '\0';
404 /* get a command letter */
405 prompt(1, "Type one of [%s] to pick a ship.", docked+1);
406 do {
407 c = getcoord(PLAYER);
408 } while
409 (!strchr(docked, c));
411 if (c == 'R')
412 ungetch('R');
413 else
415 /* map that into the corresponding symbol */
416 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
417 if (ss->symbol == c)
418 break;
420 prompt(1, "Type one of [hjklrR] to place your %s.", ss->name);
421 pgoto(cury, curx);
424 do {
425 c = getch();
426 } while
427 (!strchr("hjklrR", c) || c == FF);
429 if (c == FF)
431 clearok(stdscr, TRUE);
432 refresh();
434 else if (c == 'r')
436 prompt(1, "Random-placing your %s", ss->name);
437 randomplace(PLAYER, ss);
438 placeship(PLAYER, ss, TRUE);
439 error(NULL);
440 ss->placed = TRUE;
442 else if (c == 'R')
444 prompt(1, "Placing the rest of your fleet at random...");
445 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
446 if (!ss->placed)
448 randomplace(PLAYER, ss);
449 placeship(PLAYER, ss, TRUE);
450 ss->placed = TRUE;
452 error(NULL);
454 else if (strchr("hjkl8462", c))
456 ss->x = curx;
457 ss->y = cury;
459 switch(c)
461 case 'k': case '8': ss->dir = N; break;
462 case 'j': case '2': ss->dir = S; break;
463 case 'h': case '4': ss->dir = W; break;
464 case 'l': case '6': ss->dir = E; break;
467 if (checkplace(PLAYER, ss, TRUE))
469 placeship(PLAYER, ss, TRUE);
470 error(NULL);
471 ss->placed = TRUE;
475 for (unplaced = i = 0; i < SHIPTYPES; i++)
476 unplaced += !plyship[i].placed;
477 } while
478 (unplaced);
480 turn = rnd(2);
482 mvprintw(HYBASE, HXBASE,
483 "To fire, move the cursor to your chosen aiming point ");
484 mvprintw(HYBASE+1, HXBASE,
485 "and strike any key other than a motion key. ");
486 mvprintw(HYBASE+2, HXBASE,
487 " ");
488 mvprintw(HYBASE+3, HXBASE,
489 " ");
490 mvprintw(HYBASE+4, HXBASE,
491 " ");
492 mvprintw(HYBASE+5, HXBASE,
493 " ");
495 prompt(0, "Press any key to start...");
496 getch();
499 static int
500 getcoord(int atcpu)
502 int ny, nx, c;
504 if (atcpu)
505 cgoto(cury,curx);
506 else
507 pgoto(cury, curx);
508 refresh();
509 for (;;)
511 if (atcpu)
513 mvprintw(CYBASE + BDEPTH+1, CXBASE+11, "(%d, %c)", curx, 'A'+cury);
514 cgoto(cury, curx);
516 else
518 mvprintw(PYBASE + BDEPTH+1, PXBASE+11, "(%d, %c)", curx, 'A'+cury);
519 pgoto(cury, curx);
522 switch(c = getch())
524 case 'k': case '8':
525 #ifdef KEY_MIN
526 case KEY_UP:
527 #endif /* KEY_MIN */
528 ny = cury+BDEPTH-1; nx = curx;
529 break;
530 case 'j': case '2':
531 #ifdef KEY_MIN
532 case KEY_DOWN:
533 #endif /* KEY_MIN */
534 ny = cury+1; nx = curx;
535 break;
536 case 'h': case '4':
537 #ifdef KEY_MIN
538 case KEY_LEFT:
539 #endif /* KEY_MIN */
540 ny = cury; nx = curx+BWIDTH-1;
541 break;
542 case 'l': case '6':
543 #ifdef KEY_MIN
544 case KEY_RIGHT:
545 #endif /* KEY_MIN */
546 ny = cury; nx = curx+1;
547 break;
548 case 'y': case '7':
549 #ifdef KEY_MIN
550 case KEY_A1:
551 #endif /* KEY_MIN */
552 ny = cury+BDEPTH-1; nx = curx+BWIDTH-1;
553 break;
554 case 'b': case '1':
555 #ifdef KEY_MIN
556 case KEY_C1:
557 #endif /* KEY_MIN */
558 ny = cury+1; nx = curx+BWIDTH-1;
559 break;
560 case 'u': case '9':
561 #ifdef KEY_MIN
562 case KEY_A3:
563 #endif /* KEY_MIN */
564 ny = cury+BDEPTH-1; nx = curx+1;
565 break;
566 case 'n': case '3':
567 #ifdef KEY_MIN
568 case KEY_C3:
569 #endif /* KEY_MIN */
570 ny = cury+1; nx = curx+1;
571 break;
572 case FF:
573 nx = curx; ny = cury;
574 clearok(stdscr, TRUE);
575 refresh();
576 break;
577 default:
578 if (atcpu)
579 mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, " ");
580 else
581 mvaddstr(PYBASE + BDEPTH + 1, PXBASE + 11, " ");
582 return(c);
585 curx = nx % BWIDTH;
586 cury = ny % BDEPTH;
590 /* is this location on the selected zboard adjacent to a ship? */
591 static int
592 collidecheck(int b, int y, int x)
594 int collide;
596 /* anything on the square */
597 if ((collide = IS_SHIP(board[b][x][y])) != 0)
598 return(collide);
600 /* anything on the neighbors */
601 if (!closepack) {
602 int i;
604 for (i = 0; i < 8; i++) {
605 int xend, yend;
607 yend = y + yincr[i];
608 xend = x + xincr[i];
609 if (ONBOARD(xend, yend))
610 collide += IS_SHIP(board[b][xend][yend]);
614 return(collide);
617 static bool
618 checkplace(int b, ship_t *ss, int vis)
620 int l, xend, yend;
622 /* first, check for board edges */
623 xend = ss->x + (ss->length - 1) * xincr[ss->dir];
624 yend = ss->y + (ss->length - 1) * yincr[ss->dir];
625 if (!ONBOARD(xend, yend)) {
626 if (vis) {
627 switch(rnd(3)) {
628 case 0:
629 error("Ship is hanging from the edge of the world");
630 break;
631 case 1:
632 error("Try fitting it on the board");
633 break;
634 case 2:
635 error("Figure I won't find it if you put it there?");
636 break;
639 return(0);
642 for(l = 0; l < ss->length; ++l) {
643 if(collidecheck(b, ss->y+l*yincr[ss->dir], ss->x+l*xincr[ss->dir])) {
644 if (vis) {
645 switch(rnd(3)) {
646 case 0:
647 error("There's already a ship there");
648 break;
649 case 1:
650 error("Collision alert! Aaaaaagh!");
651 break;
652 case 2:
653 error("Er, Admiral, what about the other ship?");
654 break;
657 return(FALSE);
660 return(TRUE);
663 static int
664 awinna(void)
666 int i, j;
667 ship_t *ss;
669 for(i=0; i<2; ++i)
671 ss = (i) ? cpuship : plyship;
672 for(j=0; j < SHIPTYPES; ++j, ++ss)
673 if(ss->length > ss->hits)
674 break;
675 if (j == SHIPTYPES)
676 return(OTHER);
678 return(-1);
681 /* a hit on the targeted ship */
682 static ship_t *
683 hitship(int x, int y)
685 ship_t *sb, *ss;
686 char sym;
687 int oldx, oldy;
689 getyx(stdscr, oldy, oldx);
690 sb = (turn) ? plyship : cpuship;
691 if(!(sym = board[OTHER][x][y]))
692 return(NULL);
693 for(ss = sb; ss < sb + SHIPTYPES; ++ss)
694 if(ss->symbol == sym)
696 if (++ss->hits < ss->length) { /* still afloat? */
697 return(NULL);
698 } else { /* sunk */
699 int i, j;
701 if (!closepack) {
702 for (j = -1; j <= 1; j++) {
703 int bx = ss->x + j * xincr[(ss->dir + 2) % 8];
704 int by = ss->y + j * yincr[(ss->dir + 2) % 8];
706 for (i = -1; i <= ss->length; ++i) {
707 int cx, cy;
709 cx = bx + i * xincr[ss->dir];
710 cy = by + i * yincr[ss->dir];
711 if (ONBOARD(cx, cy)) {
712 hits[turn][cx][cy] = MARK_MISS;
713 if (turn % 2 == PLAYER) {
714 cgoto(cy, cx);
715 #ifdef A_COLOR
716 if (has_colors())
717 attron(COLOR_PAIR(COLOR_GREEN));
718 #endif /* A_COLOR */
720 addch(MARK_MISS);
721 #ifdef A_COLOR
722 attrset(0);
723 #endif /* A_COLOR */
730 for (i = 0; i < ss->length; ++i)
732 int dx = ss->x + i * xincr[ss->dir];
733 int dy = ss->y + i * yincr[ss->dir];
735 hits[turn][dx][dy] = ss->symbol;
736 if (turn % 2 == PLAYER)
738 cgoto(dy, dx);
739 addch(ss->symbol);
743 move(oldy, oldx);
744 return(ss);
747 move(oldy, oldx);
748 return(NULL);
751 static int
752 plyturn(void)
754 ship_t *ss;
755 bool hit;
756 char const *m;
758 m = NULL;
759 prompt(1, "Where do you want to shoot? ");
760 for (;;)
762 getcoord(COMPUTER);
763 if (hits[PLAYER][curx][cury])
765 prompt(1, "You shelled this spot already! Try again.");
766 beep();
768 else
769 break;
771 hit = IS_SHIP(board[COMPUTER][curx][cury]);
772 hits[PLAYER][curx][cury] = hit ? MARK_HIT : MARK_MISS;
773 cgoto(cury, curx);
774 #ifdef A_COLOR
775 if (has_colors()) {
776 if (hit)
777 attron(COLOR_PAIR(COLOR_RED));
778 else
779 attron(COLOR_PAIR(COLOR_GREEN));
781 #endif /* A_COLOR */
782 addch((chtype)hits[PLAYER][curx][cury]);
783 #ifdef A_COLOR
784 attrset(0);
785 #endif /* A_COLOR */
787 prompt(1, "You %s.", hit ? "scored a hit" : "missed");
788 if(hit && (ss = hitship(curx, cury)))
790 switch(rnd(5))
792 case 0:
793 m = " You sank my %s!";
794 break;
795 case 1:
796 m = " I have this sinking feeling about my %s....";
797 break;
798 case 2:
799 m = " My %s has gone to Davy Jones's locker!";
800 break;
801 case 3:
802 m = " Glub, glub -- my %s is headed for the bottom!";
803 break;
804 case 4:
805 m = " You'll pick up survivors from my %s, I hope...!";
806 break;
808 printw(m, ss->name);
809 beep();
810 return(awinna() == -1);
812 return(hit);
815 static int
816 sgetc(const char *s)
818 const char *s1;
819 int ch;
821 refresh();
822 for(;;) {
823 ch = getch();
824 if (islower(ch))
825 ch = toupper(ch);
827 if (ch == CTRLC)
828 uninitgame();
830 for (s1=s; *s1 && ch != *s1; ++s1)
831 continue;
833 if (*s1) {
834 addch((chtype)ch);
835 refresh();
836 return(ch);
841 /* random-fire routine -- implements simple diagonal-striping strategy */
842 static void
843 randomfire(int *px, int *py)
845 static int turncount = 0;
846 static int srchstep = BEGINSTEP;
847 static int huntoffs; /* Offset on search strategy */
848 int ypossible[BWIDTH * BDEPTH], xpossible[BWIDTH * BDEPTH], nposs;
849 int ypreferred[BWIDTH * BDEPTH], xpreferred[BWIDTH * BDEPTH], npref;
850 int x, y, i;
852 if (turncount++ == 0)
853 huntoffs = rnd(srchstep);
855 /* first, list all possible moves */
856 nposs = npref = 0;
857 for (x = 0; x < BWIDTH; x++)
858 for (y = 0; y < BDEPTH; y++)
859 if (!hits[COMPUTER][x][y])
861 xpossible[nposs] = x;
862 ypossible[nposs] = y;
863 nposs++;
864 if (((x+huntoffs) % srchstep) != (y % srchstep))
866 xpreferred[npref] = x;
867 ypreferred[npref] = y;
868 npref++;
872 if (npref)
874 i = rnd(npref);
876 *px = xpreferred[i];
877 *py = ypreferred[i];
879 else if (nposs)
881 i = rnd(nposs);
883 *px = xpossible[i];
884 *py = ypossible[i];
886 if (srchstep > 1)
887 --srchstep;
889 else
891 error("No moves possible?? Help!");
892 exit(1);
893 /*NOTREACHED*/
897 #define S_MISS 0
898 #define S_HIT 1
899 #define S_SUNK -1
901 /* fire away at given location */
902 static bool
903 cpufire(int x, int y)
905 bool hit, sunk;
906 ship_t *ss;
908 ss = NULL;
909 hits[COMPUTER][x][y] = (hit = (board[PLAYER][x][y])) ? MARK_HIT : MARK_MISS;
910 mvprintw(PROMPTLINE, 0,
911 "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" : "miss");
912 ss = hitship(x, y);
913 sunk = hit && ss;
914 if (sunk)
915 printw(" I've sunk your %s", ss->name);
916 clrtoeol();
918 pgoto(y, x);
919 #ifdef A_COLOR
920 if (has_colors()) {
921 if (hit)
922 attron(COLOR_PAIR(COLOR_RED));
923 else
924 attron(COLOR_PAIR(COLOR_GREEN));
926 #endif /* A_COLOR */
927 addch((chtype)(hit ? SHOWHIT : SHOWSPLASH));
928 #ifdef A_COLOR
929 attrset(0);
930 #endif /* A_COLOR */
932 return(hit ? (sunk ? S_SUNK : S_HIT) : S_MISS);
936 * This code implements a fairly irregular FSM, so please forgive the rampant
937 * unstructuredness below. The five labels are states which need to be held
938 * between computer turns.
940 static bool
941 cputurn(void)
943 #define POSSIBLE(x, y) (ONBOARD(x, y) && !hits[COMPUTER][x][y])
944 #define RANDOM_FIRE 0
945 #define RANDOM_HIT 1
946 #define HUNT_DIRECT 2
947 #define FIRST_PASS 3
948 #define REVERSE_JUMP 4
949 #define SECOND_PASS 5
950 static int next = RANDOM_FIRE;
951 static bool used[4];
952 static ship_t ts;
953 int navail, x, y, d, n, hit = S_MISS;
955 switch(next)
957 case RANDOM_FIRE: /* last shot was random and missed */
958 refire:
959 randomfire(&x, &y);
960 if (!(hit = cpufire(x, y)))
961 next = RANDOM_FIRE;
962 else
964 ts.x = x; ts.y = y;
965 ts.hits = 1;
966 next = (hit == S_SUNK) ? RANDOM_FIRE : RANDOM_HIT;
968 break;
970 case RANDOM_HIT: /* last shot was random and hit */
971 used[E/2] = used[S/2] = used[W/2] = used[N/2] = FALSE;
972 /* FALLTHROUGH */
974 case HUNT_DIRECT: /* last shot hit, we're looking for ship's long axis */
975 for (d = navail = 0; d < 4; d++)
977 x = ts.x + xincr[d*2]; y = ts.y + yincr[d*2];
978 if (!used[d] && POSSIBLE(x, y))
979 navail++;
980 else
981 used[d] = TRUE;
983 if (navail == 0) /* no valid places for shots adjacent... */
984 goto refire; /* ...so we must random-fire */
985 else
987 for (d = 0, n = rnd(navail) + 1; n; n--)
988 while (used[d])
989 d++;
991 assert(d <= 4);
993 used[d] = FALSE;
994 x = ts.x + xincr[d*2];
995 y = ts.y + yincr[d*2];
997 assert(POSSIBLE(x, y));
999 if (!(hit = cpufire(x, y)))
1000 next = HUNT_DIRECT;
1001 else
1003 ts.x = x; ts.y = y; ts.dir = d*2; ts.hits++;
1004 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1007 break;
1009 case FIRST_PASS: /* we have a start and a direction now */
1010 x = ts.x + xincr[ts.dir];
1011 y = ts.y + yincr[ts.dir];
1012 if (POSSIBLE(x, y) && (hit = cpufire(x, y)))
1014 ts.x = x; ts.y = y; ts.hits++;
1015 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1017 else
1018 next = REVERSE_JUMP;
1019 break;
1021 case REVERSE_JUMP: /* nail down the ship's other end */
1022 d = ts.dir + 4;
1023 x = ts.x + ts.hits * xincr[d];
1024 y = ts.y + ts.hits * yincr[d];
1025 if (POSSIBLE(x, y) && (hit = cpufire(x, y)))
1027 ts.x = x; ts.y = y; ts.dir = d; ts.hits++;
1028 next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1030 else
1031 next = RANDOM_FIRE;
1032 break;
1034 case SECOND_PASS: /* kill squares not caught on first pass */
1035 x = ts.x + xincr[ts.dir];
1036 y = ts.y + yincr[ts.dir];
1037 if (POSSIBLE(x, y) && (hit = cpufire(x, y)))
1039 ts.x = x; ts.y = y; ts.hits++;
1040 next = (hit == S_SUNK) ? RANDOM_FIRE: SECOND_PASS;
1041 break;
1043 else
1044 next = RANDOM_FIRE;
1045 break;
1048 /* check for continuation and/or winner */
1049 if (salvo)
1051 refresh();
1052 sleep(1);
1054 if (awinna() != -1)
1055 return(FALSE);
1057 #ifdef DEBUG
1058 mvprintw(PROMPTLINE + 2, 0,
1059 "New state %d, x=%d, y=%d, d=%d",
1060 next, x, y, d);
1061 #endif /* DEBUG */
1062 return(hit);
1066 playagain(void)
1068 int j;
1069 ship_t *ss;
1071 for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++) {
1072 for(j = 0; j < ss->length; j++) {
1073 cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir]);
1074 addch((chtype)ss->symbol);
1078 if(awinna()) {
1079 ++cpuwon;
1080 } else {
1081 ++plywon;
1084 j = 18 + strlen(name);
1085 if(plywon >= 10) {
1086 ++j;
1087 } else if(cpuwon >= 10) {
1088 ++j;
1091 mvprintw(1,(COLWIDTH-j)/2,
1092 "%s: %d Computer: %d",name,plywon,cpuwon);
1094 prompt(2, (awinna()) ? "Want to be humiliated again, %s [yn]? "
1095 : "Going to give me a chance for revenge, %s [yn]? ",name);
1096 return(sgetc("YN") == 'Y');
1099 static void
1100 usage(void)
1102 fprintf(stderr, "Usage: battle [-s | -b] [-c]\n");
1103 fprintf(stderr, "\tWhere the options are:\n");
1104 fprintf(stderr, "\t-s : salvo - One shot per ship in play\n");
1105 fprintf(stderr, "\t-b : blitz - Fire until you miss\n");
1106 fprintf(stderr, "\t-c : closepack - Ships may be adjacent\n");
1107 fprintf(stderr, "Blitz and Salvo are mutually exclusive\n");
1108 exit(1);
1111 static int
1112 scount(int who)
1114 int i, shots;
1115 ship_t *sp;
1117 if (who)
1118 sp = cpuship; /* count cpu shots */
1119 else
1120 sp = plyship; /* count player shots */
1122 for (i=0, shots = 0; i < SHIPTYPES; i++, sp++)
1124 if (sp->hits >= sp->length)
1125 continue; /* dead ship */
1126 else
1127 shots++;
1129 return(shots);
1133 main(int argc, char **argv)
1135 int ch;
1137 /* revoke */
1138 setgid(getgid());
1140 while ((ch = getopt(argc, argv, "bsc")) != -1) {
1141 switch (ch) {
1142 case 'b':
1143 blitz = 1;
1144 break;
1145 case 's':
1146 salvo = 1;
1147 break;
1148 case 'c':
1149 closepack = 1;
1150 break;
1151 case '?':
1152 default:
1153 usage();
1156 argc -= optind;
1157 argv += optind;
1159 if (blitz && salvo)
1160 usage();
1162 intro();
1164 do {
1165 initgame();
1166 while(awinna() == -1) {
1167 if (blitz) {
1168 while(turn ? cputurn() : plyturn())
1169 continue;
1170 } else if (salvo) {
1171 int i;
1173 i = scount(turn);
1174 while (i--) {
1175 if (turn) {
1176 if (cputurn() && awinna() != -1)
1177 i = 0;
1178 } else {
1179 if (plyturn() && awinna() != -1)
1180 i = 0;
1183 } else { /* Normal game */
1184 if(turn)
1185 cputurn();
1186 else
1187 plyturn();
1189 turn = OTHER;
1191 } while (playagain());
1193 uninitgame();
1194 exit(0);