]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/bs.c
ncurses 6.1 - patch 20180303
[ncurses.git] / test / bs.c
index ebcabc42d3bfabd7f4b75d747e30e53e60c1bc83..31f98bb8684dc3754d8976a0bd26ff85c00411da 100644 (file)
--- a/test/bs.c
+++ b/test/bs.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2010,2012 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2016,2017 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            *
@@ -34,7 +34,7 @@
  * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
  * v2.1 with ncurses mouse support, September 1995
  *
- * $Id: bs.c,v 1.59 2012/12/16 00:20:49 tom Exp $
+ * $Id: bs.c,v 1.71 2017/10/18 23:03:07 tom Exp $
  */
 
 #include <test.priv.h>
@@ -64,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
@@ -145,6 +147,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),
@@ -174,9 +177,9 @@ 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
+static void
 uninitgame(int sig GCC_UNUSED)
 /* end the game, either normally or due to signal */
 {
@@ -185,6 +188,7 @@ uninitgame(int sig GCC_UNUSED)
     (void) reset_shell_mode();
     (void) echo();
     (void) endwin();
+    free(your_name);
     ExitProgram(sig ? EXIT_FAILURE : EXIT_SUCCESS);
 }
 
@@ -214,20 +218,19 @@ announceopts(void)
 static void
 intro(void)
 {
-    char *tmpname;
+    const char *tmpname;
 
     srand((unsigned) (time(0L) + getpid()));   /* Kick the random number generator */
 
-    CATCHALL(uninitgame);
+    InitAndCatch(initscr(), uninitgame);
 
     if ((tmpname = getlogin()) != 0 &&
        (your_name = strdup(tmpname)) != 0) {
        your_name[0] = (char) toupper(UChar(your_name[0]));
     } else {
-       your_name = dftname;
+       your_name = strdup(dftname);
     }
 
-    (void) initscr();
     keypad(stdscr, TRUE);
     (void) def_prog_mode();
     (void) nonl();
@@ -306,7 +309,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;
@@ -369,16 +372,16 @@ initgame(void)
        MvAddCh(PYBASE + i, PXBASE - 3, (chtype) (i + 'A'));
 #ifdef A_COLOR
        if (has_colors())
-           attron((attr_t) COLOR_PAIR(COLOR_BLUE));
+           attron(COLOR_PAIR(COLOR_BLUE));
 #endif /* A_COLOR */
-       (void) addch(' ');
+       AddCh(' ');
        for (j = 0; j < BWIDTH; j++)
            (void) addstr(" . ");
 #ifdef A_COLOR
        (void) attrset(0);
 #endif /* A_COLOR */
-       (void) addch(' ');
-       (void) addch((chtype) (i + 'A'));
+       AddCh(' ');
+       AddCh(i + 'A');
     }
     MvAddStr(PYBASE + BDEPTH, PXBASE - 3, numbers);
     MvAddStr(CYBASE - 2, CXBASE + 7, "Hit/Miss Board");
@@ -387,16 +390,16 @@ initgame(void)
        MvAddCh(CYBASE + i, CXBASE - 3, (chtype) (i + 'A'));
 #ifdef A_COLOR
        if (has_colors())
-           attron((attr_t) COLOR_PAIR(COLOR_BLUE));
+           attron(COLOR_PAIR(COLOR_BLUE));
 #endif /* A_COLOR */
-       (void) addch(' ');
+       AddCh(' ');
        for (j = 0; j < BWIDTH; j++)
            (void) addstr(" . ");
 #ifdef A_COLOR
        (void) attrset(0);
 #endif /* A_COLOR */
-       (void) addch(' ');
-       (void) addch((chtype) (i + 'A'));
+       AddCh(' ');
+       AddCh(i + 'A');
     }
 
     MvAddStr(CYBASE + BDEPTH, CXBASE - 3, numbers);
@@ -444,7 +447,7 @@ initgame(void)
        do {
            c = (char) getcoord(PLAYER);
        } while
-           (!strchr(docked, c));
+           (!(strchr) (docked, c));
 
        if (c == 'R')
            (void) ungetch('R');
@@ -461,9 +464,11 @@ initgame(void)
        do {
            c = (char) getch();
        } while
-           (!(strchr("hjkl8462rR", 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) {
@@ -771,15 +776,15 @@ hitship(int x, int y)
                                    cgoto(y1, x1);
 #ifdef A_COLOR
                                    if (has_colors())
-                                       attron((attr_t) COLOR_PAIR(COLOR_GREEN));
+                                       attron(COLOR_PAIR(COLOR_GREEN));
 #endif /* A_COLOR */
-                                   (void) addch(MARK_MISS);
+                                   AddCh(MARK_MISS);
 #ifdef A_COLOR
                                    (void) attrset(0);
 #endif /* A_COLOR */
                                } else {
                                    pgoto(y1, x1);
-                                   (void) addch(SHOWSPLASH);
+                                   AddCh(SHOWSPLASH);
                                }
                            }
                        }
@@ -792,14 +797,14 @@ 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((attr_t) COLOR_PAIR(COLOR_RED));
+                           attron(COLOR_PAIR(COLOR_RED));
 #endif /* A_COLOR */
-                       (void) addch(SHOWHIT);
+                       AddCh(SHOWHIT);
 #ifdef A_COLOR
                        (void) attrset(0);
 #endif /* A_COLOR */
@@ -836,12 +841,12 @@ plyturn(void)
 #ifdef A_COLOR
     if (has_colors()) {
        if (hit)
-           attron((attr_t) COLOR_PAIR(COLOR_RED));
+           attron(COLOR_PAIR(COLOR_RED));
        else
-           attron((attr_t) COLOR_PAIR(COLOR_GREEN));
+           attron(COLOR_PAIR(COLOR_GREEN));
     }
 #endif /* A_COLOR */
-    (void) addch((chtype) hits[PLAYER][curx][cury]);
+    AddCh(hits[PLAYER][curx][cury]);
 #ifdef A_COLOR
     (void) attrset(0);
 #endif /* A_COLOR */
@@ -884,12 +889,12 @@ sgetc(const char *s)
        ch = getch();
        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);
        }
@@ -956,7 +961,7 @@ cpufire(int x, int y)
     bool hit, sunk;
     ship_t *ss = NULL;
 
-    hit = board[PLAYER][x][y];
+    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" :
@@ -969,12 +974,12 @@ cpufire(int x, int y)
 #ifdef A_COLOR
     if (has_colors()) {
        if (hit)
-           attron((attr_t) COLOR_PAIR(COLOR_RED));
+           attron(COLOR_PAIR(COLOR_RED));
        else
-           attron((attr_t) COLOR_PAIR(COLOR_GREEN));
+           attron(COLOR_PAIR(COLOR_GREEN));
     }
 #endif /* A_COLOR */
-    (void) addch((chtype) (hit ? SHOWHIT : SHOWSPLASH));
+    AddCh((hit ? SHOWHIT : SHOWSPLASH));
 #ifdef A_COLOR
     (void) attrset(0);
 #endif /* A_COLOR */
@@ -1002,7 +1007,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;
@@ -1038,10 +1043,10 @@ cputurn(void)
            goto refire;        /* ...so we must random-fire */
        else {
            n = rnd(navail) + 1;
-           for (d = 0; used[d]; d++) ;
+           for (d = 0; d < 4 && used[d]; d++) ;
            /* used[d] is first that == 0 */
            for (; n > 1; n--)
-               while (used[++d]) ;
+               while (d < 4 && used[++d]) ;
            /* used[d] is next that == 0 */
 
            assert(d < 4);
@@ -1127,7 +1132,7 @@ 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())
@@ -1157,7 +1162,7 @@ do_options(int c, char *op[])
            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");