ncursesw-morphos/test/bs.c

1257 lines
30 KiB
C
Raw Normal View History

2006-12-18 12:32:42 +08:00
/****************************************************************************
* Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. *
2006-12-18 12:32:42 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
1997-05-15 12:00:00 +08:00
/*
* bs.c - original author: Bruce Holloway
* salvo option by: Chuck A DeGaul
* with improved user interface, autoconfiguration and code cleanup
* by Eric S. Raymond <esr@snark.thyrsus.com>
* 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
*
* $Id: bs.c,v 1.48 2009/08/29 19:02:25 tom Exp $
1997-05-15 12:00:00 +08:00
*/
2002-10-13 11:35:53 +08:00
#include <test.priv.h>
2005-10-10 02:41:57 +08:00
#include <time.h>
1997-05-15 12:00:00 +08:00
#ifndef SIGIOT
#define SIGIOT SIGABRT
#endif
static int getcoord(int);
/*
* Constants for tuning the random-fire algorithm. It prefers moves that
* diagonal-stripe the board with a stripe separation of srchstep. If
* no such preferred moves are found, srchstep is decremented.
*/
#define BEGINSTEP 3 /* initial value of srchstep */
/* miscellaneous constants */
#define SHIPTYPES 5
#define OTHER (1-turn)
#define PLAYER 0
#define COMPUTER 1
#define MARK_HIT 'H'
#define MARK_MISS 'o'
#define CTRLC '\003' /* used as terminate command */
#define FF '\014' /* used as redraw command */
/* coordinate handling */
#define BWIDTH 10
#define BDEPTH 10
/* display symbols */
#define SHOWHIT '*'
#define SHOWSPLASH ' '
2002-10-13 11:35:53 +08:00
#define IS_SHIP(c) (isupper(UChar(c)) ? TRUE : FALSE)
1997-05-15 12:00:00 +08:00
/* how to position us on player board */
#define PYBASE 3
#define PXBASE 3
#define PY(y) (PYBASE + (y))
#define PX(x) (PXBASE + (x)*3)
#define pgoto(y, x) (void)move(PY(y), PX(x))
/* how to position us on cpu board */
#define CYBASE 3
#define CXBASE 48
#define CY(y) (CYBASE + (y))
#define CX(x) (CXBASE + (x)*3)
#define CYINV(y) ((y) - CYBASE)
#define CXINV(x) (((x) - CXBASE) / 3)
#define cgoto(y, x) (void)move(CY(y), CX(x))
#define ONBOARD(x, y) (x >= 0 && x < BWIDTH && y >= 0 && y < BDEPTH)
/* other board locations */
#define COLWIDTH 80
2002-10-13 11:35:53 +08:00
#define PROMPTLINE 21 /* prompt line */
1997-05-15 12:00:00 +08:00
#define SYBASE CYBASE + BDEPTH + 3 /* move key diagram */
#define SXBASE 63
2002-10-13 11:35:53 +08:00
#define MYBASE SYBASE - 1 /* diagram caption */
1997-05-15 12:00:00 +08:00
#define MXBASE 64
2002-10-13 11:35:53 +08:00
#define HYBASE SYBASE - 1 /* help area */
1997-05-15 12:00:00 +08:00
#define HXBASE 0
/* this will need to be changed if BWIDTH changes */
static char numbers[] = " 0 1 2 3 4 5 6 7 8 9";
static char carrier[] = "Aircraft Carrier";
static char battle[] = "Battleship";
static char sub[] = "Submarine";
static char destroy[] = "Destroyer";
static char ptboat[] = "PT Boat";
static char name[40];
static char dftname[] = "stranger";
/* direction constants */
#define E 0
#define SE 1
#define S 2
#define SW 3
#define W 4
#define NW 5
#define N 6
#define NE 7
2002-10-13 11:35:53 +08:00
static int xincr[8] =
{1, 1, 0, -1, -1, -1, 0, 1};
static int yincr[8] =
{0, 1, 1, 1, 0, -1, -1, -1};
1997-05-15 12:00:00 +08:00
/* current ship position and direction */
static int curx = (BWIDTH / 2);
static int cury = (BDEPTH / 2);
2002-10-13 11:35:53 +08:00
typedef struct {
1997-05-15 12:00:00 +08:00
char *name; /* name of the ship type */
1998-03-01 12:21:12 +08:00
int hits; /* how many times has this ship been hit? */
1997-05-15 12:00:00 +08:00
char symbol; /* symbol for game purposes */
1998-03-01 12:21:12 +08:00
int length; /* length of ship */
2002-10-13 11:35:53 +08:00
int x, y; /* coordinates of ship start point */
int dir; /* direction of `bow' */
1997-05-15 12:00:00 +08:00
bool placed; /* has it been placed on the board? */
2002-10-13 11:35:53 +08:00
} ship_t;
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
static bool checkplace(int b, ship_t * ss, int vis);
1997-05-15 12:00:00 +08:00
1999-10-24 12:32:42 +08:00
#define SHIPIT(name, symbol, length) { name, 0, symbol, length, 0,0, 0, FALSE }
1997-05-15 12:00:00 +08:00
static ship_t plyship[SHIPTYPES] =
{
2002-10-13 11:35:53 +08:00
SHIPIT(carrier, 'A', 5),
SHIPIT(battle, 'B', 4),
SHIPIT(destroy, 'D', 3),
SHIPIT(sub, 'S', 3),
SHIPIT(ptboat, 'P', 2),
1997-05-15 12:00:00 +08:00
};
static ship_t cpuship[SHIPTYPES] =
{
2002-10-13 11:35:53 +08:00
SHIPIT(carrier, 'A', 5),
SHIPIT(battle, 'B', 4),
SHIPIT(destroy, 'D', 3),
SHIPIT(sub, 'S', 3),
SHIPIT(ptboat, 'P', 2),
1997-05-15 12:00:00 +08:00
};
/* "Hits" board, and main board. */
1998-03-01 12:21:12 +08:00
static char hits[2][BWIDTH][BDEPTH];
static char board[2][BWIDTH][BDEPTH];
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
static int turn; /* 0=player, 1=computer */
static int plywon = 0, cpuwon = 0; /* How many games has each won? */
1997-05-15 12:00:00 +08:00
static int salvo, blitz, closepack;
#define PR (void)addstr
2002-10-13 11:35:53 +08:00
static RETSIGTYPE uninitgame(int sig) GCC_NORETURN;
1997-05-15 12:00:00 +08:00
2006-12-18 12:32:42 +08:00
static RETSIGTYPE
uninitgame(int sig GCC_UNUSED)
1997-05-15 12:00:00 +08:00
/* end the game, either normally or due to signal */
{
clear();
2002-10-13 11:35:53 +08:00
(void) refresh();
(void) reset_shell_mode();
(void) echo();
(void) endwin();
ExitProgram(sig ? EXIT_FAILURE : EXIT_SUCCESS);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static void
announceopts(void)
1997-05-15 12:00:00 +08:00
/* announce which game options are enabled */
{
2002-10-13 11:35:53 +08:00
if (salvo || blitz || closepack) {
1997-05-15 12:00:00 +08:00
(void) printw("Playing optional game (");
if (salvo)
(void) printw("salvo, ");
else
(void) printw("nosalvo, ");
if (blitz)
(void) printw("blitz ");
else
(void) printw("noblitz, ");
if (closepack)
(void) printw("closepack)");
else
(void) printw("noclosepack)");
2002-10-13 11:35:53 +08:00
} else
1997-05-15 12:00:00 +08:00
(void) printw(
2002-10-13 11:35:53 +08:00
"Playing standard game (noblitz, nosalvo, noclosepack)");
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static void
intro(void)
1997-05-15 12:00:00 +08:00
{
char *tmpname;
2002-10-13 11:35:53 +08:00
srand((unsigned) (time(0L) + getpid())); /* Kick the random number generator */
1997-05-15 12:00:00 +08:00
2006-12-18 12:32:42 +08:00
CATCHALL(uninitgame);
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
if ((tmpname = getlogin()) != 0) {
(void) strcpy(name, tmpname);
name[0] = toupper(UChar(name[0]));
2002-10-13 11:35:53 +08:00
} else
(void) strcpy(name, dftname);
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
(void) initscr();
1997-05-15 12:00:00 +08:00
keypad(stdscr, TRUE);
2002-10-13 11:35:53 +08:00
(void) def_prog_mode();
(void) nonl();
(void) cbreak();
(void) noecho();
1997-05-15 12:00:00 +08:00
#ifdef PENGUIN
2002-10-13 11:35:53 +08:00
(void) clear();
(void) mvaddstr(4, 29, "Welcome to Battleship!");
(void) move(8, 0);
1997-05-15 12:00:00 +08:00
PR(" \\\n");
PR(" \\ \\ \\\n");
PR(" \\ \\ \\ \\ \\_____________\n");
PR(" \\ \\ \\_____________ \\ \\/ |\n");
PR(" \\ \\/ \\ \\/ |\n");
PR(" \\/ \\_____/ |__\n");
PR(" ________________/ |\n");
PR(" \\ S.S. Penguin |\n");
PR(" \\ /\n");
PR(" \\___________________________________________________/\n");
2002-10-13 11:35:53 +08:00
(void) mvaddstr(22, 27, "Hit any key to continue...");
(void) refresh();
1997-05-15 12:00:00 +08:00
(void) getch();
#endif /* PENGUIN */
#ifdef A_COLOR
start_color();
init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
#endif /* A_COLOR */
#ifdef NCURSES_MOUSE_VERSION
2002-10-13 11:35:53 +08:00
(void) mousemask(BUTTON1_CLICKED, (mmask_t *) NULL);
#endif /* NCURSES_MOUSE_VERSION */
}
1997-05-15 12:00:00 +08:00
/* VARARGS1 */
2002-10-13 11:35:53 +08:00
static void
2005-10-10 02:41:57 +08:00
prompt(int n, NCURSES_CONST char *f, const char *s)
1997-05-15 12:00:00 +08:00
/* print a message at the prompt line */
{
(void) move(PROMPTLINE + n, 0);
(void) clrtoeol();
(void) printw(f, s);
(void) refresh();
}
2002-10-13 11:35:53 +08:00
static void
error(NCURSES_CONST char *s)
1997-05-15 12:00:00 +08:00
{
(void) move(PROMPTLINE + 2, 0);
(void) clrtoeol();
2002-10-13 11:35:53 +08:00
if (s) {
1997-05-15 12:00:00 +08:00
(void) addstr(s);
(void) beep();
}
}
2002-10-13 11:35:53 +08:00
static void
placeship(int b, ship_t * ss, int vis)
1997-05-15 12:00:00 +08:00
{
int l;
2002-10-13 11:35:53 +08:00
for (l = 0; l < ss->length; ++l) {
1997-05-15 12:00:00 +08:00
int newx = ss->x + l * xincr[ss->dir];
int newy = ss->y + l * yincr[ss->dir];
board[b][newx][newy] = ss->symbol;
2002-10-13 11:35:53 +08:00
if (vis) {
1997-05-15 12:00:00 +08:00
pgoto(newy, newx);
2002-10-13 11:35:53 +08:00
(void) addch((chtype) ss->symbol);
1997-05-15 12:00:00 +08:00
}
}
ss->hits = 0;
}
2002-10-13 11:35:53 +08:00
static int
rnd(int n)
1997-05-15 12:00:00 +08:00
{
2002-10-13 11:35:53 +08:00
return (((rand() & 0x7FFF) % n));
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static void
randomplace(int b, ship_t * ss)
1997-05-15 12:00:00 +08:00
/* generate a valid random ship placement into px,py */
{
do {
ss->dir = rnd(2) ? E : S;
2002-10-13 11:35:53 +08:00
ss->x = rnd(BWIDTH - (ss->dir == E ? ss->length : 0));
ss->y = rnd(BDEPTH - (ss->dir == S ? ss->length : 0));
1997-05-15 12:00:00 +08:00
} while
(!checkplace(b, ss, FALSE));
}
2002-10-13 11:35:53 +08:00
static void
initgame(void)
1997-05-15 12:00:00 +08:00
{
int i, j, unplaced;
ship_t *ss;
(void) clear();
2002-10-13 11:35:53 +08:00
(void) mvaddstr(0, 35, "BATTLESHIPS");
1997-05-15 12:00:00 +08:00
(void) move(PROMPTLINE + 2, 0);
announceopts();
memset(board, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
2002-10-13 11:35:53 +08:00
memset(hits, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
for (i = 0; i < SHIPTYPES; i++) {
1997-05-15 12:00:00 +08:00
ss = cpuship + i;
1998-03-01 12:21:12 +08:00
ss->x =
2002-10-13 11:35:53 +08:00
ss->y =
ss->dir =
ss->hits = 0;
1998-03-01 12:21:12 +08:00
ss->placed = FALSE;
1997-05-15 12:00:00 +08:00
ss = plyship + i;
1998-03-01 12:21:12 +08:00
ss->x =
2002-10-13 11:35:53 +08:00
ss->y =
ss->dir =
ss->hits = 0;
1998-03-01 12:21:12 +08:00
ss->placed = FALSE;
1997-05-15 12:00:00 +08:00
}
/* draw empty boards */
(void) mvaddstr(PYBASE - 2, PXBASE + 5, "Main Board");
2002-10-13 11:35:53 +08:00
(void) mvaddstr(PYBASE - 1, PXBASE - 3, numbers);
for (i = 0; i < BDEPTH; ++i) {
(void) mvaddch(PYBASE + i, PXBASE - 3, (chtype) (i + 'A'));
1997-05-15 12:00:00 +08:00
#ifdef A_COLOR
if (has_colors())
attron(COLOR_PAIR(COLOR_BLUE));
#endif /* A_COLOR */
(void) addch(' ');
for (j = 0; j < BWIDTH; j++)
(void) addstr(" . ");
#ifdef A_COLOR
(void) attrset(0);
1997-05-15 12:00:00 +08:00
#endif /* A_COLOR */
(void) addch(' ');
2002-10-13 11:35:53 +08:00
(void) addch((chtype) (i + 'A'));
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
(void) mvaddstr(PYBASE + BDEPTH, PXBASE - 3, numbers);
(void) mvaddstr(CYBASE - 2, CXBASE + 7, "Hit/Miss Board");
1997-05-15 12:00:00 +08:00
(void) mvaddstr(CYBASE - 1, CXBASE - 3, numbers);
2002-10-13 11:35:53 +08:00
for (i = 0; i < BDEPTH; ++i) {
(void) mvaddch(CYBASE + i, CXBASE - 3, (chtype) (i + 'A'));
1997-05-15 12:00:00 +08:00
#ifdef A_COLOR
if (has_colors())
attron(COLOR_PAIR(COLOR_BLUE));
#endif /* A_COLOR */
(void) addch(' ');
for (j = 0; j < BWIDTH; j++)
(void) addstr(" . ");
#ifdef A_COLOR
(void) attrset(0);
1997-05-15 12:00:00 +08:00
#endif /* A_COLOR */
(void) addch(' ');
2002-10-13 11:35:53 +08:00
(void) addch((chtype) (i + 'A'));
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
(void) mvaddstr(CYBASE + BDEPTH, CXBASE - 3, numbers);
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE, HXBASE,
1997-05-15 12:00:00 +08:00
"To position your ships: move the cursor to a spot, then");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 1, HXBASE,
1997-05-15 12:00:00 +08:00
"type the first letter of a ship type to select it, then");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 2, HXBASE,
1997-05-15 12:00:00 +08:00
"type a direction ([hjkl] or [4862]), indicating how the");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 3, HXBASE,
1997-05-15 12:00:00 +08:00
"ship should be pointed. You may also type a ship letter");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 4, HXBASE,
1997-05-15 12:00:00 +08:00
"followed by `r' to position it randomly, or type `R' to");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 5, HXBASE,
1997-05-15 12:00:00 +08:00
"place all remaining ships randomly.");
2002-10-13 11:35:53 +08:00
(void) mvaddstr(MYBASE, MXBASE, "Aiming keys:");
(void) mvaddstr(SYBASE, SXBASE, "y k u 7 8 9");
(void) mvaddstr(SYBASE + 1, SXBASE, " \\|/ \\|/ ");
(void) mvaddstr(SYBASE + 2, SXBASE, "h-+-l 4-+-6");
(void) mvaddstr(SYBASE + 3, SXBASE, " /|\\ /|\\ ");
(void) mvaddstr(SYBASE + 4, SXBASE, "b j n 1 2 3");
1997-05-15 12:00:00 +08:00
/* have the computer place ships */
2002-10-13 11:35:53 +08:00
for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++) {
1997-05-15 12:00:00 +08:00
randomplace(COMPUTER, ss);
placeship(COMPUTER, ss, FALSE);
}
2002-10-13 11:35:53 +08:00
ss = (ship_t *) NULL;
1997-05-15 12:00:00 +08:00
do {
char c, docked[SHIPTYPES + 2], *cp = docked;
/* figure which ships still wait to be placed */
*cp++ = 'R';
for (i = 0; i < SHIPTYPES; i++)
if (!plyship[i].placed)
*cp++ = plyship[i].symbol;
*cp = '\0';
/* get a command letter */
2002-10-13 11:35:53 +08:00
prompt(1, "Type one of [%s] to pick a ship.", docked + 1);
1997-05-15 12:00:00 +08:00
do {
c = getcoord(PLAYER);
} while
(!strchr(docked, c));
if (c == 'R')
(void) ungetch('R');
2002-10-13 11:35:53 +08:00
else {
1997-05-15 12:00:00 +08:00
/* map that into the corresponding symbol */
for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
if (ss->symbol == c)
break;
prompt(1, "Type one of [hjklrR] to place your %s.", ss->name);
pgoto(cury, curx);
}
do {
c = getch();
} while
2007-04-08 09:10:28 +08:00
(!(strchr("hjklrR", c) || c == FF));
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
if (c == FF) {
(void) clearok(stdscr, TRUE);
(void) refresh();
} else if (c == 'r') {
assert(ss != 0);
1997-05-15 12:00:00 +08:00
prompt(1, "Random-placing your %s", ss->name);
randomplace(PLAYER, ss);
placeship(PLAYER, ss, TRUE);
2002-10-13 11:35:53 +08:00
error((char *) NULL);
1997-05-15 12:00:00 +08:00
ss->placed = TRUE;
2002-10-13 11:35:53 +08:00
} else if (c == 'R') {
1997-05-15 12:00:00 +08:00
prompt(1, "Placing the rest of your fleet at random...", "");
for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
2002-10-13 11:35:53 +08:00
if (!ss->placed) {
1997-05-15 12:00:00 +08:00
randomplace(PLAYER, ss);
placeship(PLAYER, ss, TRUE);
ss->placed = TRUE;
}
2002-10-13 11:35:53 +08:00
error((char *) NULL);
} else if (strchr("hjkl8462", c)) {
assert(ss != 0);
1997-05-15 12:00:00 +08:00
ss->x = curx;
ss->y = cury;
2002-10-13 11:35:53 +08:00
switch (c) {
case 'k':
case '8':
ss->dir = N;
break;
case 'j':
case '2':
ss->dir = S;
break;
case 'h':
case '4':
ss->dir = W;
break;
case 'l':
case '6':
ss->dir = E;
break;
}
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
if (checkplace(PLAYER, ss, TRUE)) {
1997-05-15 12:00:00 +08:00
placeship(PLAYER, ss, TRUE);
2002-10-13 11:35:53 +08:00
error((char *) NULL);
1997-05-15 12:00:00 +08:00
ss->placed = TRUE;
}
}
for (unplaced = i = 0; i < SHIPTYPES; i++)
unplaced += !plyship[i].placed;
} while
(unplaced);
turn = rnd(2);
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE, HXBASE,
1997-05-15 12:00:00 +08:00
"To fire, move the cursor to your chosen aiming point ");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 1, HXBASE,
1997-05-15 12:00:00 +08:00
"and strike any key other than a motion key. ");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 2, HXBASE,
1997-05-15 12:00:00 +08:00
" ");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 3, HXBASE,
1997-05-15 12:00:00 +08:00
" ");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 4, HXBASE,
1997-05-15 12:00:00 +08:00
" ");
2002-10-13 11:35:53 +08:00
(void) mvprintw(HYBASE + 5, HXBASE,
1997-05-15 12:00:00 +08:00
" ");
(void) prompt(0, "Press any key to start...", "");
(void) getch();
}
2002-10-13 11:35:53 +08:00
static int
getcoord(int atcpu)
1997-05-15 12:00:00 +08:00
{
int ny, nx, c;
if (atcpu)
2002-10-13 11:35:53 +08:00
cgoto(cury, curx);
1997-05-15 12:00:00 +08:00
else
pgoto(cury, curx);
2002-10-13 11:35:53 +08:00
(void) refresh();
for (;;) {
if (atcpu) {
(void) mvprintw(CYBASE + BDEPTH + 1, CXBASE + 11, "(%d, %c)",
curx, 'A' + cury);
1997-05-15 12:00:00 +08:00
cgoto(cury, curx);
2002-10-13 11:35:53 +08:00
} else {
(void) mvprintw(PYBASE + BDEPTH + 1, PXBASE + 11, "(%d, %c)",
curx, 'A' + cury);
1997-05-15 12:00:00 +08:00
pgoto(cury, curx);
}
2002-10-13 11:35:53 +08:00
switch (c = getch()) {
case 'k':
case '8':
1997-05-15 12:00:00 +08:00
case KEY_UP:
2002-10-13 11:35:53 +08:00
ny = cury + BDEPTH - 1;
nx = curx;
1997-05-15 12:00:00 +08:00
break;
2002-10-13 11:35:53 +08:00
case 'j':
case '2':
1997-05-15 12:00:00 +08:00
case KEY_DOWN:
2002-10-13 11:35:53 +08:00
ny = cury + 1;
nx = curx;
1997-05-15 12:00:00 +08:00
break;
2002-10-13 11:35:53 +08:00
case 'h':
case '4':
1997-05-15 12:00:00 +08:00
case KEY_LEFT:
2002-10-13 11:35:53 +08:00
ny = cury;
nx = curx + BWIDTH - 1;
1997-05-15 12:00:00 +08:00
break;
2002-10-13 11:35:53 +08:00
case 'l':
case '6':
1997-05-15 12:00:00 +08:00
case KEY_RIGHT:
2002-10-13 11:35:53 +08:00
ny = cury;
nx = curx + 1;
1997-05-15 12:00:00 +08:00
break;
2002-10-13 11:35:53 +08:00
case 'y':
case '7':
1997-05-15 12:00:00 +08:00
case KEY_A1:
2002-10-13 11:35:53 +08:00
ny = cury + BDEPTH - 1;
nx = curx + BWIDTH - 1;
1997-05-15 12:00:00 +08:00
break;
2002-10-13 11:35:53 +08:00
case 'b':
case '1':
1997-05-15 12:00:00 +08:00
case KEY_C1:
2002-10-13 11:35:53 +08:00
ny = cury + 1;
nx = curx + BWIDTH - 1;
1997-05-15 12:00:00 +08:00
break;
2002-10-13 11:35:53 +08:00
case 'u':
case '9':
1997-05-15 12:00:00 +08:00
case KEY_A3:
2002-10-13 11:35:53 +08:00
ny = cury + BDEPTH - 1;
nx = curx + 1;
1997-05-15 12:00:00 +08:00
break;
2002-10-13 11:35:53 +08:00
case 'n':
case '3':
1997-05-15 12:00:00 +08:00
case KEY_C3:
2002-10-13 11:35:53 +08:00
ny = cury + 1;
nx = curx + 1;
1997-05-15 12:00:00 +08:00
break;
case FF:
2002-10-13 11:35:53 +08:00
nx = curx;
ny = cury;
(void) clearok(stdscr, TRUE);
(void) refresh();
1997-05-15 12:00:00 +08:00
break;
#ifdef NCURSES_MOUSE_VERSION
case KEY_MOUSE:
{
2002-10-13 11:35:53 +08:00
MEVENT myevent;
1997-05-15 12:00:00 +08:00
getmouse(&myevent);
if (atcpu
2002-10-13 11:35:53 +08:00
&& myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
&& myevent.x >= CX(0) && myevent.x <= CX(BDEPTH)) {
1997-05-15 12:00:00 +08:00
curx = CXINV(myevent.x);
cury = CYINV(myevent.y);
2002-10-13 11:35:53 +08:00
return (' ');
} else {
1997-05-15 12:00:00 +08:00
beep();
continue;
}
}
/* no fall through */
#endif /* NCURSES_MOUSE_VERSION */
default:
if (atcpu)
(void) mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, " ");
else
(void) mvaddstr(PYBASE + BDEPTH + 1, PXBASE + 11, " ");
2002-10-13 11:35:53 +08:00
return (c);
1997-05-15 12:00:00 +08:00
}
curx = nx % BWIDTH;
cury = ny % BDEPTH;
}
}
2002-10-13 11:35:53 +08:00
static bool
collidecheck(int b, int y, int x)
1997-05-15 12:00:00 +08:00
/* is this location on the selected zboard adjacent to a ship? */
{
1998-03-01 12:21:12 +08:00
bool collide;
1997-05-15 12:00:00 +08:00
/* anything on the square */
1998-03-01 12:21:12 +08:00
if ((collide = IS_SHIP(board[b][x][y])) != FALSE)
2002-10-13 11:35:53 +08:00
return (collide);
1997-05-15 12:00:00 +08:00
/* anything on the neighbors */
2002-10-13 11:35:53 +08:00
if (!closepack) {
1997-05-15 12:00:00 +08:00
int i;
2002-10-13 11:35:53 +08:00
for (i = 0; i < 8; i++) {
1997-05-15 12:00:00 +08:00
int xend, yend;
yend = y + yincr[i];
xend = x + xincr[i];
1998-03-01 12:21:12 +08:00
if (ONBOARD(xend, yend)
2002-10-13 11:35:53 +08:00
&& IS_SHIP(board[b][xend][yend])) {
1998-03-01 12:21:12 +08:00
collide = TRUE;
break;
}
1997-05-15 12:00:00 +08:00
}
}
2002-10-13 11:35:53 +08:00
return (collide);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static bool
checkplace(int b, ship_t * ss, int vis)
1997-05-15 12:00:00 +08:00
{
int l, xend, yend;
/* first, check for board edges */
2002-10-13 11:35:53 +08:00
xend = ss->x + (ss->length - 1) * xincr[ss->dir];
yend = ss->y + (ss->length - 1) * yincr[ss->dir];
if (!ONBOARD(xend, yend)) {
1997-05-15 12:00:00 +08:00
if (vis)
2002-10-13 11:35:53 +08:00
switch (rnd(3)) {
1997-05-15 12:00:00 +08:00
case 0:
error("Ship is hanging from the edge of the world");
break;
case 1:
error("Try fitting it on the board");
break;
case 2:
error("Figure I won't find it if you put it there?");
break;
}
2002-10-13 11:35:53 +08:00
return (FALSE);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
for (l = 0; l < ss->length; ++l) {
if (collidecheck(b, ss->y + l * yincr[ss->dir], ss->x + l * xincr[ss->dir])) {
1997-05-15 12:00:00 +08:00
if (vis)
2002-10-13 11:35:53 +08:00
switch (rnd(3)) {
case 0:
error("There's already a ship there");
break;
case 1:
error("Collision alert! Aaaaaagh!");
break;
case 2:
error("Er, Admiral, what about the other ship?");
break;
}
return (FALSE);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
}
return (TRUE);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static int
awinna(void)
1997-05-15 12:00:00 +08:00
{
int i, j;
ship_t *ss;
2002-10-13 11:35:53 +08:00
for (i = 0; i < 2; ++i) {
1997-05-15 12:00:00 +08:00
ss = (i) ? cpuship : plyship;
2002-10-13 11:35:53 +08:00
for (j = 0; j < SHIPTYPES; ++j, ++ss)
if (ss->length > ss->hits)
1997-05-15 12:00:00 +08:00
break;
if (j == SHIPTYPES)
2002-10-13 11:35:53 +08:00
return (OTHER);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
return (-1);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static ship_t *
hitship(int x, int y)
1997-05-15 12:00:00 +08:00
/* register a hit on the targeted ship */
{
ship_t *sb, *ss;
char sym;
int oldx, oldy;
getyx(stdscr, oldy, oldx);
sb = (turn) ? plyship : cpuship;
2002-10-13 11:35:53 +08:00
if ((sym = board[OTHER][x][y]) == 0)
return ((ship_t *) NULL);
for (ss = sb; ss < sb + SHIPTYPES; ++ss)
if (ss->symbol == sym) {
1997-05-15 12:00:00 +08:00
if (++ss->hits < ss->length) /* still afloat? */
2002-10-13 11:35:53 +08:00
return ((ship_t *) NULL);
else { /* sunk! */
1997-05-15 12:00:00 +08:00
int i, j;
if (!closepack)
2002-10-13 11:35:53 +08:00
for (j = -1; j <= 1; j++) {
1997-05-15 12:00:00 +08:00
int bx = ss->x + j * xincr[(ss->dir + 2) % 8];
int by = ss->y + j * yincr[(ss->dir + 2) % 8];
2002-10-13 11:35:53 +08:00
for (i = -1; i <= ss->length; ++i) {
1997-05-15 12:00:00 +08:00
int x1, y1;
2002-10-13 11:35:53 +08:00
1997-05-15 12:00:00 +08:00
x1 = bx + i * xincr[ss->dir];
y1 = by + i * yincr[ss->dir];
2002-10-13 11:35:53 +08:00
if (ONBOARD(x1, y1)) {
1997-05-15 12:00:00 +08:00
hits[turn][x1][y1] = MARK_MISS;
2002-10-13 11:35:53 +08:00
if (turn % 2 == PLAYER) {
1997-05-15 12:00:00 +08:00
cgoto(y1, x1);
#ifdef A_COLOR
if (has_colors())
attron(COLOR_PAIR(COLOR_GREEN));
#endif /* A_COLOR */
2002-10-13 11:35:53 +08:00
(void) addch(MARK_MISS);
1997-05-15 12:00:00 +08:00
#ifdef A_COLOR
(void) attrset(0);
1997-05-15 12:00:00 +08:00
#endif /* A_COLOR */
2002-10-13 11:35:53 +08:00
} else {
pgoto(y1, x1);
(void) addch(SHOWSPLASH);
1997-05-15 12:00:00 +08:00
}
}
}
}
2002-10-13 11:35:53 +08:00
for (i = 0; i < ss->length; ++i) {
1997-05-15 12:00:00 +08:00
int x1 = ss->x + i * xincr[ss->dir];
int y1 = ss->y + i * yincr[ss->dir];
hits[turn][x1][y1] = ss->symbol;
2002-10-13 11:35:53 +08:00
if (turn % 2 == PLAYER) {
1997-05-15 12:00:00 +08:00
cgoto(y1, x1);
2002-10-13 11:35:53 +08:00
(void) addch((chtype) (ss->symbol));
} else {
pgoto(y1, x1);
#ifdef A_COLOR
if (has_colors())
attron(COLOR_PAIR(COLOR_RED));
#endif /* A_COLOR */
(void) addch(SHOWHIT);
#ifdef A_COLOR
(void) attrset(0);
2002-10-13 11:35:53 +08:00
#endif /* A_COLOR */
1997-05-15 12:00:00 +08:00
}
}
(void) move(oldy, oldx);
2002-10-13 11:35:53 +08:00
return (ss);
1997-05-15 12:00:00 +08:00
}
}
(void) move(oldy, oldx);
2002-10-13 11:35:53 +08:00
return ((ship_t *) NULL);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static bool
plyturn(void)
1997-05-15 12:00:00 +08:00
{
ship_t *ss;
bool hit;
NCURSES_CONST char *m = NULL;
prompt(1, "Where do you want to shoot? ", "");
2002-10-13 11:35:53 +08:00
for (;;) {
1997-05-15 12:00:00 +08:00
(void) getcoord(COMPUTER);
2002-10-13 11:35:53 +08:00
if (hits[PLAYER][curx][cury]) {
1997-05-15 12:00:00 +08:00
prompt(1, "You shelled this spot already! Try again.", "");
beep();
2002-10-13 11:35:53 +08:00
} else
1997-05-15 12:00:00 +08:00
break;
}
hit = IS_SHIP(board[COMPUTER][curx][cury]);
1998-03-01 12:21:12 +08:00
hits[PLAYER][curx][cury] = (hit ? MARK_HIT : MARK_MISS);
1997-05-15 12:00:00 +08:00
cgoto(cury, curx);
#ifdef A_COLOR
1999-10-24 12:32:42 +08:00
if (has_colors()) {
1997-05-15 12:00:00 +08:00
if (hit)
attron(COLOR_PAIR(COLOR_RED));
else
attron(COLOR_PAIR(COLOR_GREEN));
1999-10-24 12:32:42 +08:00
}
1997-05-15 12:00:00 +08:00
#endif /* A_COLOR */
2002-10-13 11:35:53 +08:00
(void) addch((chtype) hits[PLAYER][curx][cury]);
1997-05-15 12:00:00 +08:00
#ifdef A_COLOR
(void) attrset(0);
1997-05-15 12:00:00 +08:00
#endif /* A_COLOR */
prompt(1, "You %s.", hit ? "scored a hit" : "missed");
2002-10-13 11:35:53 +08:00
if (hit && (ss = hitship(curx, cury))) {
switch (rnd(5)) {
1997-05-15 12:00:00 +08:00
case 0:
m = " You sank my %s!";
break;
case 1:
m = " I have this sinking feeling about my %s....";
break;
case 2:
m = " My %s has gone to Davy Jones's locker!";
break;
case 3:
m = " Glub, glub -- my %s is headed for the bottom!";
break;
case 4:
m = " You'll pick up survivors from my %s, I hope...!";
break;
}
2002-10-13 11:35:53 +08:00
(void) printw(m, ss->name);
(void) beep();
1997-05-15 12:00:00 +08:00
}
1998-03-01 12:21:12 +08:00
return (hit);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static int
sgetc(const char *s)
1997-05-15 12:00:00 +08:00
{
const char *s1;
int ch;
2002-10-13 11:35:53 +08:00
(void) refresh();
for (;;) {
1997-05-15 12:00:00 +08:00
ch = getch();
if (islower(ch))
ch = toupper(ch);
if (ch == CTRLC)
uninitgame(0);
2002-10-13 11:35:53 +08:00
for (s1 = s; *s1 && ch != *s1; ++s1)
1997-05-15 12:00:00 +08:00
continue;
2002-10-13 11:35:53 +08:00
if (*s1) {
(void) addch((chtype) ch);
(void) refresh();
return (ch);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
}
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static void
randomfire(int *px, int *py)
1997-05-15 12:00:00 +08:00
/* random-fire routine -- implements simple diagonal-striping strategy */
{
static int turncount = 0;
static int srchstep = BEGINSTEP;
2002-10-13 11:35:53 +08:00
static int huntoffs; /* Offset on search strategy */
1997-05-15 12:00:00 +08:00
int ypossible[BWIDTH * BDEPTH], xpossible[BWIDTH * BDEPTH], nposs;
int ypreferred[BWIDTH * BDEPTH], xpreferred[BWIDTH * BDEPTH], npref;
int x, y, i;
if (turncount++ == 0)
huntoffs = rnd(srchstep);
/* first, list all possible moves */
nposs = npref = 0;
for (x = 0; x < BWIDTH; x++)
for (y = 0; y < BDEPTH; y++)
2002-10-13 11:35:53 +08:00
if (!hits[COMPUTER][x][y]) {
1997-05-15 12:00:00 +08:00
xpossible[nposs] = x;
ypossible[nposs] = y;
nposs++;
2002-10-13 11:35:53 +08:00
if (((x + huntoffs) % srchstep) != (y % srchstep)) {
1997-05-15 12:00:00 +08:00
xpreferred[npref] = x;
ypreferred[npref] = y;
npref++;
}
}
2002-10-13 11:35:53 +08:00
if (npref) {
1997-05-15 12:00:00 +08:00
i = rnd(npref);
*px = xpreferred[i];
*py = ypreferred[i];
2002-10-13 11:35:53 +08:00
} else if (nposs) {
1997-05-15 12:00:00 +08:00
i = rnd(nposs);
*px = xpossible[i];
*py = ypossible[i];
if (srchstep > 1)
--srchstep;
2002-10-13 11:35:53 +08:00
} else {
1997-05-15 12:00:00 +08:00
error("No moves possible?? Help!");
2002-10-13 11:35:53 +08:00
ExitProgram(EXIT_FAILURE);
/*NOTREACHED */
1997-05-15 12:00:00 +08:00
}
}
#define S_MISS 0
#define S_HIT 1
#define S_SUNK -1
2002-10-13 11:35:53 +08:00
static int
cpufire(int x, int y)
1997-05-15 12:00:00 +08:00
/* fire away at given location */
{
bool hit, sunk;
ship_t *ss = NULL;
hits[COMPUTER][x][y] = (hit = (board[PLAYER][x][y])) ? MARK_HIT : MARK_MISS;
(void) mvprintw(PROMPTLINE, 0,
2002-10-13 11:35:53 +08:00
"I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" :
"miss");
1997-05-15 12:00:00 +08:00
if ((sunk = (hit && (ss = hitship(x, y)))) != 0)
(void) printw(" I've sunk your %s", ss->name);
2002-10-13 11:35:53 +08:00
(void) clrtoeol();
1997-05-15 12:00:00 +08:00
pgoto(y, x);
#ifdef A_COLOR
1999-10-24 12:32:42 +08:00
if (has_colors()) {
1997-05-15 12:00:00 +08:00
if (hit)
attron(COLOR_PAIR(COLOR_RED));
else
attron(COLOR_PAIR(COLOR_GREEN));
1999-10-24 12:32:42 +08:00
}
1997-05-15 12:00:00 +08:00
#endif /* A_COLOR */
2002-10-13 11:35:53 +08:00
(void) addch((chtype) (hit ? SHOWHIT : SHOWSPLASH));
1997-05-15 12:00:00 +08:00
#ifdef A_COLOR
(void) attrset(0);
1997-05-15 12:00:00 +08:00
#endif /* A_COLOR */
2006-12-18 12:32:42 +08:00
return hit ? (sunk ? S_SUNK : S_HIT) : S_MISS;
1997-05-15 12:00:00 +08:00
}
/*
* This code implements a fairly irregular FSM, so please forgive the rampant
* unstructuredness below. The five labels are states which need to be held
* between computer turns.
2006-12-18 12:32:42 +08:00
*
* The FSM is not externally reset to RANDOM_FIRE if the player wins. Instead,
* the other states check for "impossible" conditions which signify a new
* game, then if found transition to RANDOM_FIRE.
1997-05-15 12:00:00 +08:00
*/
2002-10-13 11:35:53 +08:00
static bool
cputurn(void)
1997-05-15 12:00:00 +08:00
{
#define POSSIBLE(x, y) (ONBOARD(x, y) && !hits[COMPUTER][x][y])
#define RANDOM_FIRE 0
#define RANDOM_HIT 1
#define HUNT_DIRECT 2
#define FIRST_PASS 3
#define REVERSE_JUMP 4
#define SECOND_PASS 5
static int next = RANDOM_FIRE;
static bool used[4];
static ship_t ts;
1998-03-01 12:21:12 +08:00
int navail, x, y, d, n;
int hit = S_MISS;
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
switch (next) {
case RANDOM_FIRE: /* last shot was random and missed */
refire:
1997-05-15 12:00:00 +08:00
randomfire(&x, &y);
if (!(hit = cpufire(x, y)))
next = RANDOM_FIRE;
2002-10-13 11:35:53 +08:00
else {
ts.x = x;
ts.y = y;
1997-05-15 12:00:00 +08:00
ts.hits = 1;
next = (hit == S_SUNK) ? RANDOM_FIRE : RANDOM_HIT;
}
break;
2002-10-13 11:35:53 +08:00
case RANDOM_HIT: /* last shot was random and hit */
used[E / 2] = used[S / 2] = used[W / 2] = used[N / 2] = FALSE;
1997-05-15 12:00:00 +08:00
/* FALLTHROUGH */
2002-10-13 11:35:53 +08:00
case HUNT_DIRECT: /* last shot hit, we're looking for ship's long axis */
for (d = navail = 0; d < 4; d++) {
x = ts.x + xincr[d * 2];
y = ts.y + yincr[d * 2];
1997-05-15 12:00:00 +08:00
if (!used[d] && POSSIBLE(x, y))
navail++;
else
used[d] = TRUE;
}
if (navail == 0) /* no valid places for shots adjacent... */
goto refire; /* ...so we must random-fire */
2002-10-13 11:35:53 +08:00
else {
2006-12-18 12:32:42 +08:00
n = rnd(navail) + 1;
for (d = 0; used[d]; d++) ;
/* used[d] is first that == 0 */
for (; n > 1; n--)
while (used[++d]) ;
/* used[d] is next that == 0 */
1997-05-15 12:00:00 +08:00
2006-12-18 12:32:42 +08:00
assert(d < 4);
assert(used[d] == FALSE);
1997-05-15 12:00:00 +08:00
2006-12-18 12:32:42 +08:00
used[d] = TRUE;
2002-10-13 11:35:53 +08:00
x = ts.x + xincr[d * 2];
y = ts.y + yincr[d * 2];
1997-05-15 12:00:00 +08:00
assert(POSSIBLE(x, y));
if (!(hit = cpufire(x, y)))
next = HUNT_DIRECT;
2002-10-13 11:35:53 +08:00
else {
ts.x = x;
ts.y = y;
ts.dir = d * 2;
ts.hits++;
1997-05-15 12:00:00 +08:00
next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
}
}
break;
2002-10-13 11:35:53 +08:00
case FIRST_PASS: /* we have a start and a direction now */
1997-05-15 12:00:00 +08:00
x = ts.x + xincr[ts.dir];
y = ts.y + yincr[ts.dir];
2002-10-13 11:35:53 +08:00
if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
ts.x = x;
ts.y = y;
ts.hits++;
1997-05-15 12:00:00 +08:00
next = (hit == S_SUNK) ? RANDOM_FIRE : FIRST_PASS;
2002-10-13 11:35:53 +08:00
} else
1997-05-15 12:00:00 +08:00
next = REVERSE_JUMP;
break;
2002-10-13 11:35:53 +08:00
case REVERSE_JUMP: /* nail down the ship's other end */
2006-12-18 12:32:42 +08:00
d = (ts.dir + 4) % 8;
1997-05-15 12:00:00 +08:00
x = ts.x + ts.hits * xincr[d];
y = ts.y + ts.hits * yincr[d];
2002-10-13 11:35:53 +08:00
if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
ts.x = x;
ts.y = y;
ts.dir = d;
ts.hits++;
1997-05-15 12:00:00 +08:00
next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
2002-10-13 11:35:53 +08:00
} else
1997-05-15 12:00:00 +08:00
next = RANDOM_FIRE;
break;
2006-12-18 12:32:42 +08:00
case SECOND_PASS: /* continue shooting after reversing */
1997-05-15 12:00:00 +08:00
x = ts.x + xincr[ts.dir];
y = ts.y + yincr[ts.dir];
2002-10-13 11:35:53 +08:00
if (POSSIBLE(x, y) && (hit = cpufire(x, y))) {
ts.x = x;
ts.y = y;
ts.hits++;
next = (hit == S_SUNK) ? RANDOM_FIRE : SECOND_PASS;
1997-05-15 12:00:00 +08:00
break;
2002-10-13 11:35:53 +08:00
} else
1997-05-15 12:00:00 +08:00
next = RANDOM_FIRE;
break;
}
2006-12-18 12:32:42 +08:00
/* pause between shots in salvo */
2002-10-13 11:35:53 +08:00
if (salvo) {
(void) refresh();
(void) sleep(1);
1997-05-15 12:00:00 +08:00
}
#ifdef DEBUG
(void) mvprintw(PROMPTLINE + 2, 0,
"New state %d, x=%d, y=%d, d=%d",
next, x, y, d);
#endif /* DEBUG */
1998-03-01 12:21:12 +08:00
return ((hit) ? TRUE : FALSE);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static int
playagain(void)
1997-05-15 12:00:00 +08:00
{
int j;
ship_t *ss;
for (ss = cpuship; ss < cpuship + SHIPTYPES; ss++)
2002-10-13 11:35:53 +08:00
for (j = 0; j < ss->length; j++) {
1997-05-15 12:00:00 +08:00
cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir]);
2002-10-13 11:35:53 +08:00
(void) addch((chtype) ss->symbol);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
if (awinna())
1997-05-15 12:00:00 +08:00
++cpuwon;
else
++plywon;
j = 18 + strlen(name);
2002-10-13 11:35:53 +08:00
if (plywon >= 10)
1997-05-15 12:00:00 +08:00
++j;
2002-10-13 11:35:53 +08:00
if (cpuwon >= 10)
1997-05-15 12:00:00 +08:00
++j;
2002-10-13 11:35:53 +08:00
(void) mvprintw(1, (COLWIDTH - j) / 2,
"%s: %d Computer: %d", name, plywon, cpuwon);
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
prompt(2, (awinna())? "Want to be humiliated again, %s [yn]? "
: "Going to give me a chance for revenge, %s [yn]? ", name);
return (sgetc("YN") == 'Y');
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
static void
do_options(int c, char *op[])
1997-05-15 12:00:00 +08:00
{
register int i;
2002-10-13 11:35:53 +08:00
if (c > 1) {
for (i = 1; i < c; i++) {
switch (op[i][0]) {
1997-05-15 12:00:00 +08:00
default:
case '?':
(void) fprintf(stderr, "Usage: battle [-s | -b] [-c]\n");
(void) fprintf(stderr, "\tWhere the options are:\n");
(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");
2002-10-13 11:35:53 +08:00
ExitProgram(EXIT_FAILURE);
1997-05-15 12:00:00 +08:00
break;
case '-':
2002-10-13 11:35:53 +08:00
switch (op[i][1]) {
1997-05-15 12:00:00 +08:00
case 'b':
blitz = 1;
2002-10-13 11:35:53 +08:00
if (salvo == 1) {
1997-05-15 12:00:00 +08:00
(void) fprintf(stderr,
2002-10-13 11:35:53 +08:00
"Bad Arg: -b and -s are mutually exclusive\n");
ExitProgram(EXIT_FAILURE);
1997-05-15 12:00:00 +08:00
}
break;
case 's':
salvo = 1;
2002-10-13 11:35:53 +08:00
if (blitz == 1) {
1997-05-15 12:00:00 +08:00
(void) fprintf(stderr,
2002-10-13 11:35:53 +08:00
"Bad Arg: -s and -b are mutually exclusive\n");
ExitProgram(EXIT_FAILURE);
1997-05-15 12:00:00 +08:00
}
break;
case 'c':
closepack = 1;
break;
default:
(void) fprintf(stderr,
2002-10-13 11:35:53 +08:00
"Bad arg: type \"%s ?\" for usage message\n",
op[0]);
ExitProgram(EXIT_FAILURE);
1997-05-15 12:00:00 +08:00
}
}
}
}
}
2002-10-13 11:35:53 +08:00
static int
scount(int who)
1997-05-15 12:00:00 +08:00
{
register int i, shots;
register ship_t *sp;
if (who)
2002-10-13 11:35:53 +08:00
sp = cpuship; /* count cpu shots */
1997-05-15 12:00:00 +08:00
else
2002-10-13 11:35:53 +08:00
sp = plyship; /* count player shots */
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
for (i = 0, shots = 0; i < SHIPTYPES; i++, sp++) {
1997-05-15 12:00:00 +08:00
if (sp->hits >= sp->length)
continue; /* dead ship */
else
shots++;
}
2002-10-13 11:35:53 +08:00
return (shots);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
int
main(int argc, char *argv[])
1997-05-15 12:00:00 +08:00
{
2002-10-13 11:35:53 +08:00
setlocale(LC_ALL, "");
1997-05-15 12:00:00 +08:00
do_options(argc, argv);
intro();
do {
initgame();
2002-10-13 11:35:53 +08:00
while (awinna() == -1) {
if (!blitz) {
if (!salvo) {
if (turn)
1997-05-15 12:00:00 +08:00
(void) cputurn();
else
(void) plyturn();
2002-10-13 11:35:53 +08:00
} else {
1997-05-15 12:00:00 +08:00
register int i;
i = scount(turn);
2002-10-13 11:35:53 +08:00
while (i--) {
if (turn) {
1997-05-15 12:00:00 +08:00
if (cputurn() && awinna() != -1)
i = 0;
2002-10-13 11:35:53 +08:00
} else {
1997-05-15 12:00:00 +08:00
if (plyturn() && awinna() != -1)
i = 0;
}
}
2002-10-13 11:35:53 +08:00
}
} else
2006-12-18 12:32:42 +08:00
while ((turn ? cputurn() : plyturn()) && awinna() == -1)
1997-05-15 12:00:00 +08:00
continue;
turn = OTHER;
}
} while
(playagain());
uninitgame(0);
2002-10-13 11:35:53 +08:00
/*NOTREACHED */
1997-05-15 12:00:00 +08:00
}
/* bs.c ends here */