1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * bs.c - original author: Bruce Holloway
30 * salvo option by: Chuck A DeGaul
31 * with improved user interface, autoconfiguration and code cleanup
32 * by Eric S. Raymond <esr@snark.thyrsus.com>
33 * v1.2 with color support and minor portability fixes, November 1990
34 * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
35 * v2.1 with ncurses mouse support, September 1995
37 * $Id: bs.c,v 1.73 2018/05/12 15:07:51 tom Exp $
40 #include <test.priv.h>
45 #define SIGIOT SIGABRT
48 static int getcoord(int);
51 * Constants for tuning the random-fire algorithm. It prefers moves that
52 * diagonal-stripe the board with a stripe separation of srchstep. If
53 * no such preferred moves are found, srchstep is decremented.
55 #define BEGINSTEP 3 /* initial value of srchstep */
57 /* miscellaneous constants */
59 #define OTHER (1-turn)
64 #define CTRLC '\003' /* used as terminate command */
65 #define FF '\014' /* used as redraw command */
67 #define is_QUIT(c) ((c) == CTRLC || (c) == QUIT)
69 /* coordinate handling */
75 #define SHOWSPLASH ' '
76 #define IS_SHIP(c) (isupper(UChar(c)) ? TRUE : FALSE)
78 /* how to position us on player board */
81 #define PY(y) (PYBASE + (y))
82 #define PX(x) (PXBASE + (x)*3)
83 #define pgoto(y, x) (void)move(PY(y), PX(x))
85 /* how to position us on cpu board */
88 #define CY(y) (CYBASE + (y))
89 #define CX(x) (CXBASE + (x)*3)
90 #define CYINV(y) ((y) - CYBASE)
91 #define CXINV(x) (((x) - CXBASE) / 3)
92 #define cgoto(y, x) (void)move(CY(y), CX(x))
94 #define ONBOARD(x, y) (x >= 0 && x < BWIDTH && y >= 0 && y < BDEPTH)
96 /* other board locations */
98 #define PROMPTLINE 21 /* prompt line */
99 #define SYBASE CYBASE + BDEPTH + 3 /* move key diagram */
101 #define MYBASE SYBASE - 1 /* diagram caption */
103 #define HYBASE SYBASE - 1 /* help area */
106 /* this will need to be changed if BWIDTH changes */
107 static char numbers[] = " 0 1 2 3 4 5 6 7 8 9";
109 static char carrier[] = "Aircraft Carrier";
110 static char battle[] = "Battleship";
111 static char sub[] = "Submarine";
112 static char destroy[] = "Destroyer";
113 static char ptboat[] = "PT Boat";
115 static char *your_name;
116 static char dftname[] = "stranger";
118 /* direction constants */
130 static int xincr[dir_MAX + 2] =
131 {1, 1, 0, -1, -1, -1, 0, 1};
132 static int yincr[dir_MAX + 2] =
133 {0, 1, 1, 1, 0, -1, -1, -1};
135 /* current ship position and direction */
136 static int curx = (BWIDTH / 2);
137 static int cury = (BDEPTH / 2);
140 char *name; /* name of the ship type */
141 int hits; /* how many times has this ship been hit? */
142 char symbol; /* symbol for game purposes */
143 int length; /* length of ship */
144 int x, y; /* coordinates of ship start point */
145 int dir; /* direction of `bow' */
146 bool placed; /* has it been placed on the board? */
149 static bool checkplace(int b, ship_t * ss, int vis);
151 #define SHIPIT(name, symbol, length) { name, 0, symbol, length, 0,0, 0, FALSE }
153 /* "ply=player", "cpu=computer" */
154 static ship_t plyship[SHIPTYPES] =
156 SHIPIT(carrier, 'A', 5),
157 SHIPIT(battle, 'B', 4),
158 SHIPIT(destroy, 'D', 3),
160 SHIPIT(ptboat, 'P', 2),
163 static ship_t cpuship[SHIPTYPES] =
165 SHIPIT(carrier, 'A', 5),
166 SHIPIT(battle, 'B', 4),
167 SHIPIT(destroy, 'D', 3),
169 SHIPIT(ptboat, 'P', 2),
172 /* "Hits" board, and main board. */
173 static char hits[2][BWIDTH][BDEPTH];
174 static char board[2][BWIDTH][BDEPTH];
176 static int turn; /* 0=player, 1=computer */
177 static int plywon = 0, cpuwon = 0; /* How many games has each won? */
179 static int salvo, blitz, closepack;
181 #define PR (void)addstr
183 static void uninitgame(int sig) GCC_NORETURN;
186 uninitgame(int sig GCC_UNUSED)
187 /* end the game, either normally or due to signal */
191 (void) reset_shell_mode();
195 ExitProgram(sig ? EXIT_FAILURE : EXIT_SUCCESS);
200 /* announce which game options are enabled */
202 if (salvo || blitz || closepack) {
203 (void) printw("Playing optional game (");
205 (void) printw("salvo, ");
207 (void) printw("nosalvo, ");
209 (void) printw("blitz ");
211 (void) printw("noblitz, ");
213 (void) printw("closepack)");
215 (void) printw("noclosepack)");
218 "Playing standard game (noblitz, nosalvo, noclosepack)");
226 srand((unsigned) (time(0L) + getpid())); /* Kick the random number generator */
228 InitAndCatch(initscr(), uninitgame);
230 if ((tmpname = getlogin()) != 0 &&
231 (your_name = strdup(tmpname)) != 0) {
232 your_name[0] = (char) toupper(UChar(your_name[0]));
234 your_name = strdup(dftname);
237 keypad(stdscr, TRUE);
238 (void) def_prog_mode();
245 MvAddStr(4, 29, "Welcome to Battleship!");
249 PR(" \\ \\ \\ \\ \\_____________\n");
250 PR(" \\ \\ \\_____________ \\ \\/ |\n");
251 PR(" \\ \\/ \\ \\/ |\n");
252 PR(" \\/ \\_____/ |__\n");
253 PR(" ________________/ |\n");
254 PR(" \\ S.S. Penguin |\n");
256 PR(" \\___________________________________________________/\n");
258 MvAddStr(22, 27, "Hit any key to continue...");
266 init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
267 init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
268 init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
269 init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
270 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
271 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
272 init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
273 init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
276 #ifdef NCURSES_MOUSE_VERSION
277 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
278 #endif /* NCURSES_MOUSE_VERSION */
283 prompt(int n, NCURSES_CONST char *f, const char *s)
284 /* print a message at the prompt line */
286 (void) move(PROMPTLINE + n, 0);
293 error(NCURSES_CONST char *s)
295 (void) move(PROMPTLINE + 2, 0);
304 placeship(int b, ship_t * ss, int vis)
308 for (l = 0; l < ss->length; ++l) {
309 int newx = ss->x + l * xincr[ss->dir];
310 int newy = ss->y + l * yincr[ss->dir];
312 board[b][newx][newy] = ss->symbol;
324 return (((rand() & 0x7FFF) % n));
328 randomplace(int b, ship_t * ss)
329 /* generate a valid random ship placement into px,py */
333 ss->dir = rnd(2) ? dir_E : dir_S;
334 ss->x = rnd(BWIDTH - (ss->dir == dir_E ? ss->length : 0));
335 ss->y = rnd(BDEPTH - (ss->dir == dir_S ? ss->length : 0));
337 (!checkplace(b, ss, FALSE));
347 MvAddStr(0, 35, "BATTLESHIPS");
348 (void) move(PROMPTLINE + 2, 0);
351 memset(board, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
352 memset(hits, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
353 for (i = 0; i < SHIPTYPES; i++) {
371 /* draw empty boards */
372 MvAddStr(PYBASE - 2, PXBASE + 5, "Main Board");
373 MvAddStr(PYBASE - 1, PXBASE - 3, numbers);
374 for (i = 0; i < BDEPTH; ++i) {
375 MvAddCh(PYBASE + i, PXBASE - 3, (chtype) (i + 'A'));
378 attron(COLOR_PAIR(COLOR_BLUE));
381 for (j = 0; j < BWIDTH; j++)
382 (void) addstr(" . ");
389 MvAddStr(PYBASE + BDEPTH, PXBASE - 3, numbers);
390 MvAddStr(CYBASE - 2, CXBASE + 7, "Hit/Miss Board");
391 MvAddStr(CYBASE - 1, CXBASE - 3, numbers);
392 for (i = 0; i < BDEPTH; ++i) {
393 MvAddCh(CYBASE + i, CXBASE - 3, (chtype) (i + 'A'));
396 attron(COLOR_PAIR(COLOR_BLUE));
399 for (j = 0; j < BWIDTH; j++)
400 (void) addstr(" . ");
408 MvAddStr(CYBASE + BDEPTH, CXBASE - 3, numbers);
410 MvPrintw(HYBASE, HXBASE,
411 "To position your ships: move the cursor to a spot, then");
412 MvPrintw(HYBASE + 1, HXBASE,
413 "type the first letter of a ship type to select it, then");
414 MvPrintw(HYBASE + 2, HXBASE,
415 "type a direction ([hjkl] or [4862]), indicating how the");
416 MvPrintw(HYBASE + 3, HXBASE,
417 "ship should be pointed. You may also type a ship letter");
418 MvPrintw(HYBASE + 4, HXBASE,
419 "followed by `r' to position it randomly, or type `R' to");
420 MvPrintw(HYBASE + 5, HXBASE,
421 "place all remaining ships randomly.");
423 MvAddStr(MYBASE, MXBASE, "Aiming keys:");
424 MvAddStr(SYBASE, SXBASE, "y k u 7 8 9");
425 MvAddStr(SYBASE + 1, SXBASE, " \\|/ \\|/ ");
426 MvAddStr(SYBASE + 2, SXBASE, "h-+-l 4-+-6");
427 MvAddStr(SYBASE + 3, SXBASE, " /|\\ /|\\ ");
428 MvAddStr(SYBASE + 4, SXBASE, "b j n 1 2 3");
430 /* have the computer place ships */
431 for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++) {
432 randomplace(COMPUTER, ss);
433 placeship(COMPUTER, ss, FALSE);
437 char c, docked[SHIPTYPES + 2], *cp = docked;
439 ss = (ship_t *) NULL;
441 /* figure which ships still wait to be placed */
443 for (i = 0; i < SHIPTYPES; i++)
444 if (!plyship[i].placed)
445 *cp++ = plyship[i].symbol;
448 /* get a command letter */
449 prompt(1, "Type one of [%s] to pick a ship.", docked + 1);
451 c = (char) getcoord(PLAYER);
453 (!(strchr) (docked, c));
458 /* map that into the corresponding symbol */
459 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
463 prompt(1, "Type one of [hjklrR] to place your %s.", ss->name);
470 (!(strchr("hjkl8462rR", c) || c == FF || is_QUIT(c)));
474 } else if (c == FF) {
475 (void) clearok(stdscr, TRUE);
477 } else if (ss == 0) {
478 beep(); /* simple to verify, unlikely to happen */
479 } else if (c == 'r') {
480 prompt(1, "Random-placing your %s", ss->name);
481 randomplace(PLAYER, ss);
482 placeship(PLAYER, ss, TRUE);
483 error((char *) NULL);
485 } else if (c == 'R') {
486 prompt(1, "Placing the rest of your fleet at random...", "");
487 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
489 randomplace(PLAYER, ss);
490 placeship(PLAYER, ss, TRUE);
493 error((char *) NULL);
494 } else if (strchr("hjkl8462", c)) {
517 if (checkplace(PLAYER, ss, TRUE)) {
518 placeship(PLAYER, ss, TRUE);
519 error((char *) NULL);
524 for (unplaced = i = 0; i < SHIPTYPES; i++)
525 unplaced += !plyship[i].placed;
531 MvPrintw(HYBASE, HXBASE,
532 "To fire, move the cursor to your chosen aiming point ");
533 MvPrintw(HYBASE + 1, HXBASE,
534 "and strike any key other than a motion key. ");
535 MvPrintw(HYBASE + 2, HXBASE,
537 MvPrintw(HYBASE + 3, HXBASE,
539 MvPrintw(HYBASE + 4, HXBASE,
541 MvPrintw(HYBASE + 5, HXBASE,
544 (void) prompt(0, "Press any key to start...", "");
560 MvPrintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
564 MvPrintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
569 switch (c = getch()) {
573 ny = cury + BDEPTH - 1;
586 nx = curx + BWIDTH - 1;
597 ny = cury + BDEPTH - 1;
598 nx = curx + BWIDTH - 1;
604 nx = curx + BWIDTH - 1;
609 ny = cury + BDEPTH - 1;
621 (void) clearok(stdscr, TRUE);
624 #ifdef NCURSES_MOUSE_VERSION
631 && myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
632 && myevent.x >= CX(0) && myevent.x <= CX(BDEPTH)) {
633 curx = CXINV(myevent.x);
634 cury = CYINV(myevent.y);
641 /* no fall through */
642 #endif /* NCURSES_MOUSE_VERSION */
646 MvAddStr(CYBASE + BDEPTH + 1, CXBASE + 11, " ");
648 MvAddStr(PYBASE + BDEPTH + 1, PXBASE + 11, " ");
658 collidecheck(int b, int y, int x)
659 /* is this location on the selected zboard adjacent to a ship? */
663 /* anything on the square */
664 if ((collide = IS_SHIP(board[b][x][y])) != FALSE)
667 /* anything on the neighbors */
671 for (i = 0; i < dir_MAX; i++) {
676 if (ONBOARD(xend, yend)
677 && IS_SHIP(board[b][xend][yend])) {
687 checkplace(int b, ship_t * ss, int vis)
691 /* first, check for board edges */
692 xend = ss->x + (ss->length - 1) * xincr[ss->dir];
693 yend = ss->y + (ss->length - 1) * yincr[ss->dir];
694 if (!ONBOARD(xend, yend)) {
698 error("Ship is hanging from the edge of the world");
701 error("Try fitting it on the board");
704 error("Figure I won't find it if you put it there?");
710 for (l = 0; l < ss->length; ++l) {
711 if (collidecheck(b, ss->y + l * yincr[ss->dir], ss->x + l * xincr[ss->dir])) {
715 error("There's already a ship there");
718 error("Collision alert! Aaaaaagh!");
721 error("Er, Admiral, what about the other ship?");
736 for (i = 0; i < 2; ++i) {
737 ss = (i) ? cpuship : plyship;
738 for (j = 0; j < SHIPTYPES; ++j, ++ss)
739 if (ss->length > ss->hits)
748 hitship(int x, int y)
749 /* register a hit on the targeted ship */
755 getyx(stdscr, oldy, oldx);
756 sb = (turn) ? plyship : cpuship;
757 if ((sym = board[OTHER][x][y]) == 0)
758 return ((ship_t *) NULL);
759 for (ss = sb; ss < sb + SHIPTYPES; ++ss)
760 if (ss->symbol == sym) {
761 if (++ss->hits < ss->length) /* still afloat? */
762 return ((ship_t *) NULL);
767 for (j = -1; j <= 1; j++) {
768 int bx = ss->x + j * xincr[(ss->dir + 2) % dir_MAX];
769 int by = ss->y + j * yincr[(ss->dir + 2) % dir_MAX];
771 for (i = -1; i <= ss->length; ++i) {
774 x1 = bx + i * xincr[ss->dir];
775 y1 = by + i * yincr[ss->dir];
776 if (ONBOARD(x1, y1)) {
777 hits[turn][x1][y1] = MARK_MISS;
778 if (turn % 2 == PLAYER) {
782 attron(COLOR_PAIR(COLOR_GREEN));
796 for (i = 0; i < ss->length; ++i) {
797 int x1 = ss->x + i * xincr[ss->dir];
798 int y1 = ss->y + i * yincr[ss->dir];
800 hits[turn][x1][y1] = ss->symbol;
801 if (turn % 2 == PLAYER) {
808 attron(COLOR_PAIR(COLOR_RED));
817 (void) move(oldy, oldx);
821 (void) move(oldy, oldx);
822 return ((ship_t *) NULL);
830 NCURSES_CONST char *m = NULL;
832 prompt(1, "Where do you want to shoot? ", "");
834 (void) getcoord(COMPUTER);
835 if (hits[PLAYER][curx][cury]) {
836 prompt(1, "You shelled this spot already! Try again.", "");
841 hit = IS_SHIP(board[COMPUTER][curx][cury]);
842 hits[PLAYER][curx][cury] = (char) (hit ? MARK_HIT : MARK_MISS);
847 attron(COLOR_PAIR(COLOR_RED));
849 attron(COLOR_PAIR(COLOR_GREEN));
852 AddCh(hits[PLAYER][curx][cury]);
857 prompt(1, "You %s.", hit ? "scored a hit" : "missed");
858 if (hit && (ss = hitship(curx, cury))) {
861 m = " You sank my %s!";
864 m = " I have this sinking feeling about my %s....";
867 m = " My %s has gone to Davy Jones's locker!";
870 m = " Glub, glub -- my %s is headed for the bottom!";
873 m = " You'll pick up survivors from my %s, I hope...!";
877 (void) printw(m, ss->name);
897 for (s1 = s; *s1 && ch != *s1; ++s1)
908 randomfire(int *px, int *py)
909 /* random-fire routine -- implements simple diagonal-striping strategy */
911 static int turncount = 0;
912 static int srchstep = BEGINSTEP;
913 static int huntoffs; /* Offset on search strategy */
914 int ypossible[BWIDTH * BDEPTH], xpossible[BWIDTH * BDEPTH], nposs;
915 int ypreferred[BWIDTH * BDEPTH], xpreferred[BWIDTH * BDEPTH], npref;
918 if (turncount++ == 0)
919 huntoffs = rnd(srchstep);
921 /* first, list all possible moves */
923 for (x = 0; x < BWIDTH; x++)
924 for (y = 0; y < BDEPTH; y++)
925 if (!hits[COMPUTER][x][y]) {
926 xpossible[nposs] = x;
927 ypossible[nposs] = y;
929 if (((x + huntoffs) % srchstep) != (y % srchstep)) {
930 xpreferred[npref] = x;
931 ypreferred[npref] = y;
950 error("No moves possible?? Help!");
951 ExitProgram(EXIT_FAILURE);
961 cpufire(int x, int y)
962 /* fire away at given location */
967 hit = (bool) board[PLAYER][x][y];
968 hits[COMPUTER][x][y] = (hit ? MARK_HIT : MARK_MISS);
969 MvPrintw(PROMPTLINE, 0,
970 "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" :
972 if ((sunk = (hit && (ss = hitship(x, y)))) != 0)
973 (void) printw(" I've sunk your %s", ss->name);
980 attron(COLOR_PAIR(COLOR_RED));
982 attron(COLOR_PAIR(COLOR_GREEN));
985 AddCh((hit ? SHOWHIT : SHOWSPLASH));
990 return hit ? (sunk ? S_SUNK : S_HIT) : S_MISS;
994 * This code implements a fairly irregular FSM, so please forgive the rampant
995 * unstructuredness below. The five labels are states which need to be held
996 * between computer turns.
998 * The FSM is not externally reset to RANDOM_FIRE if the player wins. Instead,
999 * the other states check for "impossible" conditions which signify a new
1000 * game, then if found transition to RANDOM_FIRE.
1005 #define POSSIBLE(x, y) (ONBOARD(x, y) && !hits[COMPUTER][x][y])
1006 #define RANDOM_FIRE 0
1007 #define RANDOM_HIT 1
1008 #define HUNT_DIRECT 2
1009 #define FIRST_PASS 3
1010 #define REVERSE_JUMP 4
1011 #define SECOND_PASS 5
1012 static int next = RANDOM_FIRE;
1013 static bool used[5];
1015 int navail, x, y, d, n;
1019 case RANDOM_FIRE: /* last shot was random and missed */
1022 if (!(hit = cpufire(x, y)))
1028 next = (hit == S_SUNK) ? RANDOM_FIRE : RANDOM_HIT;
1032 case RANDOM_HIT: /* last shot was random and hit */
1036 used[dir_N / 2] = FALSE;
1039 case HUNT_DIRECT: /* last shot hit, we're looking for ship's long axis */
1040 for (d = navail = 0; d < (dir_MAX) / 2; d++) {
1041 x = ts.x + xincr[d * 2];
1042 y = ts.y + yincr[d * 2];
1043 if (!used[d] && POSSIBLE(x, y))
1048 if (navail == 0) /* no valid places for shots adjacent... */
1049 goto refire; /* ...so we must random-fire */
1051 n = rnd(navail) + 1;
1052 for (d = 0; d < (dir_MAX) / 2 && used[d]; d++) ;
1053 /* used[d] is first that == 0 */
1055 while (d < (dir_MAX) / 2 && used[++d]) ;
1056 /* used[d] is next that == 0 */
1058 assert(d < (dir_MAX) / 2);
1059 assert(used[d] == FALSE);
1062 x = ts.x + xincr[d * 2];
1063 y = ts.y + yincr[d * 2];
1065 assert(POSSIBLE(x, y));
1067 if (!(hit = cpufire(x, y)))
1074 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1079 case FIRST_PASS: /* we have a start and a direction now */
1080 x = ts.x + xincr[ts.dir];
1081 y = ts.y + yincr[ts.dir];
1082 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1086 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1088 next = REVERSE_JUMP;
1091 case REVERSE_JUMP: /* nail down the ship's other end */
1092 d = (ts.dir + (dir_MAX) / 2) % dir_MAX;
1093 x = ts.x + ts.hits * xincr[d];
1094 y = ts.y + ts.hits * yincr[d];
1095 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1100 next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1105 case SECOND_PASS: /* continue shooting after reversing */
1106 x = ts.x + xincr[ts.dir];
1107 y = ts.y + yincr[ts.dir];
1108 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1112 next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1119 /* pause between shots in salvo */
1125 MvPrintw(PROMPTLINE + 2, 0,
1126 "New state %d, x=%d, y=%d, d=%d",
1129 return ((hit) ? TRUE : FALSE);
1138 for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
1139 for (j = 0; j < ss->length; j++) {
1140 cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir]);
1148 j = 18 + (int) strlen(your_name);
1153 MvPrintw(1, (COLWIDTH - j) / 2,
1154 "%s: %d Computer: %d", your_name, plywon, cpuwon);
1156 prompt(2, (awinna())? "Want to be humiliated again, %s [yn]? "
1157 : "Going to give me a chance for revenge, %s [yn]? ", your_name);
1158 return (sgetc("YN") == 'Y');
1162 do_options(int c, char *op[])
1167 for (i = 1; i < c; i++) {
1171 (void) fprintf(stderr, "Usage: bs [-s | -b] [-c]\n");
1172 (void) fprintf(stderr, "\tWhere the options are:\n");
1173 (void) fprintf(stderr, "\t-s : play a salvo game\n");
1174 (void) fprintf(stderr, "\t-b : play a blitz game\n");
1175 (void) fprintf(stderr, "\t-c : ships may be adjacent\n");
1176 ExitProgram(EXIT_FAILURE);
1183 (void) fprintf(stderr,
1184 "Bad Arg: -b and -s are mutually exclusive\n");
1185 ExitProgram(EXIT_FAILURE);
1191 (void) fprintf(stderr,
1192 "Bad Arg: -s and -b are mutually exclusive\n");
1193 ExitProgram(EXIT_FAILURE);
1200 (void) fprintf(stderr,
1201 "Bad arg: type \"%s ?\" for usage message\n",
1203 ExitProgram(EXIT_FAILURE);
1213 register int i, shots;
1214 register ship_t *sp;
1217 sp = cpuship; /* count cpu shots */
1219 sp = plyship; /* count player shots */
1221 for (i = 0, shots = 0; i < SHIPTYPES; i++, sp++) {
1222 if (sp->hits >= sp->length)
1223 continue; /* dead ship */
1231 main(int argc, char *argv[])
1233 setlocale(LC_ALL, "");
1235 do_options(argc, argv);
1240 while (awinna() == -1) {
1253 if (cputurn() && awinna() != -1)
1256 if (plyturn() && awinna() != -1)
1262 while ((turn ? cputurn() : plyturn()) && awinna() == -1)
1272 /* bs.c ends here */