1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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.67 2017/05/14 00:27:05 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 /* coordinate handling */
73 #define SHOWSPLASH ' '
74 #define IS_SHIP(c) (isupper(UChar(c)) ? TRUE : FALSE)
76 /* how to position us on player board */
79 #define PY(y) (PYBASE + (y))
80 #define PX(x) (PXBASE + (x)*3)
81 #define pgoto(y, x) (void)move(PY(y), PX(x))
83 /* how to position us on cpu board */
86 #define CY(y) (CYBASE + (y))
87 #define CX(x) (CXBASE + (x)*3)
88 #define CYINV(y) ((y) - CYBASE)
89 #define CXINV(x) (((x) - CXBASE) / 3)
90 #define cgoto(y, x) (void)move(CY(y), CX(x))
92 #define ONBOARD(x, y) (x >= 0 && x < BWIDTH && y >= 0 && y < BDEPTH)
94 /* other board locations */
96 #define PROMPTLINE 21 /* prompt line */
97 #define SYBASE CYBASE + BDEPTH + 3 /* move key diagram */
99 #define MYBASE SYBASE - 1 /* diagram caption */
101 #define HYBASE SYBASE - 1 /* help area */
104 /* this will need to be changed if BWIDTH changes */
105 static char numbers[] = " 0 1 2 3 4 5 6 7 8 9";
107 static char carrier[] = "Aircraft Carrier";
108 static char battle[] = "Battleship";
109 static char sub[] = "Submarine";
110 static char destroy[] = "Destroyer";
111 static char ptboat[] = "PT Boat";
113 static char *your_name;
114 static char dftname[] = "stranger";
116 /* direction constants */
125 static int xincr[8] =
126 {1, 1, 0, -1, -1, -1, 0, 1};
127 static int yincr[8] =
128 {0, 1, 1, 1, 0, -1, -1, -1};
130 /* current ship position and direction */
131 static int curx = (BWIDTH / 2);
132 static int cury = (BDEPTH / 2);
135 char *name; /* name of the ship type */
136 int hits; /* how many times has this ship been hit? */
137 char symbol; /* symbol for game purposes */
138 int length; /* length of ship */
139 int x, y; /* coordinates of ship start point */
140 int dir; /* direction of `bow' */
141 bool placed; /* has it been placed on the board? */
144 static bool checkplace(int b, ship_t * ss, int vis);
146 #define SHIPIT(name, symbol, length) { name, 0, symbol, length, 0,0, 0, FALSE }
148 static ship_t plyship[SHIPTYPES] =
150 SHIPIT(carrier, 'A', 5),
151 SHIPIT(battle, 'B', 4),
152 SHIPIT(destroy, 'D', 3),
154 SHIPIT(ptboat, 'P', 2),
157 static ship_t cpuship[SHIPTYPES] =
159 SHIPIT(carrier, 'A', 5),
160 SHIPIT(battle, 'B', 4),
161 SHIPIT(destroy, 'D', 3),
163 SHIPIT(ptboat, 'P', 2),
166 /* "Hits" board, and main board. */
167 static char hits[2][BWIDTH][BDEPTH];
168 static char board[2][BWIDTH][BDEPTH];
170 static int turn; /* 0=player, 1=computer */
171 static int plywon = 0, cpuwon = 0; /* How many games has each won? */
173 static int salvo, blitz, closepack;
175 #define PR (void)addstr
177 static void uninitgame(int sig) GCC_NORETURN;
180 uninitgame(int sig GCC_UNUSED)
181 /* end the game, either normally or due to signal */
185 (void) reset_shell_mode();
189 ExitProgram(sig ? EXIT_FAILURE : EXIT_SUCCESS);
194 /* announce which game options are enabled */
196 if (salvo || blitz || closepack) {
197 (void) printw("Playing optional game (");
199 (void) printw("salvo, ");
201 (void) printw("nosalvo, ");
203 (void) printw("blitz ");
205 (void) printw("noblitz, ");
207 (void) printw("closepack)");
209 (void) printw("noclosepack)");
212 "Playing standard game (noblitz, nosalvo, noclosepack)");
220 srand((unsigned) (time(0L) + getpid())); /* Kick the random number generator */
222 CATCHALL(uninitgame);
224 if ((tmpname = getlogin()) != 0 &&
225 (your_name = strdup(tmpname)) != 0) {
226 your_name[0] = (char) toupper(UChar(your_name[0]));
228 your_name = strdup(dftname);
232 keypad(stdscr, TRUE);
233 (void) def_prog_mode();
240 MvAddStr(4, 29, "Welcome to Battleship!");
244 PR(" \\ \\ \\ \\ \\_____________\n");
245 PR(" \\ \\ \\_____________ \\ \\/ |\n");
246 PR(" \\ \\/ \\ \\/ |\n");
247 PR(" \\/ \\_____/ |__\n");
248 PR(" ________________/ |\n");
249 PR(" \\ S.S. Penguin |\n");
251 PR(" \\___________________________________________________/\n");
253 MvAddStr(22, 27, "Hit any key to continue...");
261 init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
262 init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
263 init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
264 init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
265 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
266 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
267 init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
268 init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
271 #ifdef NCURSES_MOUSE_VERSION
272 (void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
273 #endif /* NCURSES_MOUSE_VERSION */
278 prompt(int n, NCURSES_CONST char *f, const char *s)
279 /* print a message at the prompt line */
281 (void) move(PROMPTLINE + n, 0);
288 error(NCURSES_CONST char *s)
290 (void) move(PROMPTLINE + 2, 0);
299 placeship(int b, ship_t * ss, int vis)
303 for (l = 0; l < ss->length; ++l) {
304 int newx = ss->x + l * xincr[ss->dir];
305 int newy = ss->y + l * yincr[ss->dir];
307 board[b][newx][newy] = ss->symbol;
319 return (((rand() & 0x7FFF) % n));
323 randomplace(int b, ship_t * ss)
324 /* generate a valid random ship placement into px,py */
328 ss->dir = rnd(2) ? E : S;
329 ss->x = rnd(BWIDTH - (ss->dir == E ? ss->length : 0));
330 ss->y = rnd(BDEPTH - (ss->dir == S ? ss->length : 0));
332 (!checkplace(b, ss, FALSE));
342 MvAddStr(0, 35, "BATTLESHIPS");
343 (void) move(PROMPTLINE + 2, 0);
346 memset(board, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
347 memset(hits, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
348 for (i = 0; i < SHIPTYPES; i++) {
366 /* draw empty boards */
367 MvAddStr(PYBASE - 2, PXBASE + 5, "Main Board");
368 MvAddStr(PYBASE - 1, PXBASE - 3, numbers);
369 for (i = 0; i < BDEPTH; ++i) {
370 MvAddCh(PYBASE + i, PXBASE - 3, (chtype) (i + 'A'));
373 attron(COLOR_PAIR(COLOR_BLUE));
376 for (j = 0; j < BWIDTH; j++)
377 (void) addstr(" . ");
384 MvAddStr(PYBASE + BDEPTH, PXBASE - 3, numbers);
385 MvAddStr(CYBASE - 2, CXBASE + 7, "Hit/Miss Board");
386 MvAddStr(CYBASE - 1, CXBASE - 3, numbers);
387 for (i = 0; i < BDEPTH; ++i) {
388 MvAddCh(CYBASE + i, CXBASE - 3, (chtype) (i + 'A'));
391 attron(COLOR_PAIR(COLOR_BLUE));
394 for (j = 0; j < BWIDTH; j++)
395 (void) addstr(" . ");
403 MvAddStr(CYBASE + BDEPTH, CXBASE - 3, numbers);
405 MvPrintw(HYBASE, HXBASE,
406 "To position your ships: move the cursor to a spot, then");
407 MvPrintw(HYBASE + 1, HXBASE,
408 "type the first letter of a ship type to select it, then");
409 MvPrintw(HYBASE + 2, HXBASE,
410 "type a direction ([hjkl] or [4862]), indicating how the");
411 MvPrintw(HYBASE + 3, HXBASE,
412 "ship should be pointed. You may also type a ship letter");
413 MvPrintw(HYBASE + 4, HXBASE,
414 "followed by `r' to position it randomly, or type `R' to");
415 MvPrintw(HYBASE + 5, HXBASE,
416 "place all remaining ships randomly.");
418 MvAddStr(MYBASE, MXBASE, "Aiming keys:");
419 MvAddStr(SYBASE, SXBASE, "y k u 7 8 9");
420 MvAddStr(SYBASE + 1, SXBASE, " \\|/ \\|/ ");
421 MvAddStr(SYBASE + 2, SXBASE, "h-+-l 4-+-6");
422 MvAddStr(SYBASE + 3, SXBASE, " /|\\ /|\\ ");
423 MvAddStr(SYBASE + 4, SXBASE, "b j n 1 2 3");
425 /* have the computer place ships */
426 for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++) {
427 randomplace(COMPUTER, ss);
428 placeship(COMPUTER, ss, FALSE);
432 char c, docked[SHIPTYPES + 2], *cp = docked;
434 ss = (ship_t *) NULL;
436 /* figure which ships still wait to be placed */
438 for (i = 0; i < SHIPTYPES; i++)
439 if (!plyship[i].placed)
440 *cp++ = plyship[i].symbol;
443 /* get a command letter */
444 prompt(1, "Type one of [%s] to pick a ship.", docked + 1);
446 c = (char) getcoord(PLAYER);
448 (!(strchr) (docked, c));
453 /* map that into the corresponding symbol */
454 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
458 prompt(1, "Type one of [hjklrR] to place your %s.", ss->name);
465 (!(strchr("hjkl8462rR", c) || c == FF));
468 (void) clearok(stdscr, TRUE);
470 } else if (ss == 0) {
471 beep(); /* simple to verify, unlikely to happen */
472 } else if (c == 'r') {
473 prompt(1, "Random-placing your %s", ss->name);
474 randomplace(PLAYER, ss);
475 placeship(PLAYER, ss, TRUE);
476 error((char *) NULL);
478 } else if (c == 'R') {
479 prompt(1, "Placing the rest of your fleet at random...", "");
480 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
482 randomplace(PLAYER, ss);
483 placeship(PLAYER, ss, TRUE);
486 error((char *) NULL);
487 } else if (strchr("hjkl8462", c)) {
510 if (checkplace(PLAYER, ss, TRUE)) {
511 placeship(PLAYER, ss, TRUE);
512 error((char *) NULL);
517 for (unplaced = i = 0; i < SHIPTYPES; i++)
518 unplaced += !plyship[i].placed;
524 MvPrintw(HYBASE, HXBASE,
525 "To fire, move the cursor to your chosen aiming point ");
526 MvPrintw(HYBASE + 1, HXBASE,
527 "and strike any key other than a motion key. ");
528 MvPrintw(HYBASE + 2, HXBASE,
530 MvPrintw(HYBASE + 3, HXBASE,
532 MvPrintw(HYBASE + 4, HXBASE,
534 MvPrintw(HYBASE + 5, HXBASE,
537 (void) prompt(0, "Press any key to start...", "");
553 MvPrintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
557 MvPrintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
562 switch (c = getch()) {
566 ny = cury + BDEPTH - 1;
579 nx = curx + BWIDTH - 1;
590 ny = cury + BDEPTH - 1;
591 nx = curx + BWIDTH - 1;
597 nx = curx + BWIDTH - 1;
602 ny = cury + BDEPTH - 1;
614 (void) clearok(stdscr, TRUE);
617 #ifdef NCURSES_MOUSE_VERSION
624 && myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
625 && myevent.x >= CX(0) && myevent.x <= CX(BDEPTH)) {
626 curx = CXINV(myevent.x);
627 cury = CYINV(myevent.y);
634 /* no fall through */
635 #endif /* NCURSES_MOUSE_VERSION */
639 MvAddStr(CYBASE + BDEPTH + 1, CXBASE + 11, " ");
641 MvAddStr(PYBASE + BDEPTH + 1, PXBASE + 11, " ");
651 collidecheck(int b, int y, int x)
652 /* is this location on the selected zboard adjacent to a ship? */
656 /* anything on the square */
657 if ((collide = IS_SHIP(board[b][x][y])) != FALSE)
660 /* anything on the neighbors */
664 for (i = 0; i < 8; i++) {
669 if (ONBOARD(xend, yend)
670 && IS_SHIP(board[b][xend][yend])) {
680 checkplace(int b, ship_t * ss, int vis)
684 /* first, check for board edges */
685 xend = ss->x + (ss->length - 1) * xincr[ss->dir];
686 yend = ss->y + (ss->length - 1) * yincr[ss->dir];
687 if (!ONBOARD(xend, yend)) {
691 error("Ship is hanging from the edge of the world");
694 error("Try fitting it on the board");
697 error("Figure I won't find it if you put it there?");
703 for (l = 0; l < ss->length; ++l) {
704 if (collidecheck(b, ss->y + l * yincr[ss->dir], ss->x + l * xincr[ss->dir])) {
708 error("There's already a ship there");
711 error("Collision alert! Aaaaaagh!");
714 error("Er, Admiral, what about the other ship?");
729 for (i = 0; i < 2; ++i) {
730 ss = (i) ? cpuship : plyship;
731 for (j = 0; j < SHIPTYPES; ++j, ++ss)
732 if (ss->length > ss->hits)
741 hitship(int x, int y)
742 /* register a hit on the targeted ship */
748 getyx(stdscr, oldy, oldx);
749 sb = (turn) ? plyship : cpuship;
750 if ((sym = board[OTHER][x][y]) == 0)
751 return ((ship_t *) NULL);
752 for (ss = sb; ss < sb + SHIPTYPES; ++ss)
753 if (ss->symbol == sym) {
754 if (++ss->hits < ss->length) /* still afloat? */
755 return ((ship_t *) NULL);
760 for (j = -1; j <= 1; j++) {
761 int bx = ss->x + j * xincr[(ss->dir + 2) % 8];
762 int by = ss->y + j * yincr[(ss->dir + 2) % 8];
764 for (i = -1; i <= ss->length; ++i) {
767 x1 = bx + i * xincr[ss->dir];
768 y1 = by + i * yincr[ss->dir];
769 if (ONBOARD(x1, y1)) {
770 hits[turn][x1][y1] = MARK_MISS;
771 if (turn % 2 == PLAYER) {
775 attron(COLOR_PAIR(COLOR_GREEN));
789 for (i = 0; i < ss->length; ++i) {
790 int x1 = ss->x + i * xincr[ss->dir];
791 int y1 = ss->y + i * yincr[ss->dir];
793 hits[turn][x1][y1] = ss->symbol;
794 if (turn % 2 == PLAYER) {
801 attron(COLOR_PAIR(COLOR_RED));
810 (void) move(oldy, oldx);
814 (void) move(oldy, oldx);
815 return ((ship_t *) NULL);
823 NCURSES_CONST char *m = NULL;
825 prompt(1, "Where do you want to shoot? ", "");
827 (void) getcoord(COMPUTER);
828 if (hits[PLAYER][curx][cury]) {
829 prompt(1, "You shelled this spot already! Try again.", "");
834 hit = IS_SHIP(board[COMPUTER][curx][cury]);
835 hits[PLAYER][curx][cury] = (char) (hit ? MARK_HIT : MARK_MISS);
840 attron(COLOR_PAIR(COLOR_RED));
842 attron(COLOR_PAIR(COLOR_GREEN));
845 AddCh(hits[PLAYER][curx][cury]);
850 prompt(1, "You %s.", hit ? "scored a hit" : "missed");
851 if (hit && (ss = hitship(curx, cury))) {
854 m = " You sank my %s!";
857 m = " I have this sinking feeling about my %s....";
860 m = " My %s has gone to Davy Jones's locker!";
863 m = " Glub, glub -- my %s is headed for the bottom!";
866 m = " You'll pick up survivors from my %s, I hope...!";
870 (void) printw(m, ss->name);
890 for (s1 = s; *s1 && ch != *s1; ++s1)
901 randomfire(int *px, int *py)
902 /* random-fire routine -- implements simple diagonal-striping strategy */
904 static int turncount = 0;
905 static int srchstep = BEGINSTEP;
906 static int huntoffs; /* Offset on search strategy */
907 int ypossible[BWIDTH * BDEPTH], xpossible[BWIDTH * BDEPTH], nposs;
908 int ypreferred[BWIDTH * BDEPTH], xpreferred[BWIDTH * BDEPTH], npref;
911 if (turncount++ == 0)
912 huntoffs = rnd(srchstep);
914 /* first, list all possible moves */
916 for (x = 0; x < BWIDTH; x++)
917 for (y = 0; y < BDEPTH; y++)
918 if (!hits[COMPUTER][x][y]) {
919 xpossible[nposs] = x;
920 ypossible[nposs] = y;
922 if (((x + huntoffs) % srchstep) != (y % srchstep)) {
923 xpreferred[npref] = x;
924 ypreferred[npref] = y;
943 error("No moves possible?? Help!");
944 ExitProgram(EXIT_FAILURE);
954 cpufire(int x, int y)
955 /* fire away at given location */
960 hit = (bool) board[PLAYER][x][y];
961 hits[COMPUTER][x][y] = (hit ? MARK_HIT : MARK_MISS);
962 MvPrintw(PROMPTLINE, 0,
963 "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" :
965 if ((sunk = (hit && (ss = hitship(x, y)))) != 0)
966 (void) printw(" I've sunk your %s", ss->name);
973 attron(COLOR_PAIR(COLOR_RED));
975 attron(COLOR_PAIR(COLOR_GREEN));
978 AddCh((hit ? SHOWHIT : SHOWSPLASH));
983 return hit ? (sunk ? S_SUNK : S_HIT) : S_MISS;
987 * This code implements a fairly irregular FSM, so please forgive the rampant
988 * unstructuredness below. The five labels are states which need to be held
989 * between computer turns.
991 * The FSM is not externally reset to RANDOM_FIRE if the player wins. Instead,
992 * the other states check for "impossible" conditions which signify a new
993 * game, then if found transition to RANDOM_FIRE.
998 #define POSSIBLE(x, y) (ONBOARD(x, y) && !hits[COMPUTER][x][y])
999 #define RANDOM_FIRE 0
1000 #define RANDOM_HIT 1
1001 #define HUNT_DIRECT 2
1002 #define FIRST_PASS 3
1003 #define REVERSE_JUMP 4
1004 #define SECOND_PASS 5
1005 static int next = RANDOM_FIRE;
1006 static bool used[5];
1008 int navail, x, y, d, n;
1012 case RANDOM_FIRE: /* last shot was random and missed */
1015 if (!(hit = cpufire(x, y)))
1021 next = (hit == S_SUNK) ? RANDOM_FIRE : RANDOM_HIT;
1025 case RANDOM_HIT: /* last shot was random and hit */
1026 used[E / 2] = used[S / 2] = used[W / 2] = used[N / 2] = FALSE;
1029 case HUNT_DIRECT: /* last shot hit, we're looking for ship's long axis */
1030 for (d = navail = 0; d < 4; d++) {
1031 x = ts.x + xincr[d * 2];
1032 y = ts.y + yincr[d * 2];
1033 if (!used[d] && POSSIBLE(x, y))
1038 if (navail == 0) /* no valid places for shots adjacent... */
1039 goto refire; /* ...so we must random-fire */
1041 n = rnd(navail) + 1;
1042 for (d = 0; d < 4 && used[d]; d++) ;
1043 /* used[d] is first that == 0 */
1045 while (d < 4 && used[++d]) ;
1046 /* used[d] is next that == 0 */
1049 assert(used[d] == FALSE);
1052 x = ts.x + xincr[d * 2];
1053 y = ts.y + yincr[d * 2];
1055 assert(POSSIBLE(x, y));
1057 if (!(hit = cpufire(x, y)))
1064 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1069 case FIRST_PASS: /* we have a start and a direction now */
1070 x = ts.x + xincr[ts.dir];
1071 y = ts.y + yincr[ts.dir];
1072 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1076 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1078 next = REVERSE_JUMP;
1081 case REVERSE_JUMP: /* nail down the ship's other end */
1082 d = (ts.dir + 4) % 8;
1083 x = ts.x + ts.hits * xincr[d];
1084 y = ts.y + ts.hits * yincr[d];
1085 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1090 next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1095 case SECOND_PASS: /* continue shooting after reversing */
1096 x = ts.x + xincr[ts.dir];
1097 y = ts.y + yincr[ts.dir];
1098 if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
1102 next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1109 /* pause between shots in salvo */
1115 MvPrintw(PROMPTLINE + 2, 0,
1116 "New state %d, x=%d, y=%d, d=%d",
1119 return ((hit) ? TRUE : FALSE);
1128 for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
1129 for (j = 0; j < ss->length; j++) {
1130 cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir]);
1138 j = 18 + (int) strlen(your_name);
1143 MvPrintw(1, (COLWIDTH - j) / 2,
1144 "%s: %d Computer: %d", your_name, plywon, cpuwon);
1146 prompt(2, (awinna())? "Want to be humiliated again, %s [yn]? "
1147 : "Going to give me a chance for revenge, %s [yn]? ", your_name);
1148 return (sgetc("YN") == 'Y');
1152 do_options(int c, char *op[])
1157 for (i = 1; i < c; i++) {
1161 (void) fprintf(stderr, "Usage: battle [-s | -b] [-c]\n");
1162 (void) fprintf(stderr, "\tWhere the options are:\n");
1163 (void) fprintf(stderr, "\t-s : play a salvo game\n");
1164 (void) fprintf(stderr, "\t-b : play a blitz game\n");
1165 (void) fprintf(stderr, "\t-c : ships may be adjacent\n");
1166 ExitProgram(EXIT_FAILURE);
1173 (void) fprintf(stderr,
1174 "Bad Arg: -b and -s are mutually exclusive\n");
1175 ExitProgram(EXIT_FAILURE);
1181 (void) fprintf(stderr,
1182 "Bad Arg: -s and -b are mutually exclusive\n");
1183 ExitProgram(EXIT_FAILURE);
1190 (void) fprintf(stderr,
1191 "Bad arg: type \"%s ?\" for usage message\n",
1193 ExitProgram(EXIT_FAILURE);
1203 register int i, shots;
1204 register ship_t *sp;
1207 sp = cpuship; /* count cpu shots */
1209 sp = plyship; /* count player shots */
1211 for (i = 0, shots = 0; i < SHIPTYPES; i++, sp++) {
1212 if (sp->hits >= sp->length)
1213 continue; /* dead ship */
1221 main(int argc, char *argv[])
1223 setlocale(LC_ALL, "");
1225 do_options(argc, argv);
1230 while (awinna() == -1) {
1243 if (cputurn() && awinna() != -1)
1246 if (plyturn() && awinna() != -1)
1252 while ((turn ? cputurn() : plyturn()) && awinna() == -1)
1262 /* bs.c ends here */