]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/bs.c
ncurses 6.1 - patch 20191005
[ncurses.git] / test / bs.c
index 518308a0f31437878e60c33f979b1439fd84c9aa..4de4dbc7f987f3a4141f3305d1cfd5bbbf8b0d31 100644 (file)
--- a/test/bs.c
+++ b/test/bs.c
@@ -1,3 +1,30 @@
+/****************************************************************************
+ * Copyright (c) 1998-2018,2019 Free Software Foundation, Inc.              *
+ *                                                                          *
+ * Permission is hereby granted, free of charge, to any person obtaining a  *
+ * copy of this software and associated documentation files (the            *
+ * "Software"), to deal in the Software without restriction, including      *
+ * without limitation the rights to use, copy, modify, merge, publish,      *
+ * distribute, distribute with modifications, sublicense, and/or sell       *
+ * copies of the Software, and to permit persons to whom the Software is    *
+ * furnished to do so, subject to the following conditions:                 *
+ *                                                                          *
+ * The above copyright notice and this permission notice shall be included  *
+ * in all copies or substantial portions of the Software.                   *
+ *                                                                          *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
+ * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
+ *                                                                          *
+ * Except as contained in this notice, the name(s) of the above copyright   *
+ * holders shall not be used in advertising or otherwise to promote the     *
+ * sale, use or other dealings in this Software without prior written       *
+ * authorization.                                                           *
+ ****************************************************************************/
 /* 
  * bs.c - original author: Bruce Holloway
  *             salvo option by: Chuck A DeGaul
  * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
  * v2.1 with ncurses mouse support, September 1995
  *
- * $Id: bs.c,v 1.36 2002/06/29 23:32:18 tom Exp $
+ * $Id: bs.c,v 1.74 2019/08/17 21:49:19 tom Exp $
  */
 
-#include <ctype.h>
-#include <assert.h>
-#include <time.h>
-
 #include <test.priv.h>
 
+#include <time.h>
+
 #ifndef SIGIOT
 #define SIGIOT SIGABRT
 #endif
@@ -39,6 +64,8 @@ static int getcoord(int);
 #define CTRLC          '\003'  /* used as terminate command */
 #define FF             '\014'  /* used as redraw command */
 
+#define is_QUIT(c) ((c) == CTRLC || (c) == QUIT)
+
 /* coordinate handling */
 #define BWIDTH         10
 #define BDEPTH         10
@@ -85,21 +112,24 @@ static char sub[] = "Submarine";
 static char destroy[] = "Destroyer";
 static char ptboat[] = "PT Boat";
 
-static char name[40];
+static char *your_name;
 static char dftname[] = "stranger";
 
 /* direction constants */
-#define E      0
-#define SE     1
-#define S      2
-#define SW     3
-#define W      4
-#define NW     5
-#define N      6
-#define NE     7
-static int xincr[8] =
+typedef enum {
+    dir_E = 0
+    ,dir_SE
+    ,dir_S
+    ,dir_SW
+    ,dir_W
+    ,dir_NW
+    ,dir_N
+    ,dir_NE
+    ,dir_MAX
+} DIRECTIONS;
+static int xincr[dir_MAX + 2] =
 {1, 1, 0, -1, -1, -1, 0, 1};
-static int yincr[8] =
+static int yincr[dir_MAX + 2] =
 {0, 1, 1, 1, 0, -1, -1, -1};
 
 /* current ship position and direction */
@@ -120,6 +150,7 @@ static bool checkplace(int b, ship_t * ss, int vis);
 
 #define SHIPIT(name, symbol, length) { name, 0, symbol, length, 0,0, 0, FALSE }
 
+/* "ply=player", "cpu=computer" */
 static ship_t plyship[SHIPTYPES] =
 {
     SHIPIT(carrier, 'A', 5),
@@ -149,9 +180,10 @@ static int salvo, blitz, closepack;
 
 #define        PR      (void)addstr
 
-static RETSIGTYPE uninitgame(int sig) GCC_NORETURN;
+static void uninitgame(int sig) GCC_NORETURN;
 
-static RETSIGTYPE uninitgame(int sig GCC_UNUSED)
+static void
+uninitgame(int sig GCC_UNUSED)
 /* end the game, either normally or due to signal */
 {
     clear();
@@ -159,6 +191,7 @@ static RETSIGTYPE uninitgame(int sig GCC_UNUSED)
     (void) reset_shell_mode();
     (void) echo();
     (void) endwin();
+    free(your_name);
     ExitProgram(sig ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
@@ -188,23 +221,19 @@ announceopts(void)
 static void
 intro(void)
 {
-    char *tmpname;
+    const char *tmpname;
 
     srand((unsigned) (time(0L) + getpid()));   /* Kick the random number generator */
 
-    (void) signal(SIGINT, uninitgame);
-    (void) signal(SIGINT, uninitgame);
-    (void) signal(SIGIOT, uninitgame); /* for assert(3) */
-    if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
-       (void) signal(SIGQUIT, uninitgame);
+    InitAndCatch(initscr(), uninitgame);
 
-    if ((tmpname = getlogin()) != 0) {
-       (void) strcpy(name, tmpname);
-       name[0] = toupper(name[0]);
-    } else
-       (void) strcpy(name, dftname);
+    if ((tmpname = getlogin()) != 0 &&
+       (your_name = strdup(tmpname)) != 0) {
+       your_name[0] = (char) toupper(UChar(your_name[0]));
+    } else {
+       your_name = strdup(dftname);
+    }
 
-    (void) initscr();
     keypad(stdscr, TRUE);
     (void) def_prog_mode();
     (void) nonl();
@@ -213,7 +242,7 @@ intro(void)
 
 #ifdef PENGUIN
     (void) clear();
-    (void) mvaddstr(4, 29, "Welcome to Battleship!");
+    MvAddStr(4, 29, "Welcome to Battleship!");
     (void) move(8, 0);
     PR("                                                  \\\n");
     PR("                           \\                     \\ \\\n");
@@ -226,7 +255,7 @@ intro(void)
     PR("            \\                                                     /\n");
     PR("             \\___________________________________________________/\n");
 
-    (void) mvaddstr(22, 27, "Hit any key to continue...");
+    MvAddStr(22, 27, "Hit any key to continue...");
     (void) refresh();
     (void) getch();
 #endif /* PENGUIN */
@@ -283,7 +312,7 @@ placeship(int b, ship_t * ss, int vis)
        board[b][newx][newy] = ss->symbol;
        if (vis) {
            pgoto(newy, newx);
-           (void) addch((chtype) ss->symbol);
+           AddCh(ss->symbol);
        }
     }
     ss->hits = 0;
@@ -301,9 +330,9 @@ randomplace(int b, ship_t * ss)
 {
 
     do {
-       ss->dir = rnd(2) ? E : S;
-       ss->x = rnd(BWIDTH - (ss->dir == E ? ss->length : 0));
-       ss->y = rnd(BDEPTH - (ss->dir == S ? ss->length : 0));
+       ss->dir = rnd(2) ? dir_E : dir_S;
+       ss->x = rnd(BWIDTH - (ss->dir == dir_E ? ss->length : 0));
+       ss->y = rnd(BDEPTH - (ss->dir == dir_S ? ss->length : 0));
     } while
        (!checkplace(b, ss, FALSE));
 }
@@ -315,7 +344,7 @@ initgame(void)
     ship_t *ss;
 
     (void) clear();
-    (void) mvaddstr(0, 35, "BATTLESHIPS");
+    MvAddStr(0, 35, "BATTLESHIPS");
     (void) move(PROMPTLINE + 2, 0);
     announceopts();
 
@@ -340,63 +369,63 @@ initgame(void)
     }
 
     /* draw empty boards */
-    (void) mvaddstr(PYBASE - 2, PXBASE + 5, "Main Board");
-    (void) mvaddstr(PYBASE - 1, PXBASE - 3, numbers);
+    MvAddStr(PYBASE - 2, PXBASE + 5, "Main Board");
+    MvAddStr(PYBASE - 1, PXBASE - 3, numbers);
     for (i = 0; i < BDEPTH; ++i) {
-       (void) mvaddch(PYBASE + i, PXBASE - 3, (chtype) (i + 'A'));
+       MvAddCh(PYBASE + i, PXBASE - 3, (chtype) (i + 'A'));
 #ifdef A_COLOR
        if (has_colors())
            attron(COLOR_PAIR(COLOR_BLUE));
 #endif /* A_COLOR */
-       (void) addch(' ');
+       AddCh(' ');
        for (j = 0; j < BWIDTH; j++)
            (void) addstr(" . ");
 #ifdef A_COLOR
-       attrset(0);
+       (void) attrset(0);
 #endif /* A_COLOR */
-       (void) addch(' ');
-       (void) addch((chtype) (i + 'A'));
+       AddCh(' ');
+       AddCh(i + 'A');
     }
-    (void) mvaddstr(PYBASE + BDEPTH, PXBASE - 3, numbers);
-    (void) mvaddstr(CYBASE - 2, CXBASE + 7, "Hit/Miss Board");
-    (void) mvaddstr(CYBASE - 1, CXBASE - 3, numbers);
+    MvAddStr(PYBASE + BDEPTH, PXBASE - 3, numbers);
+    MvAddStr(CYBASE - 2, CXBASE + 7, "Hit/Miss Board");
+    MvAddStr(CYBASE - 1, CXBASE - 3, numbers);
     for (i = 0; i < BDEPTH; ++i) {
-       (void) mvaddch(CYBASE + i, CXBASE - 3, (chtype) (i + 'A'));
+       MvAddCh(CYBASE + i, CXBASE - 3, (chtype) (i + 'A'));
 #ifdef A_COLOR
        if (has_colors())
            attron(COLOR_PAIR(COLOR_BLUE));
 #endif /* A_COLOR */
-       (void) addch(' ');
+       AddCh(' ');
        for (j = 0; j < BWIDTH; j++)
            (void) addstr(" . ");
 #ifdef A_COLOR
-       attrset(0);
+       (void) attrset(0);
 #endif /* A_COLOR */
-       (void) addch(' ');
-       (void) addch((chtype) (i + 'A'));
+       AddCh(' ');
+       AddCh(i + 'A');
     }
 
-    (void) mvaddstr(CYBASE + BDEPTH, CXBASE - 3, numbers);
-
-    (void) mvprintw(HYBASE, HXBASE,
-                   "To position your ships: move the cursor to a spot, then");
-    (void) mvprintw(HYBASE + 1, HXBASE,
-                   "type the first letter of a ship type to select it, then");
-    (void) mvprintw(HYBASE + 2, HXBASE,
-                   "type a direction ([hjkl] or [4862]), indicating how the");
-    (void) mvprintw(HYBASE + 3, HXBASE,
-                   "ship should be pointed. You may also type a ship letter");
-    (void) mvprintw(HYBASE + 4, HXBASE,
-                   "followed by `r' to position it randomly, or type `R' to");
-    (void) mvprintw(HYBASE + 5, HXBASE,
-                   "place all remaining ships randomly.");
-
-    (void) mvaddstr(MYBASE, MXBASE, "Aiming keys:");
-    (void) mvaddstr(SYBASE, SXBASE, "y k u    7 8 9");
-    (void) mvaddstr(SYBASE + 1, SXBASE, " \\|/      \\|/ ");
-    (void) mvaddstr(SYBASE + 2, SXBASE, "h-+-l    4-+-6");
-    (void) mvaddstr(SYBASE + 3, SXBASE, " /|\\      /|\\ ");
-    (void) mvaddstr(SYBASE + 4, SXBASE, "b j n    1 2 3");
+    MvAddStr(CYBASE + BDEPTH, CXBASE - 3, numbers);
+
+    MvPrintw(HYBASE, HXBASE,
+            "To position your ships: move the cursor to a spot, then");
+    MvPrintw(HYBASE + 1, HXBASE,
+            "type the first letter of a ship type to select it, then");
+    MvPrintw(HYBASE + 2, HXBASE,
+            "type a direction ([hjkl] or [4862]), indicating how the");
+    MvPrintw(HYBASE + 3, HXBASE,
+            "ship should be pointed. You may also type a ship letter");
+    MvPrintw(HYBASE + 4, HXBASE,
+            "followed by `r' to position it randomly, or type `R' to");
+    MvPrintw(HYBASE + 5, HXBASE,
+            "place all remaining ships randomly.");
+
+    MvAddStr(MYBASE, MXBASE, "Aiming keys:");
+    MvAddStr(SYBASE, SXBASE, "y k u    7 8 9");
+    MvAddStr(SYBASE + 1, SXBASE, " \\|/      \\|/ ");
+    MvAddStr(SYBASE + 2, SXBASE, "h-+-l    4-+-6");
+    MvAddStr(SYBASE + 3, SXBASE, " /|\\      /|\\ ");
+    MvAddStr(SYBASE + 4, SXBASE, "b j n    1 2 3");
 
     /* have the computer place ships */
     for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++) {
@@ -404,10 +433,11 @@ initgame(void)
        placeship(COMPUTER, ss, FALSE);
     }
 
-    ss = (ship_t *) NULL;
     do {
        char c, docked[SHIPTYPES + 2], *cp = docked;
 
+       ss = (ship_t *) NULL;
+
        /* figure which ships still wait to be placed */
        *cp++ = 'R';
        for (i = 0; i < SHIPTYPES; i++)
@@ -418,9 +448,9 @@ initgame(void)
        /* get a command letter */
        prompt(1, "Type one of [%s] to pick a ship.", docked + 1);
        do {
-           c = getcoord(PLAYER);
+           c = (char) getcoord(PLAYER);
        } while
-           (!strchr(docked, c));
+           (!(strchr) (docked, c));
 
        if (c == 'R')
            (void) ungetch('R');
@@ -435,13 +465,17 @@ initgame(void)
        }
 
        do {
-           c = getch();
+           c = (char) getch();
        } while
-           (!strchr("hjklrR", c) || c == FF);
+           (!(strchr("hjkl8462rR", c) || c == FF || is_QUIT(c)));
 
-       if (c == FF) {
+       if (is_QUIT(c)) {
+           uninitgame(0);
+       } else if (c == FF) {
            (void) clearok(stdscr, TRUE);
            (void) refresh();
+       } else if (ss == 0) {
+           beep();             /* simple to verify, unlikely to happen */
        } else if (c == 'r') {
            prompt(1, "Random-placing your %s", ss->name);
            randomplace(PLAYER, ss);
@@ -464,19 +498,19 @@ initgame(void)
            switch (c) {
            case 'k':
            case '8':
-               ss->dir = N;
+               ss->dir = dir_N;
                break;
            case 'j':
            case '2':
-               ss->dir = S;
+               ss->dir = dir_S;
                break;
            case 'h':
            case '4':
-               ss->dir = W;
+               ss->dir = dir_W;
                break;
            case 'l':
            case '6':
-               ss->dir = E;
+               ss->dir = dir_E;
                break;
            }
 
@@ -494,18 +528,18 @@ initgame(void)
 
     turn = rnd(2);
 
-    (void) mvprintw(HYBASE, HXBASE,
-                   "To fire, move the cursor to your chosen aiming point   ");
-    (void) mvprintw(HYBASE + 1, HXBASE,
-                   "and strike any key other than a motion key.            ");
-    (void) mvprintw(HYBASE + 2, HXBASE,
-                   "                                                       ");
-    (void) mvprintw(HYBASE + 3, HXBASE,
-                   "                                                       ");
-    (void) mvprintw(HYBASE + 4, HXBASE,
-                   "                                                       ");
-    (void) mvprintw(HYBASE + 5, HXBASE,
-                   "                                                       ");
+    MvPrintw(HYBASE, HXBASE,
+            "To fire, move the cursor to your chosen aiming point   ");
+    MvPrintw(HYBASE + 1, HXBASE,
+            "and strike any key other than a motion key.            ");
+    MvPrintw(HYBASE + 2, HXBASE,
+            "                                                       ");
+    MvPrintw(HYBASE + 3, HXBASE,
+            "                                                       ");
+    MvPrintw(HYBASE + 4, HXBASE,
+            "                                                       ");
+    MvPrintw(HYBASE + 5, HXBASE,
+            "                                                       ");
 
     (void) prompt(0, "Press any key to start...", "");
     (void) getch();
@@ -514,21 +548,22 @@ initgame(void)
 static int
 getcoord(int atcpu)
 {
-    int ny, nx, c;
-
     if (atcpu)
        cgoto(cury, curx);
     else
        pgoto(cury, curx);
     (void) refresh();
+
     for (;;) {
+       int ny, nx, c;
+
        if (atcpu) {
-           (void) mvprintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
-                           curx, 'A' + cury);
+           MvPrintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
+                    curx, 'A' + cury);
            cgoto(cury, curx);
        } else {
-           (void) mvprintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
-                           curx, 'A' + cury);
+           MvPrintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
+                    curx, 'A' + cury);
            pgoto(cury, curx);
        }
 
@@ -609,9 +644,9 @@ getcoord(int atcpu)
 
        default:
            if (atcpu)
-               (void) mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, "      ");
+               MvAddStr(CYBASE + BDEPTH + 1, CXBASE + 11, "      ");
            else
-               (void) mvaddstr(PYBASE + BDEPTH + 1, PXBASE + 11, "      ");
+               MvAddStr(PYBASE + BDEPTH + 1, PXBASE + 11, "      ");
            return (c);
        }
 
@@ -634,7 +669,7 @@ collidecheck(int b, int y, int x)
     if (!closepack) {
        int i;
 
-       for (i = 0; i < 8; i++) {
+       for (i = 0; i < dir_MAX; i++) {
            int xend, yend;
 
            yend = y + yincr[i];
@@ -697,10 +732,9 @@ static int
 awinna(void)
 {
     int i, j;
-    ship_t *ss;
 
     for (i = 0; i < 2; ++i) {
-       ss = (i) ? cpuship : plyship;
+       ship_t *ss = (i) ? cpuship : plyship;
        for (j = 0; j < SHIPTYPES; ++j, ++ss)
            if (ss->length > ss->hits)
                break;
@@ -727,12 +761,14 @@ hitship(int x, int y)
            if (++ss->hits < ss->length)        /* still afloat? */
                return ((ship_t *) NULL);
            else {              /* sunk! */
-               int i, j;
+               int i;
+
+               if (!closepack) {
+                   int j;
 
-               if (!closepack)
                    for (j = -1; j <= 1; j++) {
-                       int bx = ss->x + j * xincr[(ss->dir + 2) % 8];
-                       int by = ss->y + j * yincr[(ss->dir + 2) % 8];
+                       int bx = ss->x + j * xincr[(ss->dir + 2) % dir_MAX];
+                       int by = ss->y + j * yincr[(ss->dir + 2) % dir_MAX];
 
                        for (i = -1; i <= ss->length; ++i) {
                            int x1, y1;
@@ -747,17 +783,18 @@ hitship(int x, int y)
                                    if (has_colors())
                                        attron(COLOR_PAIR(COLOR_GREEN));
 #endif /* A_COLOR */
-                                   (void) addch(MARK_MISS);
+                                   AddCh(MARK_MISS);
 #ifdef A_COLOR
-                                   attrset(0);
+                                   (void) attrset(0);
 #endif /* A_COLOR */
                                } else {
                                    pgoto(y1, x1);
-                                   (void) addch(SHOWSPLASH);
+                                   AddCh(SHOWSPLASH);
                                }
                            }
                        }
                    }
+               }
 
                for (i = 0; i < ss->length; ++i) {
                    int x1 = ss->x + i * xincr[ss->dir];
@@ -766,16 +803,16 @@ hitship(int x, int y)
                    hits[turn][x1][y1] = ss->symbol;
                    if (turn % 2 == PLAYER) {
                        cgoto(y1, x1);
-                       (void) addch((chtype) (ss->symbol));
+                       AddCh(ss->symbol);
                    } else {
                        pgoto(y1, x1);
 #ifdef A_COLOR
                        if (has_colors())
                            attron(COLOR_PAIR(COLOR_RED));
 #endif /* A_COLOR */
-                       (void) addch(SHOWHIT);
+                       AddCh(SHOWHIT);
 #ifdef A_COLOR
-                       attrset(0);
+                       (void) attrset(0);
 #endif /* A_COLOR */
                    }
                }
@@ -805,7 +842,7 @@ plyturn(void)
            break;
     }
     hit = IS_SHIP(board[COMPUTER][curx][cury]);
-    hits[PLAYER][curx][cury] = (hit ? MARK_HIT : MARK_MISS);
+    hits[PLAYER][curx][cury] = (char) (hit ? MARK_HIT : MARK_MISS);
     cgoto(cury, curx);
 #ifdef A_COLOR
     if (has_colors()) {
@@ -815,9 +852,9 @@ plyturn(void)
            attron(COLOR_PAIR(COLOR_GREEN));
     }
 #endif /* A_COLOR */
-    (void) addch((chtype) hits[PLAYER][curx][cury]);
+    AddCh(hits[PLAYER][curx][cury]);
 #ifdef A_COLOR
-    attrset(0);
+    (void) attrset(0);
 #endif /* A_COLOR */
 
     prompt(1, "You %s.", hit ? "scored a hit" : "missed");
@@ -839,9 +876,10 @@ plyturn(void)
            m = " You'll pick up survivors from my %s, I hope...!";
            break;
        }
-       (void) printw(m, ss->name);
+       if (m != 0) {
+           (void) printw(m, ss->name);
+       }
        (void) beep();
-       return (awinna() == -1);
     }
     return (hit);
 }
@@ -849,20 +887,20 @@ plyturn(void)
 static int
 sgetc(const char *s)
 {
-    const char *s1;
-    int ch;
-
     (void) refresh();
+
     for (;;) {
-       ch = getch();
+       int ch = getch();
+       const char *s1;
+
        if (islower(ch))
            ch = toupper(ch);
-       if (ch == CTRLC)
+       if (is_QUIT(ch))
            uninitgame(0);
        for (s1 = s; *s1 && ch != *s1; ++s1)
            continue;
        if (*s1) {
-           (void) addch((chtype) ch);
+           AddCh(ch);
            (void) refresh();
            return (ch);
        }
@@ -929,10 +967,11 @@ cpufire(int x, int y)
     bool hit, sunk;
     ship_t *ss = NULL;
 
-    hits[COMPUTER][x][y] = (hit = (board[PLAYER][x][y])) ? MARK_HIT : MARK_MISS;
-    (void) mvprintw(PROMPTLINE, 0,
-                   "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" :
-                   "miss");
+    hit = (bool) board[PLAYER][x][y];
+    hits[COMPUTER][x][y] = (hit ? MARK_HIT : MARK_MISS);
+    MvPrintw(PROMPTLINE, 0,
+            "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" :
+            "miss");
     if ((sunk = (hit && (ss = hitship(x, y)))) != 0)
        (void) printw(" I've sunk your %s", ss->name);
     (void) clrtoeol();
@@ -946,18 +985,22 @@ cpufire(int x, int y)
            attron(COLOR_PAIR(COLOR_GREEN));
     }
 #endif /* A_COLOR */
-    (void) addch((chtype) (hit ? SHOWHIT : SHOWSPLASH));
+    AddCh((hit ? SHOWHIT : SHOWSPLASH));
 #ifdef A_COLOR
-    attrset(0);
+    (void) attrset(0);
 #endif /* A_COLOR */
 
-    return ((hit ? (sunk ? S_SUNK : S_HIT) : S_MISS) ? TRUE : FALSE);
+    return hit ? (sunk ? S_SUNK : S_HIT) : S_MISS;
 }
 
 /*
  * This code implements a fairly irregular FSM, so please forgive the rampant
  * unstructuredness below. The five labels are states which need to be held
  * between computer turns.
+ *
+ * The FSM is not externally reset to RANDOM_FIRE if the player wins. Instead,
+ * the other states check for "impossible" conditions which signify a new
+ * game, then if found transition to RANDOM_FIRE.
  */
 static bool
 cputurn(void)
@@ -970,7 +1013,7 @@ cputurn(void)
 #define REVERSE_JUMP   4
 #define SECOND_PASS    5
     static int next = RANDOM_FIRE;
-    static bool used[4];
+    static bool used[5];
     static ship_t ts;
     int navail, x, y, d, n;
     int hit = S_MISS;
@@ -990,11 +1033,14 @@ cputurn(void)
        break;
 
     case RANDOM_HIT:           /* last shot was random and hit */
-       used[E / 2] = used[S / 2] = used[W / 2] = used[N / 2] = FALSE;
+       used[dir_E / 2] =
+           used[dir_S / 2] =
+           used[dir_W / 2] =
+           used[dir_N / 2] = FALSE;
        /* FALLTHROUGH */
 
     case HUNT_DIRECT:          /* last shot hit, we're looking for ship's long axis */
-       for (d = navail = 0; d < 4; d++) {
+       for (d = navail = 0; d < (dir_MAX) / 2; d++) {
            x = ts.x + xincr[d * 2];
            y = ts.y + yincr[d * 2];
            if (!used[d] && POSSIBLE(x, y))
@@ -1005,13 +1051,17 @@ cputurn(void)
        if (navail == 0)        /* no valid places for shots adjacent... */
            goto refire;        /* ...so we must random-fire */
        else {
-           for (d = 0, n = rnd(navail) + 1; n; n--)
-               while (used[d])
-                   d++;
+           n = rnd(navail) + 1;
+           for (d = 0; d < (dir_MAX) / 2 && used[d]; d++) ;
+           /* used[d] is first that == 0 */
+           for (; n > 1; n--)
+               while (d < (dir_MAX) / 2 && used[++d]) ;
+           /* used[d] is next that == 0 */
 
-           assert(d <= 4);
+           assert(d < (dir_MAX) / 2);
+           assert(used[d] == FALSE);
 
-           used[d] = FALSE;
+           used[d] = TRUE;
            x = ts.x + xincr[d * 2];
            y = ts.y + yincr[d * 2];
 
@@ -1042,7 +1092,7 @@ cputurn(void)
        break;
 
     case REVERSE_JUMP:         /* nail down the ship's other end */
-       d = ts.dir + 4;
+       d = (ts.dir + (dir_MAX) / 2) % dir_MAX;
        x = ts.x + ts.hits * xincr[d];
        y = ts.y + ts.hits * yincr[d];
        if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
@@ -1055,7 +1105,7 @@ cputurn(void)
            next = RANDOM_FIRE;
        break;
 
-    case SECOND_PASS:          /* kill squares not caught on first pass */
+    case SECOND_PASS:          /* continue shooting after reversing */
        x = ts.x + xincr[ts.dir];
        y = ts.y + yincr[ts.dir];
        if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
@@ -1069,18 +1119,15 @@ cputurn(void)
        break;
     }
 
-    /* check for continuation and/or winner */
+    /* pause between shots in salvo */
     if (salvo) {
        (void) refresh();
        (void) sleep(1);
     }
-    if (awinna() != -1)
-       return (FALSE);
-
 #ifdef DEBUG
-    (void) mvprintw(PROMPTLINE + 2, 0,
-                   "New state %d, x=%d, y=%d, d=%d",
-                   next, x, y, d);
+    MvPrintw(PROMPTLINE + 2, 0,
+            "New state %d, x=%d, y=%d, d=%d",
+            next, x, y, d);
 #endif /* DEBUG */
     return ((hit) ? TRUE : FALSE);
 }
@@ -1094,37 +1141,37 @@ playagain(void)
     for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
        for (j = 0; j < ss->length; j++) {
            cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir]);
-           (void) addch((chtype) ss->symbol);
+           AddCh(ss->symbol);
        }
 
     if (awinna())
        ++cpuwon;
     else
        ++plywon;
-    j = 18 + strlen(name);
+    j = 18 + (int) strlen(your_name);
     if (plywon >= 10)
        ++j;
     if (cpuwon >= 10)
        ++j;
-    (void) mvprintw(1, (COLWIDTH - j) / 2,
-                   "%s: %d     Computer: %d", name, plywon, cpuwon);
+    MvPrintw(1, (COLWIDTH - j) / 2,
+            "%s: %d     Computer: %d", your_name, plywon, cpuwon);
 
     prompt(2, (awinna())? "Want to be humiliated again, %s [yn]? "
-          : "Going to give me a chance for revenge, %s [yn]? ", name);
+          : "Going to give me a chance for revenge, %s [yn]? ", your_name);
     return (sgetc("YN") == 'Y');
 }
 
 static void
 do_options(int c, char *op[])
 {
-    register int i;
-
     if (c > 1) {
+       int i;
+
        for (i = 1; i < c; i++) {
            switch (op[i][0]) {
            default:
            case '?':
-               (void) fprintf(stderr, "Usage: battle [-s | -b] [-c]\n");
+               (void) fprintf(stderr, "Usage: bs [-s | -b] [-c]\n");
                (void) fprintf(stderr, "\tWhere the options are:\n");
                (void) fprintf(stderr, "\t-s : play a salvo game\n");
                (void) fprintf(stderr, "\t-b : play a blitz game\n");
@@ -1215,7 +1262,7 @@ main(int argc, char *argv[])
                    }
                }
            } else
-               while (turn ? cputurn() : plyturn())
+               while ((turn ? cputurn() : plyturn()) && awinna() == -1)
                    continue;
            turn = OTHER;
        }