X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fbs.c;h=7f2984742c7136073ebf4a23ab017be2290955c1;hp=12df0934a58d6abef9885ae7dab0b4bfb2518866;hb=07e31b3b587a07281ff7c71e5c13248a31048257;hpb=b6d0d9ad9e372e856f01a4c283cf784a15993903 diff --git a/test/bs.c b/test/bs.c index 12df0934..7f298474 100644 --- 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.53 2012/06/09 20:30:32 tom Exp $ + * $Id: bs.c,v 1.67 2017/05/14 00:27:05 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 */ { @@ -185,6 +185,7 @@ uninitgame(int sig GCC_UNUSED) (void) reset_shell_mode(); (void) echo(); (void) endwin(); + free(your_name); ExitProgram(sig ? EXIT_FAILURE : EXIT_SUCCESS); } @@ -214,17 +215,18 @@ announceopts(void) static void intro(void) { - char *tmpname; + const char *tmpname; srand((unsigned) (time(0L) + getpid())); /* Kick the random number generator */ 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 = strdup(dftname); + } (void) initscr(); keypad(stdscr, TRUE); @@ -305,7 +307,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; @@ -368,16 +370,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"); @@ -386,16 +388,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); @@ -426,10 +428,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++) @@ -442,7 +445,7 @@ initgame(void) do { c = (char) getcoord(PLAYER); } while - (!strchr(docked, c)); + (!(strchr) (docked, c)); if (c == 'R') (void) ungetch('R'); @@ -459,13 +462,14 @@ initgame(void) do { 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') { - assert(ss != 0); prompt(1, "Random-placing your %s", ss->name); randomplace(PLAYER, ss); placeship(PLAYER, ss, TRUE); @@ -481,7 +485,6 @@ initgame(void) } error((char *) NULL); } else if (strchr("hjkl8462", c)) { - assert(ss != 0); ss->x = curx; ss->y = cury; @@ -769,15 +772,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); } } } @@ -790,14 +793,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 */ @@ -834,12 +837,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 */ @@ -863,7 +866,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); @@ -885,7 +890,7 @@ sgetc(const char *s) for (s1 = s; *s1 && ch != *s1; ++s1) continue; if (*s1) { - (void) addch((chtype) ch); + AddCh(ch); (void) refresh(); return (ch); } @@ -952,8 +957,8 @@ cpufire(int x, int y) 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, "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" : "miss"); @@ -965,12 +970,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 */ @@ -998,7 +1003,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; @@ -1034,10 +1039,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); @@ -1123,23 +1128,23 @@ 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()) ++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, - "%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]? " - : "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'); }