]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/bs.c
ncurses 5.7 - patch 20090829
[ncurses.git] / test / bs.c
index 749faf015deeeeed49c201edb4265331d9eac491..fd0780bd413a4ffe618101f97e373c664bfc832a 100644 (file)
--- a/test/bs.c
+++ b/test/bs.c
@@ -1,3 +1,30 @@
+/****************************************************************************
+ * Copyright (c) 1998-2008,2009 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.39 2003/12/06 18:10:13 tom Exp $
+ * $Id: bs.c,v 1.48 2009/08/29 19:02:25 tom Exp $
  */
 
-#include <time.h>
-
 #include <test.priv.h>
 
+#include <time.h>
+
 #ifndef SIGIOT
 #define SIGIOT SIGABRT
 #endif
@@ -149,7 +176,8 @@ static int salvo, blitz, closepack;
 
 static RETSIGTYPE uninitgame(int sig) GCC_NORETURN;
 
-static RETSIGTYPE uninitgame(int sig GCC_UNUSED)
+static RETSIGTYPE
+uninitgame(int sig GCC_UNUSED)
 /* end the game, either normally or due to signal */
 {
     clear();
@@ -190,15 +218,11 @@ intro(void)
 
     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);
+    CATCHALL(uninitgame);
 
     if ((tmpname = getlogin()) != 0) {
        (void) strcpy(name, tmpname);
-       name[0] = toupper(name[0]);
+       name[0] = toupper(UChar(name[0]));
     } else
        (void) strcpy(name, dftname);
 
@@ -249,7 +273,7 @@ intro(void)
 
 /* VARARGS1 */
 static void
-prompt(int n, const char *f, const char *s)
+prompt(int n, NCURSES_CONST char *f, const char *s)
 /* print a message at the prompt line */
 {
     (void) move(PROMPTLINE + n, 0);
@@ -350,7 +374,7 @@ initgame(void)
        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'));
@@ -368,7 +392,7 @@ initgame(void)
        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'));
@@ -435,12 +459,13 @@ initgame(void)
        do {
            c = getch();
        } while
-           (!strchr("hjklrR", c) || c == FF);
+           (!(strchr("hjklrR", c) || c == FF));
 
        if (c == FF) {
            (void) clearok(stdscr, TRUE);
            (void) refresh();
        } else if (c == 'r') {
+           assert(ss != 0);
            prompt(1, "Random-placing your %s", ss->name);
            randomplace(PLAYER, ss);
            placeship(PLAYER, ss, TRUE);
@@ -456,6 +481,7 @@ initgame(void)
                }
            error((char *) NULL);
        } else if (strchr("hjkl8462", c)) {
+           assert(ss != 0);
            ss->x = curx;
            ss->y = cury;
 
@@ -747,7 +773,7 @@ hitship(int x, int y)
 #endif /* A_COLOR */
                                    (void) addch(MARK_MISS);
 #ifdef A_COLOR
-                                   attrset(0);
+                                   (void) attrset(0);
 #endif /* A_COLOR */
                                } else {
                                    pgoto(y1, x1);
@@ -773,7 +799,7 @@ hitship(int x, int y)
 #endif /* A_COLOR */
                        (void) addch(SHOWHIT);
 #ifdef A_COLOR
-                       attrset(0);
+                       (void) attrset(0);
 #endif /* A_COLOR */
                    }
                }
@@ -815,7 +841,7 @@ plyturn(void)
 #endif /* A_COLOR */
     (void) addch((chtype) 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,7 +865,6 @@ plyturn(void)
        }
        (void) printw(m, ss->name);
        (void) beep();
-       return (awinna() == -1);
     }
     return (hit);
 }
@@ -946,16 +971,20 @@ cpufire(int x, int y)
 #endif /* A_COLOR */
     (void) addch((chtype) (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)
@@ -1003,13 +1032,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; used[d]; d++) ;
+           /* used[d] is first that == 0 */
+           for (; n > 1; n--)
+               while (used[++d]) ;
+           /* used[d] is next that == 0 */
 
-           assert(d <= 4);
+           assert(d < 4);
+           assert(used[d] == FALSE);
 
-           used[d] = FALSE;
+           used[d] = TRUE;
            x = ts.x + xincr[d * 2];
            y = ts.y + yincr[d * 2];
 
@@ -1040,7 +1073,7 @@ cputurn(void)
        break;
 
     case REVERSE_JUMP:         /* nail down the ship's other end */
-       d = ts.dir + 4;
+       d = (ts.dir + 4) % 8;
        x = ts.x + ts.hits * xincr[d];
        y = ts.y + ts.hits * yincr[d];
        if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
@@ -1053,7 +1086,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))) {
@@ -1067,14 +1100,11 @@ 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",
@@ -1213,7 +1243,7 @@ main(int argc, char *argv[])
                    }
                }
            } else
-               while (turn ? cputurn() : plyturn())
+               while ((turn ? cputurn() : plyturn()) && awinna() == -1)
                    continue;
            turn = OTHER;
        }