X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fbs.c;h=ae893712ea643757c4538fef77c0eef8a64e0ef4;hp=5b9f6ecdc49150e3a0716e5fc4daabe9c2e40fdb;hb=b35ecb17e0406b48556609207aa52e9fee15cec6;hpb=55ccd2b959766810cf7db8d1c4462f338ce0afc8 diff --git a/test/bs.c b/test/bs.c index 5b9f6ecd..ae893712 100644 --- a/test/bs.c +++ b/test/bs.c @@ -1,3 +1,30 @@ +/**************************************************************************** + * Copyright (c) 1998-2009,2010 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 @@ -7,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.41 2005/05/28 21:38:24 tom Exp $ + * $Id: bs.c,v 1.51 2010/05/01 19:12:26 tom Exp $ */ #include @@ -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] = (char) toupper(UChar(name[0])); } else (void) strcpy(name, dftname); @@ -211,7 +235,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"); @@ -224,7 +248,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 */ @@ -313,7 +337,7 @@ initgame(void) ship_t *ss; (void) clear(); - (void) mvaddstr(0, 35, "BATTLESHIPS"); + MvAddStr(0, 35, "BATTLESHIPS"); (void) move(PROMPTLINE + 2, 0); announceopts(); @@ -338,10 +362,10 @@ 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)); @@ -350,16 +374,16 @@ 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')); } - (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)); @@ -368,33 +392,33 @@ 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')); } - (void) mvaddstr(CYBASE + BDEPTH, CXBASE - 3, numbers); + MvAddStr(CYBASE + BDEPTH, CXBASE - 3, numbers); - (void) mvprintw(HYBASE, HXBASE, + MvPrintw(HYBASE, HXBASE, "To position your ships: move the cursor to a spot, then"); - (void) mvprintw(HYBASE + 1, HXBASE, + MvPrintw(HYBASE + 1, HXBASE, "type the first letter of a ship type to select it, then"); - (void) mvprintw(HYBASE + 2, HXBASE, + MvPrintw(HYBASE + 2, HXBASE, "type a direction ([hjkl] or [4862]), indicating how the"); - (void) mvprintw(HYBASE + 3, HXBASE, + MvPrintw(HYBASE + 3, HXBASE, "ship should be pointed. You may also type a ship letter"); - (void) mvprintw(HYBASE + 4, HXBASE, + MvPrintw(HYBASE + 4, HXBASE, "followed by `r' to position it randomly, or type `R' to"); - (void) mvprintw(HYBASE + 5, HXBASE, + 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(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++) { @@ -416,7 +440,7 @@ 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)); @@ -433,14 +457,15 @@ initgame(void) } do { - c = getch(); + c = (char) 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; @@ -492,17 +518,17 @@ initgame(void) turn = rnd(2); - (void) mvprintw(HYBASE, HXBASE, + MvPrintw(HYBASE, HXBASE, "To fire, move the cursor to your chosen aiming point "); - (void) mvprintw(HYBASE + 1, HXBASE, + MvPrintw(HYBASE + 1, HXBASE, "and strike any key other than a motion key. "); - (void) mvprintw(HYBASE + 2, HXBASE, + MvPrintw(HYBASE + 2, HXBASE, " "); - (void) mvprintw(HYBASE + 3, HXBASE, + MvPrintw(HYBASE + 3, HXBASE, " "); - (void) mvprintw(HYBASE + 4, HXBASE, + MvPrintw(HYBASE + 4, HXBASE, " "); - (void) mvprintw(HYBASE + 5, HXBASE, + MvPrintw(HYBASE + 5, HXBASE, " "); (void) prompt(0, "Press any key to start...", ""); @@ -521,11 +547,11 @@ getcoord(int atcpu) (void) refresh(); for (;;) { if (atcpu) { - (void) mvprintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)", + MvPrintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)", curx, 'A' + cury); cgoto(cury, curx); } else { - (void) mvprintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)", + MvPrintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)", curx, 'A' + cury); pgoto(cury, curx); } @@ -607,9 +633,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); } @@ -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 */ } } @@ -803,7 +829,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,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); } @@ -927,8 +952,9 @@ 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, + hit = board[PLAYER][x][y] ? MARK_HIT : MARK_MISS; + hits[COMPUTER][x][y] = (char) hit; + MvPrintw(PROMPTLINE, 0, "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" : "miss"); if ((sunk = (hit && (ss = hitship(x, y)))) != 0) @@ -946,16 +972,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 +1033,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 +1074,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 +1087,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,16 +1101,13 @@ 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, + MvPrintw(PROMPTLINE + 2, 0, "New state %d, x=%d, y=%d, d=%d", next, x, y, d); #endif /* DEBUG */ @@ -1099,12 +1130,12 @@ playagain(void) ++cpuwon; else ++plywon; - j = 18 + strlen(name); + j = 18 + (int) strlen(name); if (plywon >= 10) ++j; if (cpuwon >= 10) ++j; - (void) mvprintw(1, (COLWIDTH - j) / 2, + MvPrintw(1, (COLWIDTH - j) / 2, "%s: %d Computer: %d", name, plywon, cpuwon); prompt(2, (awinna())? "Want to be humiliated again, %s [yn]? " @@ -1213,7 +1244,7 @@ main(int argc, char *argv[]) } } } else - while (turn ? cputurn() : plyturn()) + while ((turn ? cputurn() : plyturn()) && awinna() == -1) continue; turn = OTHER; }