X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fbs.c;h=a954d2d2ade23630e2adcaec3ea3f4f5bdf9d77b;hp=ae893712ea643757c4538fef77c0eef8a64e0ef4;hb=12b49d3c56a6130feb2d39fbe2d6c1bc0838f0fa;hpb=92e187a3459ab7ce1613a3684ca6642447c73620 diff --git a/test/bs.c b/test/bs.c index ae893712..a954d2d2 100644 --- a/test/bs.c +++ b/test/bs.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998-2009,2010 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.51 2010/05/01 19:12:26 tom Exp $ + * $Id: bs.c,v 1.70 2017/09/30 15:40:39 tom Exp $ */ #include @@ -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 @@ -110,7 +112,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 */ @@ -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,19 +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) { - (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); (void) def_prog_mode(); (void) nonl(); @@ -305,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; @@ -370,14 +374,14 @@ initgame(void) if (has_colors()) 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"); @@ -388,30 +392,30 @@ initgame(void) if (has_colors()) 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); 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, - "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, - "type a direction ([hjkl] or [4862]), indicating how the"); + "type a direction ([hjkl] or [4862]), indicating how the"); 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, - "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, - "place all remaining ships randomly."); + "place all remaining ships randomly."); MvAddStr(MYBASE, MXBASE, "Aiming keys:"); MvAddStr(SYBASE, SXBASE, "y k u 7 8 9"); @@ -426,10 +430,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 +447,7 @@ initgame(void) do { c = (char) getcoord(PLAYER); } while - (!strchr(docked, c)); + (!(strchr) (docked, c)); if (c == 'R') (void) ungetch('R'); @@ -459,13 +464,16 @@ initgame(void) do { c = (char) getch(); } while - (!(strchr("hjklrR", 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) { + 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 +489,6 @@ initgame(void) } error((char *) NULL); } else if (strchr("hjkl8462", c)) { - assert(ss != 0); ss->x = curx; ss->y = cury; @@ -519,17 +526,17 @@ initgame(void) 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, - "and strike any key other than a motion key. "); + "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(); @@ -548,11 +555,11 @@ getcoord(int atcpu) 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)", - curx, 'A' + cury); + curx, 'A' + cury); pgoto(cury, curx); } @@ -771,13 +778,13 @@ hitship(int x, int y) if (has_colors()) 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 +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(COLOR_PAIR(COLOR_RED)); #endif /* A_COLOR */ - (void) addch(SHOWHIT); + AddCh(SHOWHIT); #ifdef A_COLOR (void) attrset(0); #endif /* A_COLOR */ @@ -839,7 +846,7 @@ plyturn(void) 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 +870,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); @@ -880,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); } @@ -952,11 +961,11 @@ 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"); + "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(); @@ -970,7 +979,7 @@ cpufire(int x, int y) 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 +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; @@ -1034,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); @@ -1108,8 +1117,8 @@ cputurn(void) } #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); } @@ -1123,23 +1132,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'); }