From 48d1d04157f7ddd73a68530544b582a0450f993b Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Fri, 14 Aug 1998 03:53:19 +0000 Subject: [PATCH] Code cleanup. --- bs.c | 111 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 44 deletions(-) diff --git a/bs.c b/bs.c index fc32f1b..1da99d5 100644 --- a/bs.c +++ b/bs.c @@ -6,17 +6,18 @@ * v1.2 with color support and minor portability fixes, November 1990 * v2.0 featuring strict ANSI/POSIX conformance, November 1993. * v2.1 with ncurses mouse support, September 1995 + * v2.2 code cleanup. + * + * $Id: bs.c,v 1.22 1997/12/20 15:11:53 tom Exp $ */ -#define _POSIX_SOURCE #include #include #include -#include -#include #include #include #include +#include #ifndef SIGIOT #define SIGIOT SIGABRT @@ -31,6 +32,9 @@ #define strchr index #endif /* !A_UNDERLINE */ +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 + static int getcoord(int); /* @@ -57,7 +61,7 @@ static int getcoord(int); /* display symbols */ #define SHOWHIT '*' #define SHOWSPLASH ' ' -#define IS_SHIP(c) isupper(c) +#define IS_SHIP(c) (isupper(c) ? TRUE : FALSE) /* how to position us on player board */ #define PYBASE 3 @@ -117,19 +121,19 @@ static int cury = (BDEPTH / 2); typedef struct { - char *name; /* name of the ship type */ - unsigned hits; /* how many times has this ship been hit? */ - char symbol; /* symbol for game purposes */ - char length; /* length of ship */ - char x, y; /* coordinates of ship start point */ - unsigned char dir; /* direction of `bow' */ - bool placed; /* has it been placed on the board? */ + char *name; /* name of the ship type */ + int hits; /* how many times has this ship been hit? */ + char symbol; /* symbol for game purposes */ + int length; /* length of ship */ + char x, y; /* coordinates of ship start point */ + unsigned char dir; /* direction of `bow' */ + bool placed; /* has it been placed on the board? */ } ship_t; static bool checkplace(int b, ship_t *ss, int vis); -ship_t plyship[SHIPTYPES] = +static ship_t plyship[SHIPTYPES] = { { carrier, 0, 'A', 5}, { battle, 0, 'B', 4}, @@ -138,7 +142,7 @@ ship_t plyship[SHIPTYPES] = { ptboat, 0, 'P', 2}, }; -ship_t cpuship[SHIPTYPES] = +static ship_t cpuship[SHIPTYPES] = { { carrier, 0, 'A', 5}, { battle, 0, 'B', 4}, @@ -148,7 +152,8 @@ ship_t cpuship[SHIPTYPES] = }; /* "Hits" board, and main board. */ -static char hits[2][BWIDTH][BDEPTH], board[2][BWIDTH][BDEPTH]; +static char hits[2][BWIDTH][BDEPTH]; +static char board[2][BWIDTH][BDEPTH]; static int turn; /* 0=player, 1=computer */ static int plywon=0, cpuwon=0; /* How many games has each won? */ @@ -157,7 +162,7 @@ static int salvo, blitz, closepack; #define PR (void)addstr -static void uninitgame(int sig) +static void uninitgame(int sig GCC_UNUSED) /* end the game, either normally or due to signal */ { clear(); @@ -165,7 +170,7 @@ static void uninitgame(int sig) (void)resetterm(); (void)echo(); (void)endwin(); - exit(sig); + exit(EXIT_FAILURE); } static void announceopts(void) @@ -194,7 +199,6 @@ static void announceopts(void) static void intro(void) { - extern char *getlogin(void); char *tmpname; srand((unsigned)(time(0L)+getpid())); /* Kick the random number generator */ @@ -260,7 +264,7 @@ static void intro(void) } /* VARARGS1 */ -static void prompt(int n, char *f, char *s) +static void prompt(int n, NCURSES_CONST char *f, const char *s) /* print a message at the prompt line */ { (void) move(PROMPTLINE + n, 0); @@ -269,7 +273,7 @@ static void prompt(int n, char *f, char *s) (void) refresh(); } -static void error(char *s) +static void error(NCURSES_CONST char *s) { (void) move(PROMPTLINE + 2, 0); (void) clrtoeol(); @@ -333,9 +337,20 @@ static void initgame(void) for (i = 0; i < SHIPTYPES; i++) { ss = cpuship + i; - ss->x = ss->y = ss->dir = ss->hits = ss->placed = 0; + + ss->x = + ss->y = + ss->dir = + ss->hits = 0; + ss->placed = FALSE; + ss = plyship + i; - ss->x = ss->y = ss->dir = ss->hits = ss->placed = 0; + + ss->x = + ss->y = + ss->dir = + ss->hits = 0; + ss->placed = FALSE; } /* draw empty boards */ @@ -604,10 +619,14 @@ static int getcoord(int atcpu) return(' '); } else + { beep(); + continue; + } } - break; + /* no fall through */ #endif /* NCURSES_MOUSE_VERSION */ + default: if (atcpu) (void) mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, " "); @@ -621,13 +640,13 @@ static int getcoord(int atcpu) } } -static int collidecheck(int b, int y, int x) +static bool collidecheck(int b, int y, int x) /* is this location on the selected zboard adjacent to a ship? */ { - int collide; + bool collide; /* anything on the square */ - if ((collide = IS_SHIP(board[b][x][y])) != 0) + if ((collide = IS_SHIP(board[b][x][y])) != FALSE) return(collide); /* anything on the neighbors */ @@ -641,8 +660,11 @@ static int collidecheck(int b, int y, int x) yend = y + yincr[i]; xend = x + xincr[i]; - if (ONBOARD(xend, yend)) - collide += IS_SHIP(board[b][xend][yend]); + if (ONBOARD(xend, yend) + && IS_SHIP(board[b][xend][yend])) { + collide = TRUE; + break; + } } } return(collide); @@ -670,7 +692,7 @@ static bool checkplace(int b, ship_t *ss, int vis) error("Figure I won't find it if you put it there?"); break; } - return(0); + return(FALSE); } for(l = 0; l < ss->length; ++l) @@ -722,7 +744,7 @@ static ship_t *hitship(int x, int y) getyx(stdscr, oldy, oldx); sb = (turn) ? plyship : cpuship; - if(!(sym = board[OTHER][x][y])) + if((sym = board[OTHER][x][y]) == 0) return((ship_t *)NULL); for(ss = sb; ss < sb + SHIPTYPES; ++ss) if(ss->symbol == sym) @@ -789,7 +811,7 @@ static int plyturn(void) { ship_t *ss; bool hit; - char *m = NULL; + NCURSES_CONST char *m = NULL; prompt(1, "Where do you want to shoot? ", ""); for (;;) @@ -804,7 +826,7 @@ static int plyturn(void) break; } hit = IS_SHIP(board[COMPUTER][curx][cury]); - hits[PLAYER][curx][cury] = hit ? MARK_HIT : MARK_MISS; + hits[PLAYER][curx][cury] = (hit ? MARK_HIT : MARK_MISS); cgoto(cury, curx); #ifdef A_COLOR if (has_colors()) @@ -843,12 +865,12 @@ static int plyturn(void) (void)beep(); return(awinna() == -1); } - return(hit); + return (hit); } -static int sgetc(char *s) +static int sgetc(const char *s) { - char *s1; + const char *s1; int ch; (void)refresh(); @@ -921,7 +943,7 @@ static void randomfire(int *px, int *py) else { error("No moves possible?? Help!"); - exit(1); + exit(EXIT_FAILURE); /*NOTREACHED*/ } } @@ -930,7 +952,7 @@ static void randomfire(int *px, int *py) #define S_HIT 1 #define S_SUNK -1 -static bool cpufire(int x, int y) +static int cpufire(int x, int y) /* fire away at given location */ { bool hit, sunk; @@ -939,7 +961,7 @@ static bool cpufire(int x, int y) 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"); - if ((sunk = (hit && (ss = hitship(x, y))))) + if ((sunk = (hit && (ss = hitship(x, y)))) != 0) (void) printw(" I've sunk your %s", ss->name); (void)clrtoeol(); @@ -956,7 +978,7 @@ static bool cpufire(int x, int y) attrset(0); #endif /* A_COLOR */ - return(hit ? (sunk ? S_SUNK : S_HIT) : S_MISS); + return ((hit ? (sunk ? S_SUNK : S_HIT) : S_MISS) ? TRUE : FALSE); } /* @@ -976,7 +998,8 @@ static bool cputurn(void) static int next = RANDOM_FIRE; static bool used[4]; static ship_t ts; - int navail, x, y, d, n, hit = S_MISS; + int navail, x, y, d, n; + int hit = S_MISS; switch(next) { @@ -1085,7 +1108,7 @@ static bool cputurn(void) "New state %d, x=%d, y=%d, d=%d", next, x, y, d); #endif /* DEBUG */ - return(hit); + return ((hit) ? TRUE : FALSE); } static @@ -1135,7 +1158,7 @@ static void do_options(int c, char *op[]) (void) fprintf(stderr, "\t-s : play a salvo game\n"); (void) fprintf(stderr, "\t-b : play a blitz game\n"); (void) fprintf(stderr, "\t-c : ships may be adjacent\n"); - exit(1); + exit(EXIT_FAILURE); break; case '-': switch(op[i][1]) @@ -1146,7 +1169,7 @@ static void do_options(int c, char *op[]) { (void) fprintf(stderr, "Bad Arg: -b and -s are mutually exclusive\n"); - exit(1); + exit(EXIT_FAILURE); } break; case 's': @@ -1155,7 +1178,7 @@ static void do_options(int c, char *op[]) { (void) fprintf(stderr, "Bad Arg: -s and -b are mutually exclusive\n"); - exit(1); + exit(EXIT_FAILURE); } break; case 'c': @@ -1164,7 +1187,7 @@ static void do_options(int c, char *op[]) default: (void) fprintf(stderr, "Bad arg: type \"%s ?\" for usage message\n", op[0]); - exit(1); + exit(EXIT_FAILURE); } } } -- 2.43.0