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.
8 * v2.1 with ncurses mouse support, September 1995
10 * $Id: bs.c,v 1.24 1999/08/21 23:14:38 tom Exp $
13 #include <test.priv.h>
22 #define SIGIOT SIGABRT
25 #ifndef A_UNDERLINE /* BSD curses */
26 #define beep() write(1,"\007",1);
28 #define saveterm savetty
29 #define resetterm resetty
30 #define nocbreak nocrmode
32 #endif /* !A_UNDERLINE */
34 static int getcoord(int);
37 * Constants for tuning the random-fire algorithm. It prefers moves that
38 * diagonal-stripe the board with a stripe separation of srchstep. If
39 * no such preferred moves are found, srchstep is decremented.
41 #define BEGINSTEP 3 /* initial value of srchstep */
43 /* miscellaneous constants */
45 #define OTHER (1-turn)
50 #define CTRLC '\003' /* used as terminate command */
51 #define FF '\014' /* used as redraw command */
53 /* coordinate handling */
59 #define SHOWSPLASH ' '
60 #define IS_SHIP(c) (isupper(c) ? TRUE : FALSE)
62 /* how to position us on player board */
65 #define PY(y) (PYBASE + (y))
66 #define PX(x) (PXBASE + (x)*3)
67 #define pgoto(y, x) (void)move(PY(y), PX(x))
69 /* how to position us on cpu board */
72 #define CY(y) (CYBASE + (y))
73 #define CX(x) (CXBASE + (x)*3)
74 #define CYINV(y) ((y) - CYBASE)
75 #define CXINV(x) (((x) - CXBASE) / 3)
76 #define cgoto(y, x) (void)move(CY(y), CX(x))
78 #define ONBOARD(x, y) (x >= 0 && x < BWIDTH && y >= 0 && y < BDEPTH)
80 /* other board locations */
82 #define PROMPTLINE 21 /* prompt line */
83 #define SYBASE CYBASE + BDEPTH + 3 /* move key diagram */
85 #define MYBASE SYBASE - 1 /* diagram caption */
87 #define HYBASE SYBASE - 1 /* help area */
90 /* this will need to be changed if BWIDTH changes */
91 static char numbers[] = " 0 1 2 3 4 5 6 7 8 9";
93 static char carrier[] = "Aircraft Carrier";
94 static char battle[] = "Battleship";
95 static char sub[] = "Submarine";
96 static char destroy[] = "Destroyer";
97 static char ptboat[] = "PT Boat";
100 static char dftname[] = "stranger";
102 /* direction constants */
111 static int xincr[8] = {1, 1, 0, -1, -1, -1, 0, 1};
112 static int yincr[8] = {0, 1, 1, 1, 0, -1, -1, -1};
114 /* current ship position and direction */
115 static int curx = (BWIDTH / 2);
116 static int cury = (BDEPTH / 2);
120 char *name; /* name of the ship type */
121 int hits; /* how many times has this ship been hit? */
122 char symbol; /* symbol for game purposes */
123 int length; /* length of ship */
124 char x, y; /* coordinates of ship start point */
125 unsigned char dir; /* direction of `bow' */
126 bool placed; /* has it been placed on the board? */
130 static bool checkplace(int b, ship_t *ss, int vis);
132 #define SHIPIT(name, symbol, length) { name, 0, symbol, length, 0,0, 0, FALSE }
134 static ship_t plyship[SHIPTYPES] =
136 SHIPIT(carrier, 'A', 5),
137 SHIPIT(battle, 'B', 4),
138 SHIPIT(destroy, 'D', 3),
140 SHIPIT(ptboat, 'P', 2),
143 static ship_t cpuship[SHIPTYPES] =
145 SHIPIT(carrier, 'A', 5),
146 SHIPIT(battle, 'B', 4),
147 SHIPIT(destroy, 'D', 3),
149 SHIPIT(ptboat, 'P', 2),
152 /* "Hits" board, and main board. */
153 static char hits[2][BWIDTH][BDEPTH];
154 static char board[2][BWIDTH][BDEPTH];
156 static int turn; /* 0=player, 1=computer */
157 static int plywon=0, cpuwon=0; /* How many games has each won? */
159 static int salvo, blitz, closepack;
161 #define PR (void)addstr
163 static RETSIGTYPE uninitgame(int sig) GCC_NORETURN;
165 static RETSIGTYPE uninitgame(int sig GCC_UNUSED)
166 /* end the game, either normally or due to signal */
176 static void announceopts(void)
177 /* announce which game options are enabled */
179 if (salvo || blitz || closepack)
181 (void) printw("Playing optional game (");
183 (void) printw("salvo, ");
185 (void) printw("nosalvo, ");
187 (void) printw("blitz ");
189 (void) printw("noblitz, ");
191 (void) printw("closepack)");
193 (void) printw("noclosepack)");
197 "Playing standard game (noblitz, nosalvo, noclosepack)");
200 static void intro(void)
204 srand((unsigned)(time(0L)+getpid())); /* Kick the random number generator */
206 (void) signal(SIGINT,uninitgame);
207 (void) signal(SIGINT,uninitgame);
208 (void) signal(SIGIOT,uninitgame); /* for assert(3) */
209 if(signal(SIGQUIT,SIG_IGN) != SIG_IGN)
210 (void)signal(SIGQUIT,uninitgame);
212 if((tmpname = getlogin()) != 0)
214 (void)strcpy(name,tmpname);
215 name[0] = toupper(name[0]);
218 (void)strcpy(name,dftname);
222 keypad(stdscr, TRUE);
231 (void)mvaddstr(4,29,"Welcome to Battleship!");
235 PR(" \\ \\ \\ \\ \\_____________\n");
236 PR(" \\ \\ \\_____________ \\ \\/ |\n");
237 PR(" \\ \\/ \\ \\/ |\n");
238 PR(" \\/ \\_____/ |__\n");
239 PR(" ________________/ |\n");
240 PR(" \\ S.S. Penguin |\n");
242 PR(" \\___________________________________________________/\n");
244 (void) mvaddstr(22,27,"Hit any key to continue..."); (void)refresh();
251 init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
252 init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
253 init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
254 init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
255 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
256 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
257 init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
258 init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
261 #ifdef NCURSES_MOUSE_VERSION
262 (void) mousemask(BUTTON1_CLICKED, (mmask_t *)NULL);
263 #endif /* NCURSES_MOUSE_VERSION*/
267 static void prompt(int n, NCURSES_CONST char *f, const char *s)
268 /* print a message at the prompt line */
270 (void) move(PROMPTLINE + n, 0);
276 static void error(NCURSES_CONST char *s)
278 (void) move(PROMPTLINE + 2, 0);
287 static void placeship(int b, ship_t *ss, int vis)
291 for(l = 0; l < ss->length; ++l)
293 int newx = ss->x + l * xincr[ss->dir];
294 int newy = ss->y + l * yincr[ss->dir];
296 board[b][newx][newy] = ss->symbol;
300 (void) addch((chtype)ss->symbol);
306 static int rnd(int n)
308 return(((rand() & 0x7FFF) % n));
311 static void randomplace(int b, ship_t *ss)
312 /* generate a valid random ship placement into px,py */
314 register int bwidth = BWIDTH - ss->length;
315 register int bdepth = BDEPTH - ss->length;
320 ss->dir = rnd(2) ? E : S;
322 (!checkplace(b, ss, FALSE));
325 static void initgame(void)
331 (void) mvaddstr(0,35,"BATTLESHIPS");
332 (void) move(PROMPTLINE + 2, 0);
335 memset(board, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
336 memset(hits, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
337 for (i = 0; i < SHIPTYPES; i++)
356 /* draw empty boards */
357 (void) mvaddstr(PYBASE - 2, PXBASE + 5, "Main Board");
358 (void) mvaddstr(PYBASE - 1, PXBASE - 3,numbers);
359 for(i=0; i < BDEPTH; ++i)
361 (void) mvaddch(PYBASE + i, PXBASE - 3, (chtype)(i + 'A'));
364 attron(COLOR_PAIR(COLOR_BLUE));
367 for (j = 0; j < BWIDTH; j++)
368 (void) addstr(" . ");
373 (void) addch((chtype)(i + 'A'));
375 (void) mvaddstr(PYBASE + BDEPTH, PXBASE - 3,numbers);
376 (void) mvaddstr(CYBASE - 2, CXBASE + 7,"Hit/Miss Board");
377 (void) mvaddstr(CYBASE - 1, CXBASE - 3, numbers);
378 for(i=0; i < BDEPTH; ++i)
380 (void) mvaddch(CYBASE + i, CXBASE - 3, (chtype)(i + 'A'));
383 attron(COLOR_PAIR(COLOR_BLUE));
386 for (j = 0; j < BWIDTH; j++)
387 (void) addstr(" . ");
392 (void) addch((chtype)(i + 'A'));
395 (void) mvaddstr(CYBASE + BDEPTH,CXBASE - 3,numbers);
397 (void) mvprintw(HYBASE, HXBASE,
398 "To position your ships: move the cursor to a spot, then");
399 (void) mvprintw(HYBASE+1,HXBASE,
400 "type the first letter of a ship type to select it, then");
401 (void) mvprintw(HYBASE+2,HXBASE,
402 "type a direction ([hjkl] or [4862]), indicating how the");
403 (void) mvprintw(HYBASE+3,HXBASE,
404 "ship should be pointed. You may also type a ship letter");
405 (void) mvprintw(HYBASE+4,HXBASE,
406 "followed by `r' to position it randomly, or type `R' to");
407 (void) mvprintw(HYBASE+5,HXBASE,
408 "place all remaining ships randomly.");
410 (void) mvaddstr(MYBASE, MXBASE, "Aiming keys:");
411 (void) mvaddstr(SYBASE, SXBASE, "y k u 7 8 9");
412 (void) mvaddstr(SYBASE+1, SXBASE, " \\|/ \\|/ ");
413 (void) mvaddstr(SYBASE+2, SXBASE, "h-+-l 4-+-6");
414 (void) mvaddstr(SYBASE+3, SXBASE, " /|\\ /|\\ ");
415 (void) mvaddstr(SYBASE+4, SXBASE, "b j n 1 2 3");
417 /* have the computer place ships */
418 for(ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
420 randomplace(COMPUTER, ss);
421 placeship(COMPUTER, ss, FALSE);
426 char c, docked[SHIPTYPES + 2], *cp = docked;
428 /* figure which ships still wait to be placed */
430 for (i = 0; i < SHIPTYPES; i++)
431 if (!plyship[i].placed)
432 *cp++ = plyship[i].symbol;
435 /* get a command letter */
436 prompt(1, "Type one of [%s] to pick a ship.", docked+1);
438 c = getcoord(PLAYER);
440 (!strchr(docked, c));
446 /* map that into the corresponding symbol */
447 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
451 prompt(1, "Type one of [hjklrR] to place your %s.", ss->name);
458 (!strchr("hjklrR", c) || c == FF);
462 (void)clearok(stdscr, TRUE);
467 prompt(1, "Random-placing your %s", ss->name);
468 randomplace(PLAYER, ss);
469 placeship(PLAYER, ss, TRUE);
475 prompt(1, "Placing the rest of your fleet at random...", "");
476 for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
479 randomplace(PLAYER, ss);
480 placeship(PLAYER, ss, TRUE);
485 else if (strchr("hjkl8462", c))
492 case 'k': case '8': ss->dir = N; break;
493 case 'j': case '2': ss->dir = S; break;
494 case 'h': case '4': ss->dir = W; break;
495 case 'l': case '6': ss->dir = E; break;
498 if (checkplace(PLAYER, ss, TRUE))
500 placeship(PLAYER, ss, TRUE);
506 for (unplaced = i = 0; i < SHIPTYPES; i++)
507 unplaced += !plyship[i].placed;
513 (void) mvprintw(HYBASE, HXBASE,
514 "To fire, move the cursor to your chosen aiming point ");
515 (void) mvprintw(HYBASE+1, HXBASE,
516 "and strike any key other than a motion key. ");
517 (void) mvprintw(HYBASE+2, HXBASE,
519 (void) mvprintw(HYBASE+3, HXBASE,
521 (void) mvprintw(HYBASE+4, HXBASE,
523 (void) mvprintw(HYBASE+5, HXBASE,
526 (void) prompt(0, "Press any key to start...", "");
530 static int getcoord(int atcpu)
543 (void) mvprintw(CYBASE + BDEPTH+1, CXBASE+11, "(%d, %c)", curx, 'A'+cury);
548 (void) mvprintw(PYBASE + BDEPTH+1, PXBASE+11, "(%d, %c)", curx, 'A'+cury);
558 ny = cury+BDEPTH-1; nx = curx;
564 ny = cury+1; nx = curx;
570 ny = cury; nx = curx+BWIDTH-1;
576 ny = cury; nx = curx+1;
582 ny = cury+BDEPTH-1; nx = curx+BWIDTH-1;
588 ny = cury+1; nx = curx+BWIDTH-1;
594 ny = cury+BDEPTH-1; nx = curx+1;
600 ny = cury+1; nx = curx+1;
603 nx = curx; ny = cury;
604 (void)clearok(stdscr, TRUE);
607 #ifdef NCURSES_MOUSE_VERSION
614 && myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
615 && myevent.x >= CX(0) && myevent.x <= CX(BDEPTH))
617 curx = CXINV(myevent.x);
618 cury = CYINV(myevent.y);
627 /* no fall through */
628 #endif /* NCURSES_MOUSE_VERSION */
632 (void) mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, " ");
634 (void) mvaddstr(PYBASE + BDEPTH + 1, PXBASE + 11, " ");
643 static bool collidecheck(int b, int y, int x)
644 /* is this location on the selected zboard adjacent to a ship? */
648 /* anything on the square */
649 if ((collide = IS_SHIP(board[b][x][y])) != FALSE)
652 /* anything on the neighbors */
657 for (i = 0; i < 8; i++)
663 if (ONBOARD(xend, yend)
664 && IS_SHIP(board[b][xend][yend])) {
673 static bool checkplace(int b, ship_t *ss, int vis)
677 /* first, check for board edges */
678 xend = ss->x + ss->length * xincr[ss->dir];
679 yend = ss->y + ss->length * yincr[ss->dir];
680 if (!ONBOARD(xend, yend))
686 error("Ship is hanging from the edge of the world");
689 error("Try fitting it on the board");
692 error("Figure I won't find it if you put it there?");
698 for(l = 0; l < ss->length; ++l)
700 if(collidecheck(b, ss->y+l*yincr[ss->dir], ss->x+l*xincr[ss->dir]))
706 error("There's already a ship there");
709 error("Collision alert! Aaaaaagh!");
712 error("Er, Admiral, what about the other ship?");
721 static int awinna(void)
728 ss = (i) ? cpuship : plyship;
729 for(j=0; j < SHIPTYPES; ++j, ++ss)
730 if(ss->length > ss->hits)
738 static ship_t *hitship(int x, int y)
739 /* register a hit on the targeted ship */
745 getyx(stdscr, oldy, oldx);
746 sb = (turn) ? plyship : cpuship;
747 if((sym = board[OTHER][x][y]) == 0)
748 return((ship_t *)NULL);
749 for(ss = sb; ss < sb + SHIPTYPES; ++ss)
750 if(ss->symbol == sym)
752 if (++ss->hits < ss->length) /* still afloat? */
753 return((ship_t *)NULL);
759 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)
768 x1 = bx + i * xincr[ss->dir];
769 y1 = by + i * yincr[ss->dir];
772 hits[turn][x1][y1] = MARK_MISS;
773 if (turn % 2 == PLAYER)
778 attron(COLOR_PAIR(COLOR_GREEN));
780 (void)addch(MARK_MISS);
789 for (i = 0; i < ss->length; ++i)
791 int x1 = ss->x + i * xincr[ss->dir];
792 int y1 = ss->y + i * yincr[ss->dir];
794 hits[turn][x1][y1] = ss->symbol;
795 if (turn % 2 == PLAYER)
798 (void) addch((chtype)(ss->symbol));
802 (void) move(oldy, oldx);
806 (void) move(oldy, oldx);
807 return((ship_t *)NULL);
810 static int plyturn(void)
814 NCURSES_CONST char *m = NULL;
816 prompt(1, "Where do you want to shoot? ", "");
819 (void) getcoord(COMPUTER);
820 if (hits[PLAYER][curx][cury])
822 prompt(1, "You shelled this spot already! Try again.", "");
828 hit = IS_SHIP(board[COMPUTER][curx][cury]);
829 hits[PLAYER][curx][cury] = (hit ? MARK_HIT : MARK_MISS);
834 attron(COLOR_PAIR(COLOR_RED));
836 attron(COLOR_PAIR(COLOR_GREEN));
839 (void) addch((chtype)hits[PLAYER][curx][cury]);
844 prompt(1, "You %s.", hit ? "scored a hit" : "missed");
845 if(hit && (ss = hitship(curx, cury)))
850 m = " You sank my %s!";
853 m = " I have this sinking feeling about my %s....";
856 m = " My %s has gone to Davy Jones's locker!";
859 m = " Glub, glub -- my %s is headed for the bottom!";
862 m = " You'll pick up survivors from my %s, I hope...!";
865 (void)printw(m, ss->name);
867 return(awinna() == -1);
872 static int sgetc(const char *s)
885 for (s1=s; *s1 && ch != *s1; ++s1)
889 (void) addch((chtype)ch);
897 static void randomfire(int *px, int *py)
898 /* random-fire routine -- implements simple diagonal-striping strategy */
900 static int turncount = 0;
901 static int srchstep = BEGINSTEP;
902 static int huntoffs; /* Offset on search strategy */
903 int ypossible[BWIDTH * BDEPTH], xpossible[BWIDTH * BDEPTH], nposs;
904 int ypreferred[BWIDTH * BDEPTH], xpreferred[BWIDTH * BDEPTH], npref;
907 if (turncount++ == 0)
908 huntoffs = rnd(srchstep);
910 /* first, list all possible moves */
912 for (x = 0; x < BWIDTH; x++)
913 for (y = 0; y < BDEPTH; y++)
914 if (!hits[COMPUTER][x][y])
916 xpossible[nposs] = x;
917 ypossible[nposs] = y;
919 if (((x+huntoffs) % srchstep) != (y % srchstep))
921 xpreferred[npref] = x;
922 ypreferred[npref] = y;
946 error("No moves possible?? Help!");
956 static int cpufire(int x, int y)
957 /* fire away at given location */
962 hits[COMPUTER][x][y] = (hit = (board[PLAYER][x][y])) ? MARK_HIT : MARK_MISS;
963 (void) mvprintw(PROMPTLINE, 0,
964 "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" : "miss");
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 (void)addch((chtype)(hit ? SHOWHIT : SHOWSPLASH));
983 return ((hit ? (sunk ? S_SUNK : S_HIT) : S_MISS) ? TRUE : FALSE);
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 static bool cputurn(void)
993 #define POSSIBLE(x, y) (ONBOARD(x, y) && !hits[COMPUTER][x][y])
994 #define RANDOM_FIRE 0
996 #define HUNT_DIRECT 2
998 #define REVERSE_JUMP 4
999 #define SECOND_PASS 5
1000 static int next = RANDOM_FIRE;
1001 static bool used[4];
1003 int navail, x, y, d, n;
1008 case RANDOM_FIRE: /* last shot was random and missed */
1011 if (!(hit = cpufire(x, y)))
1017 next = (hit == S_SUNK) ? RANDOM_FIRE : RANDOM_HIT;
1021 case RANDOM_HIT: /* last shot was random and hit */
1022 used[E/2] = used[S/2] = used[W/2] = used[N/2] = FALSE;
1025 case HUNT_DIRECT: /* last shot hit, we're looking for ship's long axis */
1026 for (d = navail = 0; d < 4; d++)
1028 x = ts.x + xincr[d*2]; y = ts.y + yincr[d*2];
1029 if (!used[d] && POSSIBLE(x, y))
1034 if (navail == 0) /* no valid places for shots adjacent... */
1035 goto refire; /* ...so we must random-fire */
1038 for (d = 0, n = rnd(navail) + 1; n; n--)
1045 x = ts.x + xincr[d*2];
1046 y = ts.y + yincr[d*2];
1048 assert(POSSIBLE(x, y));
1050 if (!(hit = cpufire(x, y)))
1054 ts.x = x; ts.y = y; ts.dir = d*2; ts.hits++;
1055 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1060 case FIRST_PASS: /* we have a start and a direction now */
1061 x = ts.x + xincr[ts.dir];
1062 y = ts.y + yincr[ts.dir];
1063 if (POSSIBLE(x, y) && (hit = cpufire(x, y)))
1065 ts.x = x; ts.y = y; ts.hits++;
1066 next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
1069 next = REVERSE_JUMP;
1072 case REVERSE_JUMP: /* nail down the ship's other end */
1074 x = ts.x + ts.hits * xincr[d];
1075 y = ts.y + ts.hits * yincr[d];
1076 if (POSSIBLE(x, y) && (hit = cpufire(x, y)))
1078 ts.x = x; ts.y = y; ts.dir = d; ts.hits++;
1079 next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1085 case SECOND_PASS: /* kill squares not caught on first pass */
1086 x = ts.x + xincr[ts.dir];
1087 y = ts.y + yincr[ts.dir];
1088 if (POSSIBLE(x, y) && (hit = cpufire(x, y)))
1090 ts.x = x; ts.y = y; ts.hits++;
1091 next = (hit == S_SUNK) ? RANDOM_FIRE: SECOND_PASS;
1099 /* check for continuation and/or winner */
1109 (void) mvprintw(PROMPTLINE + 2, 0,
1110 "New state %d, x=%d, y=%d, d=%d",
1113 return ((hit) ? TRUE : FALSE);
1122 for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
1123 for(j = 0; j < ss->length; j++)
1125 cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir]);
1126 (void)addch((chtype)ss->symbol);
1133 j = 18 + strlen(name);
1138 (void) mvprintw(1,(COLWIDTH-j)/2,
1139 "%s: %d Computer: %d",name,plywon,cpuwon);
1141 prompt(2, (awinna()) ? "Want to be humiliated again, %s [yn]? "
1142 : "Going to give me a chance for revenge, %s [yn]? ",name);
1143 return(sgetc("YN") == 'Y');
1146 static void do_options(int c, char *op[])
1158 (void) fprintf(stderr, "Usage: battle [-s | -b] [-c]\n");
1159 (void) fprintf(stderr, "\tWhere the options are:\n");
1160 (void) fprintf(stderr, "\t-s : play a salvo game\n");
1161 (void) fprintf(stderr, "\t-b : play a blitz game\n");
1162 (void) fprintf(stderr, "\t-c : ships may be adjacent\n");
1172 (void) fprintf(stderr,
1173 "Bad Arg: -b and -s are mutually exclusive\n");
1181 (void) fprintf(stderr,
1182 "Bad Arg: -s and -b are mutually exclusive\n");
1190 (void) fprintf(stderr,
1191 "Bad arg: type \"%s ?\" for usage message\n", op[0]);
1199 static int scount(int who)
1201 register int i, shots;
1202 register ship_t *sp;
1205 sp = cpuship; /* count cpu shots */
1207 sp = plyship; /* count player shots */
1209 for (i=0, shots = 0; i < SHIPTYPES; i++, sp++)
1211 if (sp->hits >= sp->length)
1212 continue; /* dead ship */
1219 int main(int argc, char *argv[])
1221 do_options(argc, argv);
1226 while(awinna() == -1)
1246 if (cputurn() && awinna() != -1)
1251 if (plyturn() && awinna() != -1)
1258 while(turn ? cputurn() : plyturn())
1268 /* bs.c ends here */