* 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 <curses.h>
#include <signal.h>
#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <time.h>
+#include <unistd.h>
#ifndef SIGIOT
#define SIGIOT SIGABRT
#define strchr index
#endif /* !A_UNDERLINE */
+#define EXIT_SUCCESS 0
+#define EXIT_FAILURE 1
+
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
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},
{ ptboat, 0, 'P', 2},
};
-ship_t cpuship[SHIPTYPES] =
+static ship_t cpuship[SHIPTYPES] =
{
{ carrier, 0, 'A', 5},
{ battle, 0, 'B', 4},
};
/* "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? */
#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();
(void)resetterm();
(void)echo();
(void)endwin();
- exit(sig);
+ exit(EXIT_FAILURE);
}
static void announceopts(void)
static void intro(void)
{
- extern char *getlogin(void);
char *tmpname;
srand((unsigned)(time(0L)+getpid())); /* Kick the random number generator */
}
/* 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);
(void) refresh();
}
-static void error(char *s)
+static void error(NCURSES_CONST char *s)
{
(void) move(PROMPTLINE + 2, 0);
(void) clrtoeol();
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 */
return(' ');
}
else
+ {
beep();
+ continue;
+ }
}
- break;
+ /* no fall through */
#endif /* NCURSES_MOUSE_VERSION */
+
default:
if (atcpu)
(void) mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, " ");
}
}
-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 */
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);
error("Figure I won't find it if you put it there?");
break;
}
- return(0);
+ return(FALSE);
}
for(l = 0; l < ss->length; ++l)
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)
{
ship_t *ss;
bool hit;
- char *m = NULL;
+ NCURSES_CONST char *m = NULL;
prompt(1, "Where do you want to shoot? ", "");
for (;;)
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())
(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();
else
{
error("No moves possible?? Help!");
- exit(1);
+ exit(EXIT_FAILURE);
/*NOTREACHED*/
}
}
#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;
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();
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);
}
/*
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)
{
"New state %d, x=%d, y=%d, d=%d",
next, x, y, d);
#endif /* DEBUG */
- return(hit);
+ return ((hit) ? TRUE : FALSE);
}
static
(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])
{
(void) fprintf(stderr,
"Bad Arg: -b and -s are mutually exclusive\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
break;
case 's':
{
(void) fprintf(stderr,
"Bad Arg: -s and -b are mutually exclusive\n");
- exit(1);
+ exit(EXIT_FAILURE);
}
break;
case 'c':
default:
(void) fprintf(stderr,
"Bad arg: type \"%s ?\" for usage message\n", op[0]);
- exit(1);
+ exit(EXIT_FAILURE);
}
}
}