X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fbs.c;h=9a43e6233bd2240ccce91967fe379d2f2429a830;hp=53a4df7637c9d6f247c99e7a501a904eddd87881;hb=f486c68b1efe3bab5739c3f464fde6685a52bee5;hpb=98d8891f42d8acac5c2ade39d163f386057a22e4 diff --git a/test/bs.c b/test/bs.c index 53a4df76..9a43e623 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-2012,2013 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.54 2012/11/18 00:57:48 tom Exp $ + * $Id: bs.c,v 1.60 2013/02/16 19:54:37 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 */ @@ -220,11 +220,12 @@ intro(void) 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 = dftname; + } (void) initscr(); keypad(stdscr, TRUE); @@ -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++) @@ -459,13 +461,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 +484,6 @@ initgame(void) } error((char *) NULL); } else if (strchr("hjkl8462", c)) { - assert(ss != 0); ss->x = curx; ss->y = cury; @@ -954,8 +956,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"); @@ -1132,16 +1134,16 @@ playagain(void) ++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'); }