]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/bs.c
ncurses 6.0
[ncurses.git] / test / bs.c
index ae893712ea643757c4538fef77c0eef8a64e0ef4..a74f64300ad27daf8888bea0919e2df68ab2b65d 100644 (file)
--- a/test/bs.c
+++ b/test/bs.c
@@ -1,5 +1,5 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2013,2014 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            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -34,7 +34,7 @@
  * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
  * v2.1 with ncurses mouse support, September 1995
  *
  * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
  * v2.1 with ncurses mouse support, September 1995
  *
- * $Id: bs.c,v 1.51 2010/05/01 19:12:26 tom Exp $
+ * $Id: bs.c,v 1.63 2014/08/09 22:27:12 tom Exp $
  */
 
 #include <test.priv.h>
  */
 
 #include <test.priv.h>
@@ -110,7 +110,7 @@ static char sub[] = "Submarine";
 static char destroy[] = "Destroyer";
 static char ptboat[] = "PT Boat";
 
 static char destroy[] = "Destroyer";
 static char ptboat[] = "PT Boat";
 
-static char name[40];
+static char *your_name;
 static char dftname[] = "stranger";
 
 /* direction constants */
 static char dftname[] = "stranger";
 
 /* direction constants */
@@ -174,9 +174,9 @@ static int salvo, blitz, closepack;
 
 #define        PR      (void)addstr
 
 
 #define        PR      (void)addstr
 
-static RETSIGTYPE uninitgame(int sig) GCC_NORETURN;
+static void uninitgame(int sig) GCC_NORETURN;
 
 
-static RETSIGTYPE
+static void
 uninitgame(int sig GCC_UNUSED)
 /* end the game, either normally or due to signal */
 {
 uninitgame(int sig GCC_UNUSED)
 /* end the game, either normally or due to signal */
 {
@@ -220,11 +220,12 @@ intro(void)
 
     CATCHALL(uninitgame);
 
 
     CATCHALL(uninitgame);
 
-    if ((tmpname = getlogin()) != 0) {
-       (void) strcpy(name, tmpname);
-       name[0] = (char) toupper(UChar(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 = dftname;
+    }
 
     (void) initscr();
     keypad(stdscr, TRUE);
 
     (void) initscr();
     keypad(stdscr, TRUE);
@@ -401,17 +402,17 @@ initgame(void)
     MvAddStr(CYBASE + BDEPTH, CXBASE - 3, numbers);
 
     MvPrintw(HYBASE, HXBASE,
     MvAddStr(CYBASE + BDEPTH, CXBASE - 3, numbers);
 
     MvPrintw(HYBASE, HXBASE,
-                   "To position your ships: move the cursor to a spot, then");
+            "To position your ships: move the cursor to a spot, then");
     MvPrintw(HYBASE + 1, HXBASE,
     MvPrintw(HYBASE + 1, HXBASE,
-                   "type the first letter of a ship type to select it, then");
+            "type the first letter of a ship type to select it, then");
     MvPrintw(HYBASE + 2, HXBASE,
     MvPrintw(HYBASE + 2, HXBASE,
-                   "type a direction ([hjkl] or [4862]), indicating how the");
+            "type a direction ([hjkl] or [4862]), indicating how the");
     MvPrintw(HYBASE + 3, HXBASE,
     MvPrintw(HYBASE + 3, HXBASE,
-                   "ship should be pointed. You may also type a ship letter");
+            "ship should be pointed. You may also type a ship letter");
     MvPrintw(HYBASE + 4, HXBASE,
     MvPrintw(HYBASE + 4, HXBASE,
-                   "followed by `r' to position it randomly, or type `R' to");
+            "followed by `r' to position it randomly, or type `R' to");
     MvPrintw(HYBASE + 5, HXBASE,
     MvPrintw(HYBASE + 5, HXBASE,
-                   "place all remaining ships randomly.");
+            "place all remaining ships randomly.");
 
     MvAddStr(MYBASE, MXBASE, "Aiming keys:");
     MvAddStr(SYBASE, SXBASE, "y k u    7 8 9");
 
     MvAddStr(MYBASE, MXBASE, "Aiming keys:");
     MvAddStr(SYBASE, SXBASE, "y k u    7 8 9");
@@ -426,10 +427,11 @@ initgame(void)
        placeship(COMPUTER, ss, FALSE);
     }
 
        placeship(COMPUTER, ss, FALSE);
     }
 
-    ss = (ship_t *) NULL;
     do {
        char c, docked[SHIPTYPES + 2], *cp = docked;
 
     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++)
        /* figure which ships still wait to be placed */
        *cp++ = 'R';
        for (i = 0; i < SHIPTYPES; i++)
@@ -442,7 +444,7 @@ initgame(void)
        do {
            c = (char) getcoord(PLAYER);
        } while
        do {
            c = (char) getcoord(PLAYER);
        } while
-           (!strchr(docked, c));
+           (!(strchr) (docked, c));
 
        if (c == 'R')
            (void) ungetch('R');
 
        if (c == 'R')
            (void) ungetch('R');
@@ -459,13 +461,14 @@ initgame(void)
        do {
            c = (char) getch();
        } while
        do {
            c = (char) getch();
        } while
-           (!(strchr("hjklrR", c) || c == FF));
+           (!(strchr("hjkl8462rR", c) || c == FF));
 
        if (c == FF) {
            (void) clearok(stdscr, TRUE);
            (void) refresh();
 
        if (c == FF) {
            (void) clearok(stdscr, TRUE);
            (void) refresh();
+       } else if (ss == 0) {
+           beep();             /* simple to verify, unlikely to happen */
        } else if (c == 'r') {
        } else if (c == 'r') {
-           assert(ss != 0);
            prompt(1, "Random-placing your %s", ss->name);
            randomplace(PLAYER, ss);
            placeship(PLAYER, ss, TRUE);
            prompt(1, "Random-placing your %s", ss->name);
            randomplace(PLAYER, ss);
            placeship(PLAYER, ss, TRUE);
@@ -481,7 +484,6 @@ initgame(void)
                }
            error((char *) NULL);
        } else if (strchr("hjkl8462", c)) {
                }
            error((char *) NULL);
        } else if (strchr("hjkl8462", c)) {
-           assert(ss != 0);
            ss->x = curx;
            ss->y = cury;
 
            ss->x = curx;
            ss->y = cury;
 
@@ -519,17 +521,17 @@ initgame(void)
     turn = rnd(2);
 
     MvPrintw(HYBASE, HXBASE,
     turn = rnd(2);
 
     MvPrintw(HYBASE, HXBASE,
-                   "To fire, move the cursor to your chosen aiming point   ");
+            "To fire, move the cursor to your chosen aiming point   ");
     MvPrintw(HYBASE + 1, HXBASE,
     MvPrintw(HYBASE + 1, HXBASE,
-                   "and strike any key other than a motion key.            ");
+            "and strike any key other than a motion key.            ");
     MvPrintw(HYBASE + 2, HXBASE,
     MvPrintw(HYBASE + 2, HXBASE,
-                   "                                                       ");
+            "                                                       ");
     MvPrintw(HYBASE + 3, HXBASE,
     MvPrintw(HYBASE + 3, HXBASE,
-                   "                                                       ");
+            "                                                       ");
     MvPrintw(HYBASE + 4, HXBASE,
     MvPrintw(HYBASE + 4, HXBASE,
-                   "                                                       ");
+            "                                                       ");
     MvPrintw(HYBASE + 5, HXBASE,
     MvPrintw(HYBASE + 5, HXBASE,
-                   "                                                       ");
+            "                                                       ");
 
     (void) prompt(0, "Press any key to start...", "");
     (void) getch();
 
     (void) prompt(0, "Press any key to start...", "");
     (void) getch();
@@ -548,11 +550,11 @@ getcoord(int atcpu)
     for (;;) {
        if (atcpu) {
            MvPrintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
     for (;;) {
        if (atcpu) {
            MvPrintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
-                           curx, 'A' + cury);
+                    curx, 'A' + cury);
            cgoto(cury, curx);
        } else {
            MvPrintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
            cgoto(cury, curx);
        } else {
            MvPrintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
-                           curx, 'A' + cury);
+                    curx, 'A' + cury);
            pgoto(cury, curx);
        }
 
            pgoto(cury, curx);
        }
 
@@ -863,7 +865,9 @@ plyturn(void)
            m = " You'll pick up survivors from my %s, I hope...!";
            break;
        }
            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 (hit);
        (void) beep();
     }
     return (hit);
@@ -952,11 +956,11 @@ cpufire(int x, int y)
     bool hit, sunk;
     ship_t *ss = NULL;
 
     bool hit, sunk;
     ship_t *ss = NULL;
 
-    hit = board[PLAYER][x][y] ? MARK_HIT : MARK_MISS;
-    hits[COMPUTER][x][y] = (char) hit;
+    hit = (bool) board[PLAYER][x][y];
+    hits[COMPUTER][x][y] = (hit ? MARK_HIT : MARK_MISS);
     MvPrintw(PROMPTLINE, 0,
     MvPrintw(PROMPTLINE, 0,
-                   "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" :
-                   "miss");
+            "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();
     if ((sunk = (hit && (ss = hitship(x, y)))) != 0)
        (void) printw(" I've sunk your %s", ss->name);
     (void) clrtoeol();
@@ -1108,8 +1112,8 @@ cputurn(void)
     }
 #ifdef DEBUG
     MvPrintw(PROMPTLINE + 2, 0,
     }
 #ifdef DEBUG
     MvPrintw(PROMPTLINE + 2, 0,
-                   "New state %d, x=%d, y=%d, d=%d",
-                   next, x, y, d);
+            "New state %d, x=%d, y=%d, d=%d",
+            next, x, y, d);
 #endif /* DEBUG */
     return ((hit) ? TRUE : FALSE);
 }
 #endif /* DEBUG */
     return ((hit) ? TRUE : FALSE);
 }
@@ -1130,16 +1134,16 @@ playagain(void)
        ++cpuwon;
     else
        ++plywon;
        ++cpuwon;
     else
        ++plywon;
-    j = 18 + (int) strlen(name);
+    j = 18 + (int) strlen(your_name);
     if (plywon >= 10)
        ++j;
     if (cpuwon >= 10)
        ++j;
     MvPrintw(1, (COLWIDTH - j) / 2,
     if (plywon >= 10)
        ++j;
     if (cpuwon >= 10)
        ++j;
     MvPrintw(1, (COLWIDTH - j) / 2,
-                   "%s: %d     Computer: %d", name, plywon, cpuwon);
+            "%s: %d     Computer: %d", your_name, plywon, cpuwon);
 
     prompt(2, (awinna())? "Want to be humiliated again, %s [yn]? "
 
     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');
 }
 
     return (sgetc("YN") == 'Y');
 }