1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * bs.c - original author: Bruce Holloway
31 * salvo option by: Chuck A DeGaul
32 * with improved user interface, autoconfiguration and code cleanup
33 * by Eric S. Raymond <esr@snark.thyrsus.com>
34 * v1.2 with color support and minor portability fixes, November 1990
35 * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
36 * v2.1 with ncurses mouse support, September 1995
38 * $Id: bs.c,v 1.77 2021/06/17 21:11:08 tom Exp $
41 #include <test.priv.h>
46 #define SIGIOT SIGABRT
49 static int getcoord(int);
52 * Constants for tuning the random-fire algorithm. It prefers moves that
53 * diagonal-stripe the board with a stripe separation of srchstep. If
54 * no such preferred moves are found, srchstep is decremented.
56 #define BEGINSTEP 3 /* initial value of srchstep */
58 /* miscellaneous constants */
60 #define OTHER (1-turn)
65 #define CTRLC '\003' /* used as terminate command */
66 #define FF '\014' /* used as redraw command */
68 #define is_QUIT(c) ((c) == CTRLC || (c) == QUIT)
70 /* coordinate handling */
76 #define SHOWSPLASH ' '
77 #define IS_SHIP(c) (isupper(UChar(c)) ? TRUE : FALSE)
79 /* how to position us on player board */
82 #define PY(y) (PYBASE + (y))
83 #define PX(x) (PXBASE + (x)*3)
84 #define pgoto(y, x) (void)move(PY(y), PX(x))
86 /* how to position us on cpu board */
89 #define CY(y) (CYBASE + (y))
90 #define CX(x) (CXBASE + (x)*3)
91 #define CYINV(y) ((y) - CYBASE)
92 #define CXINV(x) (((x) - CXBASE) / 3)
93 #define cgoto(y, x) (void)move(CY(y), CX(x))
95 #define ONBOARD(x, y) (x >= 0 && x < BWIDTH && y >= 0 && y < BDEPTH)
97 /* other board locations */
99 #define PROMPTLINE 21 /* prompt line */
100 #define SYBASE CYBASE + BDEPTH + 3 /* move key diagram */
102 #define MYBASE SYBASE - 1 /* diagram caption */
104 #define HYBASE SYBASE - 1 /* help area */
107 /* this will need to be changed if BWIDTH changes */
108 static char numbers[] = " 0 1 2 3 4 5 6 7 8 9";
110 static char carrier[] = "Aircraft Carrier";
111 static char battle[] = "Battleship";
112 static char sub[] = "Submarine";
113 static char destroy[] = "Destroyer";
114 static char ptboat[] = "PT Boat";
116 static char *your_name;
117 static char dftname[] = "stranger";
119 /* direction constants */
131 static int xincr[dir_MAX + 2] =
132 {1, 1, 0, -1, -1, -1, 0, 1};
133 static int yincr[dir_MAX + 2] =
134 {0, 1, 1, 1, 0, -1, -1, -1};
136 /* current ship position and direction */
137 static int curx = (BWIDTH / 2);
138 static int cury = (BDEPTH / 2);
141 char *name; /* name of the ship type */
142 int hits; /* how many times has this ship been hit? */
143 char symbol; /* symbol for game purposes */
144 int length; /* length of ship */
145 int x, y; /* coordinates of ship start point */
146 int dir; /* direction of `bow' */
147 bool placed; /* has it been placed on the board? */
150 static bool checkplace(int b, ship_t * ss, int vis);
152 #define SHIPIT(name, symbol, length) { name, 0, symbol, length, 0,0, 0, FALSE }
154 /* "ply=player", "cpu=computer" */
155 static ship_t plyship[SHIPTYPES] =
157 SHIPIT(carrier, 'A', 5),
158 SHIPIT(battle, 'B', 4),
159 SHIPIT(destroy, 'D', 3),
161 SHIPIT(ptboat, 'P', 2),
164 static ship_t cpuship[SHIPTYPES] =
166 SHIPIT(carrier, 'A', 5),
167 SHIPIT(battle, 'B', 4),
168 SHIPIT(destroy, 'D', 3),
170 SHIPIT(ptboat, 'P', 2),
173 /* "Hits" board, and main board. */
174 static char hits[2][BWIDTH][BDEPTH];
175 static char board[2][BWIDTH][BDEPTH];
177 static int turn; /* 0=player, 1=computer */
178 static int plywon = 0, cpuwon = 0; /* How many games has each won? */
180 static int salvo, blitz, closepack;
182 #define PR (void)addstr
184 static GCC_NORETURN void uninitgame(int sig);
187 uninitgame(int sig GCC_UNUSED)
188 /* end the game, either normally or due to signal */
192 (void) reset_shell_mode();
196 ExitProgram(sig ? EXIT_FAILURE : EXIT_SUCCESS);
201 /* announce which game options are enabled */
203 if (salvo || blitz || closepack) {
204 (void) printw("Playing optional game (");
206 (void) printw("salvo, ");
208 (void) printw("nosalvo, ");
210 (void) printw("blitz ");
212 (void) printw("noblitz, ");
214 (void) printw("closepack)");
216 (void) printw("noclosepack)");
219 "Playing standard game (noblitz, nosalvo, noclosepack)");
227 srand((unsigned) (time(0L) + getpid())); /* Kick the random number generator */
229 InitAndCatch(initscr(), uninitgame);
231 if ((tmpname = getlogin()) != 0 &&
232 (your_name = strdup(tmpname)) != 0) {
233 your_name[0] = (char) toupper(UChar(your_name[0]));
235 your_name = strdup(dftname);
238 keypad(stdscr, TRUE);
239 (void) def_prog_mode();
246 MvAddStr(4, 29, "Welcome to Battleship!");
250 PR(" \\ \\ \\ \\ \\_____________\n");
251 PR(" \\ \\ \\_____________ \\ \\/ |\n");
252 PR(" \\ \\/ \\ \\/ |\n");
253 PR(" \\/ \\_____/ |__\n");
254 PR(" ________________/ |\n");
255 PR(" \\ S.S. Penguin |\n");
257 PR(" \\___________________________________________________/\n");
259 MvAddStr(22, 27, "Hit any key to continue...");
267 init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
268 init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
269 init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
270 init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
271 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
272 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
273 init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
274 init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
277 #ifdef NCURSES_MOUSE_VERSION
278 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
279 #endif /* NCURSES_MOUSE_VERSION */
284 prompt(int n, NCURSES_CONST char *f, const char *s)
285 /* print a message at the prompt line */
287 (void) move(PROMPTLINE + n, 0);
294 error(NCURSES_CONST char *s)
296 (void) move(PROMPTLINE + 2, 0);
305 placeship(int b, ship_t * ss, int vis)
309 for (l = 0; l < ss->length; ++l) {
310 int newx = ss->x + l * xincr[ss->dir];
311 int newy = ss->y + l * yincr[ss->dir];
313 board[b][newx][newy] = ss->symbol;
325 return (((rand() & 0x7FFF) % n));
329 randomplace(int b, ship_t * ss)
330 /* generate a valid random ship placement into px,py */
334 ss->dir = rnd(2) ? dir_E : dir_S;
335 ss->x = rnd(BWIDTH - (ss->dir == dir_E ? ss->length : 0));
336 ss->y = rnd(BDEPTH - (ss->dir == dir_S ? ss->length : 0));
338 (!checkplace(b, ss, FALSE));
348 MvAddStr(0, 35, "BATTLESHIPS");
349 (void) move(PROMPTLINE + 2, 0);
352 memset(board, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
353 memset(hits, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
354 for (i = 0; i < SHIPTYPES; i++) {
372 /* draw empty boards */
373 MvAddStr(PYBASE - 2, PXBASE + 5, "Main Board");
374 MvAddStr(PYBASE - 1, PXBASE - 3, numbers);
375 for (i = 0; i < BDEPTH; ++i) {
376 MvAddCh(PYBASE + i, PXBASE - 3, (chtype) (i + 'A'));
379 attron(COLOR_PAIR(COLOR_BLUE));
382 for (j = 0; j < BWIDTH; j++)
383 (void) addstr(" . ");
390 MvAddStr(PYBASE + BDEPTH, PXBASE - 3, numbers);
391 MvAddStr(CYBASE - 2, CXBASE + 7, "Hit/Miss Board");
392 MvAddStr(CYBASE - 1, CXBASE - 3, numbers);
393 for (i = 0; i < BDEPTH; ++i) {
394 MvAddCh(CYBASE + i, CXBASE - 3, (chtype) (i + 'A'));
397 attron(COLOR_PAIR(COLOR_BLUE));
400 for (j = 0; j < BWIDTH; j++)
401 (void) addstr(" . ");
409 MvAddStr(CYBASE + BDEPTH, CXBASE - 3, numbers);
411 MvPrintw(HYBASE, HXBASE,
412 "To position your ships: move the cursor to a spot, then");
413 MvPrintw(HYBASE + 1, HXBASE,
414 "type the first letter of a ship type to select it, then");
415 MvPrintw(HYBASE + 2, HXBASE,
416 "type a direction ([hjkl] or [4862]), indicating how the");
417 MvPrintw(HYBASE + 3, HXBASE,
418 "ship should be pointed. You may also type a ship letter");
419 MvPrintw(HYBASE + 4, HXBASE,
420 "followed by `r' to position it randomly, or type `R' to");
421 MvPrintw(HYBASE + 5, HXBASE,
422 "place all remaining ships randomly.");
424 MvAddStr(MYBASE, MXBASE, "Aiming keys:");
425 MvAddStr(SYBASE, SXBASE, "y k u 7 8 9");
426 MvAddStr(SYBASE + 1, SXBASE, " \\|/ \\|/ ");
427 MvAddStr(SYBASE + 2, SXBASE, "h-+-l 4-+-6");
428 MvAddStr(SYBASE + 3, SXBASE, " /|\\ /|\\ ");
429 MvAddStr(SYBASE + 4, SXBASE, "b j n 1 2 3");
431 /* have the computer place ships */
432 for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++) {
433 randomplace(COMPUTER, ss);
434 placeship(COMPUTER, ss, FALSE);
438 char c, docked[SHIPTYPES + 2], *cp = docked;
440 ss = (ship_t *) NULL;
442 /* figure which ships still wait to be placed */
444 for (i = 0; i < SHIPTYPES; i++)
445 if (!plyship[i].placed)
446 *cp++ = plyship[i].symbol;
449 /* get a command letter */
450 prompt(1, "Type one of [%s] to pick a ship.", docked + 1);
452 c = (char) getcoord(PLAYER);
454 (!(strchr) (docked, c));
459 /* map that into the corresponding symbol */
460 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
464 prompt(1, "Type one of [hjklrR] to place your %s.", ss->name);
471 (!(strchr("hjkl8462rR", c) || c == FF || is_QUIT(c)));
475 } else if (c == FF) {
476 (void) clearok(stdscr, TRUE);
478 } else if (ss == 0) {
479 beep(); /* simple to verify, unlikely to happen */
480 } else if (c == 'r') {
481 prompt(1, "Random-placing your %s", ss->name);
482 randomplace(PLAYER, ss);
483 placeship(PLAYER, ss, TRUE);
484 error((char *) NULL);
486 } else if (c == 'R') {
487 prompt(1, "Placing the rest of your fleet at random...", "");
488 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
490 randomplace(PLAYER, ss);
491 placeship(PLAYER, ss, TRUE);
494 error((char *) NULL);
495 } else if (strchr("hjkl8462", c)) {
518 if (checkplace(PLAYER, ss, TRUE)) {
519 placeship(PLAYER, ss, TRUE);
520 error((char *) NULL);
525 for (unplaced = i = 0; i < SHIPTYPES; i++)
526 unplaced += !plyship[i].placed;
532 MvPrintw(HYBASE, HXBASE,
533 "To fire, move the cursor to your chosen aiming point ");
534 MvPrintw(HYBASE + 1, HXBASE,
535 "and strike any key other than a motion key. ");
536 MvPrintw(HYBASE + 2, HXBASE,
538 MvPrintw(HYBASE + 3, HXBASE,
540 MvPrintw(HYBASE + 4, HXBASE,
542 MvPrintw(HYBASE + 5, HXBASE,
545 (void) prompt(0, "Press any key to start...", "");
562 MvPrintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
566 MvPrintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
571 switch (c = getch()) {
575 ny = cury + BDEPTH - 1;
588 nx = curx + BWIDTH - 1;
599 ny = cury + BDEPTH - 1;
600 nx = curx + BWIDTH - 1;
606 nx = curx + BWIDTH - 1;
611 ny = cury + BDEPTH - 1;
623 (void) clearok(stdscr, TRUE);
626 #ifdef NCURSES_MOUSE_VERSION
633 && myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
634 && myevent.x >= CX(0) && myevent.x <= CX(BDEPTH)) {
635 curx = CXINV(myevent.x);
636 cury = CYINV(myevent.y);
643 /* no fall through */
644 #endif /* NCURSES_MOUSE_VERSION */
648 MvAddStr(CYBASE + BDEPTH + 1, CXBASE + 11, " ");
650 MvAddStr(PYBASE + BDEPTH + 1, PXBASE + 11, " ");
660 collidecheck(int b, int y, int x)
661 /* is this location on the selected zboard adjacent to a ship? */
665 /* anything on the square */
666 if ((collide = IS_SHIP(board[b][x][y])) != FALSE)
669 /* anything on the neighbors */
673 for (i = 0; i < dir_MAX; i++) {
678 if (ONBOARD(xend, yend)
679 && IS_SHIP(board[b][xend][yend])) {
689 checkplace(int b, ship_t * ss, int vis)
693 /* first, check for board edges */
694 xend = ss->x + (ss->length - 1) * xincr[ss->dir];
695 yend = ss->y + (ss->length - 1) * yincr[ss->dir];
696 if (!ONBOARD(xend, yend)) {
700 error("Ship is hanging from the edge of the world");
703 error("Try fitting it on the board");
706 error("Figure I won't find it if you put it there?");
712 for (l = 0; l < ss->length; ++l) {
713 if (collidecheck(b, ss->y + l * yincr[ss->dir], ss->x + l * xincr[ss->dir])) {
717 error("There's already a ship there");
720 error("Collision alert! Aaaaaagh!");
723 error("Er, Admiral, what about the other ship?");
737 for (i = 0; i < 2; ++i) {
738 ship_t *ss = (i) ? cpuship : plyship;
739 for (j = 0; j < SHIPTYPES; ++j, ++ss)
740 if (ss->length > ss->hits)
749 hitship(int x, int y)
750 /* register a hit on the targeted ship */
756 getyx(stdscr, oldy, oldx);
757 sb = (turn) ? plyship : cpuship;
758 if ((sym = board[OTHER][x][y]) == 0)
759 return ((ship_t *) NULL);
760 for (ss = sb; ss < sb + SHIPTYPES; ++ss)
761 if (ss->symbol == sym) {
762 if (++ss->hits < ss->length) /* still afloat? */
763 return ((ship_t *) NULL);
770 for (j = -1; j <= 1; j++) {
771 int bx = ss->x + j * xincr[(ss->dir + 2) % dir_MAX];
772 int by = ss->y + j * yincr[(ss->dir + 2) % dir_MAX];
774 for (i = -1; i <= ss->length; ++i) {
777 x1 = bx + i * xincr[ss->dir];
778 y1 = by + i * yincr[ss->dir];
779 if (ONBOARD(x1, y1)) {
780 hits[turn][x1][y1] = MARK_MISS;
781 if (turn % 2 == PLAYER) {
785 attron(COLOR_PAIR(COLOR_GREEN));
800 for (i = 0; i < ss->length; ++i) {
801 int x1 = ss->x + i * xincr[ss->dir];
802 int y1 = ss->y + i * yincr[ss->dir];
804 hits[turn][x1][y1] = ss->symbol;
805 if (turn % 2 == PLAYER) {
812 attron(COLOR_PAIR(COLOR_RED));
821 (void) move(oldy, oldx);
825 (void) move(oldy, oldx);
826 return ((ship_t *) NULL);
834 NCURSES_CONST char *m = NULL;
836 prompt(1, "Where do you want to shoot? ", "");
838 (void) getcoord(COMPUTER);
839 if (hits[PLAYER][curx][cury]) {
840 prompt(1, "You shelled this spot already! Try again.", "");
845 hit = IS_SHIP(board[COMPUTER][curx][cury]);
846 hits[PLAYER][curx][cury] = (char) (hit ? MARK_HIT : MARK_MISS);
851 attron(COLOR_PAIR(COLOR_RED));
853 attron(COLOR_PAIR(COLOR_GREEN));
856 AddCh(hits[PLAYER][curx][cury]);
861 prompt(1, "You %s.", hit ? "scored a hit" : "missed");
862 if (hit && (ss = hitship(curx, cury))) {
865 m = " You sank my %s!";
868 m = " I have this sinking feeling about my %s....";
871 m = " My %s has gone to Davy Jones's locker!";
874 m = " Glub, glub -- my %s is headed for the bottom!";
877 m = " You'll pick up survivors from my %s, I hope...!";
881 (void) printw(m, ss->name);
901 for (s1 = s; *s1 && ch != *s1; ++s1)
912 randomfire(int *px, int *py)
913 /* random-fire routine -- implements simple diagonal-striping strategy */
915 static int turncount = 0;
916 static int srchstep = BEGINSTEP;
917 static int huntoffs; /* Offset on search strategy */
918 int ypossible[BWIDTH * BDEPTH], xpossible[BWIDTH * BDEPTH], nposs;
919 int ypreferred[BWIDTH * BDEPTH], xpreferred[BWIDTH * BDEPTH], npref;
922 if (turncount++ == 0)
923 huntoffs = rnd(srchstep);
925 /* first, list all possible moves */
927 for (x = 0; x < BWIDTH; x++)
928 for (y = 0; y < BDEPTH; y++)
929 if (!hits[COMPUTER][x][y]) {
930 xpossible[nposs] = x;
931 ypossible[nposs] = y;
933 if (((x + huntoffs) % srchstep) != (y % srchstep)) {
934 xpreferred[npref] = x;
935 ypreferred[npref] = y;
954 error("No moves possible?? Help!");
955 ExitProgram(EXIT_FAILURE);
965 cpufire(int x, int y)
966 /* fire away at given location */
971 hit = (bool) board[PLAYER][x][y];
972 hits[COMPUTER][x][y] = (hit ? MARK_HIT : MARK_MISS);
973 MvPrintw(PROMPTLINE, 0,
974 "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" :
976 if ((sunk = (hit && (ss = hitship(x, y)))) != 0)
977 (void) printw(" I've sunk your %s", ss->name);
984 attron(COLOR_PAIR(COLOR_RED));
986 attron(COLOR_PAIR(COLOR_GREEN));
989 AddCh((hit ? SHOWHIT : SHOWSPLASH));
994 return hit ? (sunk ? S_SUNK : S_HIT) : S_MISS;
998 * This code implements a fairly irregular FSM, so please forgive the rampant
999 * unstructuredness below. The five labels are states which need to be held
1000 * between computer turns.
1002 * The FSM is not externally reset to RANDOM_FIRE if the player wins. Instead,
1003 * the other states check for "impossible" conditions which signify a new
1004 * game, then if found transition to RANDOM_FIRE.
1009 #define POSSIBLE(x, y) (ONBOARD(x, y) && !hits[COMPUTER][x][y])
1010 #define RANDOM_FIRE 0
1011 #define RANDOM_HIT 1
1012 #define HUNT_DIRECT 2
1013 #define FIRST_PASS 3
1014 #define REVERSE_JUMP 4
1015 #define SECOND_PASS 5
1016 static int next = RANDOM_FIRE;
1017 static bool used[5];
1019 int navail, x, y, d, n;
1023 case RANDOM_FIRE: /* last shot was random and missed */
1026 if (!(hit = cpufire(x, y)))
1032 next = (hit == S_SUNK) ? RANDOM_FIRE : RANDOM_HIT;
1036 case RANDOM_HIT: /* last shot was random and hit */
1040 used[dir_N / 2] = FALSE;
1043 case HUNT_DIRECT: /* last shot hit, we're looking for ship's long axis */
1044 for (d = navail = 0; d < (dir_MAX) / 2; d++) {
1045 x = ts.x + xincr[d * 2];
1046 y = ts.y + yincr[d * 2];
1047 if (!used[d] && POSSIBLE(x, y))
1052 if (navail == 0) /* no valid places for shots adjacent... */
1053 goto refire; /* ...so we must random-fire */
1055 n = rnd(navail) + 1;
1056 for (d = 0; d < (dir_MAX) / 2 && used[d]; d++) ;
1057 /* used[d] is first that == 0 */
1059 while (d < (dir_MAX) / 2 && used[++d]) ;
1060 /* used[d] is next that == 0 */
1062 assert(d < (dir_MAX) / 2);
1063 assert(used[d] == FALSE);
1066 x = ts.x + xincr[d * 2];
1067 y = ts.y + yincr[d * 2];
1069 assert(POSSIBLE(x, y));
1071 if (!(hit = cpufire(x, y)))
1078 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1083 case FIRST_PASS: /* we have a start and a direction now */
1084 x = ts.x + xincr[ts.dir];
1085 y = ts.y + yincr[ts.dir];
1086 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1090 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1092 next = REVERSE_JUMP;
1095 case REVERSE_JUMP: /* nail down the ship's other end */
1096 d = (ts.dir + (dir_MAX) / 2) % dir_MAX;
1097 x = ts.x + ts.hits * xincr[d];
1098 y = ts.y + ts.hits * yincr[d];
1099 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1104 next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1109 case SECOND_PASS: /* continue shooting after reversing */
1110 x = ts.x + xincr[ts.dir];
1111 y = ts.y + yincr[ts.dir];
1112 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1116 next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1123 /* pause between shots in salvo */
1129 MvPrintw(PROMPTLINE + 2, 0,
1130 "New state %d, x=%d, y=%d, d=%d",
1133 return ((hit) ? TRUE : FALSE);
1142 for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
1143 for (j = 0; j < ss->length; j++) {
1144 cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir]);
1152 j = 18 + (int) strlen(your_name);
1157 MvPrintw(1, (COLWIDTH - j) / 2,
1158 "%s: %d Computer: %d", your_name, plywon, cpuwon);
1160 prompt(2, (awinna())? "Want to be humiliated again, %s [yn]? "
1161 : "Going to give me a chance for revenge, %s [yn]? ", your_name);
1162 return (sgetc("YN") == 'Y');
1166 do_options(int c, char *op[])
1171 for (i = 1; i < c; i++) {
1175 (void) fprintf(stderr, "Usage: bs [-s | -b] [-c]\n");
1176 (void) fprintf(stderr, "\tWhere the options are:\n");
1177 (void) fprintf(stderr, "\t-s : play a salvo game\n");
1178 (void) fprintf(stderr, "\t-b : play a blitz game\n");
1179 (void) fprintf(stderr, "\t-c : ships may be adjacent\n");
1180 ExitProgram(EXIT_FAILURE);
1187 (void) fprintf(stderr,
1188 "Bad Arg: -b and -s are mutually exclusive\n");
1189 ExitProgram(EXIT_FAILURE);
1195 (void) fprintf(stderr,
1196 "Bad Arg: -s and -b are mutually exclusive\n");
1197 ExitProgram(EXIT_FAILURE);
1204 (void) fprintf(stderr,
1205 "Bad arg: type \"%s ?\" for usage message\n",
1207 ExitProgram(EXIT_FAILURE);
1217 register int i, shots;
1218 register ship_t *sp;
1221 sp = cpuship; /* count cpu shots */
1223 sp = plyship; /* count player shots */
1225 for (i = 0, shots = 0; i < SHIPTYPES; i++, sp++) {
1226 if (sp->hits >= sp->length)
1227 continue; /* dead ship */
1235 main(int argc, char *argv[])
1237 setlocale(LC_ALL, "");
1239 do_options(argc, argv);
1244 while (awinna() == -1) {
1257 if (cputurn() && awinna() != -1)
1260 if (plyturn() && awinna() != -1)
1266 while ((turn ? cputurn() : plyturn()) && awinna() == -1)
1276 /* bs.c ends here */