X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fbs.c;h=a74f64300ad27daf8888bea0919e2df68ab2b65d;hp=4330f902d7c180f253d0db03bb4729391b0b353d;hb=acc28c6418f43cbf15187f2f1fd9a562d5d96535;hpb=2639531af0c3ca25b48e7bcb9c790fa566cc5892 diff --git a/test/bs.c b/test/bs.c index 4330f902..a74f6430 100644 --- a/test/bs.c +++ b/test/bs.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998-2006,2007 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 * @@ -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.45 2007/04/07 17:13:11 tom Exp $ + * $Id: bs.c,v 1.63 2014/08/09 22:27:12 tom Exp $ */ #include @@ -110,7 +110,7 @@ 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 */ @@ -174,9 +174,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 */ { @@ -220,11 +220,12 @@ intro(void) CATCHALL(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 = dftname; + } (void) initscr(); keypad(stdscr, TRUE); @@ -235,7 +236,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"); @@ -248,7 +249,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 */ @@ -337,7 +338,7 @@ initgame(void) ship_t *ss; (void) clear(); - (void) mvaddstr(0, 35, "BATTLESHIPS"); + MvAddStr(0, 35, "BATTLESHIPS"); (void) move(PROMPTLINE + 2, 0); announceopts(); @@ -362,10 +363,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)); @@ -374,16 +375,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)); @@ -392,33 +393,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); - - (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++) { @@ -426,10 +427,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++) @@ -440,9 +442,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'); @@ -457,13 +459,15 @@ initgame(void) } do { - c = getch(); + c = (char) getch(); } while - (!(strchr("hjklrR", c) || c == FF)); + (!(strchr("hjkl8462rR", c) || c == FF)); 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); @@ -516,18 +520,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(); @@ -545,12 +549,12 @@ getcoord(int atcpu) (void) refresh(); for (;;) { 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); } @@ -631,9 +635,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); } @@ -771,7 +775,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); @@ -797,7 +801,7 @@ hitship(int x, int y) #endif /* A_COLOR */ (void) addch(SHOWHIT); #ifdef A_COLOR - attrset(0); + (void) attrset(0); #endif /* A_COLOR */ } } @@ -827,7 +831,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()) { @@ -839,7 +843,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"); @@ -861,7 +865,9 @@ 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 (hit); @@ -950,10 +956,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(); @@ -969,7 +976,7 @@ 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; @@ -1104,9 +1111,9 @@ cputurn(void) (void) sleep(1); } #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); } @@ -1127,16 +1134,16 @@ playagain(void) ++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'); }