| File: | src/games/bs/bs.c |
| Warning: | line 668, column 12 The left operand of '%' is a garbage value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* $OpenBSD: bs.c,v 1.42 2021/10/23 11:22:48 mestre Exp $ */ | |||
| 2 | /* | |||
| 3 | * Copyright (c) 1986, Bruce Holloway | |||
| 4 | * All rights reserved. | |||
| 5 | * | |||
| 6 | * Redistribution and use in source and binary forms, with or without | |||
| 7 | * modification, are permitted provided that the following conditions are | |||
| 8 | * met: | |||
| 9 | * | |||
| 10 | * - Redistributions of source code must retain the above copyright | |||
| 11 | * notice, this list of conditions and the following disclaimer. | |||
| 12 | * - Redistributions in binary form must reproduce the above copyright | |||
| 13 | * notice, this list of conditions and the following disclaimer in the | |||
| 14 | * documentation and/or other materials provided with the distribution. | |||
| 15 | * - Neither the name of the <ORGANIZATION> nor the names of its | |||
| 16 | * contributors may be used to endorse or promote products derived from | |||
| 17 | * this software without specific prior written permission. | |||
| 18 | * | |||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | |||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |||
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |||
| 22 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |||
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |||
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |||
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |||
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
| 30 | */ | |||
| 31 | /* | |||
| 32 | * bs.c - original author: Bruce Holloway | |||
| 33 | * salvo option by: Chuck A DeGaul | |||
| 34 | * with improved user interface, autoconfiguration and code cleanup | |||
| 35 | * by Eric S. Raymond <esr@snark.thyrsus.com> | |||
| 36 | * v1.2 with color support and minor portability fixes, November 1990 | |||
| 37 | * v2.0 featuring strict ANSI/POSIX conformance, November 1993. | |||
| 38 | * v2.1 with ncurses mouse support, September 1995 | |||
| 39 | * v2.2 with bugfixes and strategical improvements, March 1998. | |||
| 40 | */ | |||
| 41 | ||||
| 42 | #include <ctype.h> | |||
| 43 | #include <curses.h> | |||
| 44 | #include <err.h> | |||
| 45 | #include <limits.h> | |||
| 46 | #include <signal.h> | |||
| 47 | #include <stdlib.h> | |||
| 48 | #include <string.h> | |||
| 49 | #include <unistd.h> | |||
| 50 | ||||
| 51 | typedef struct { | |||
| 52 | char *name; /* name of the ship type */ | |||
| 53 | char hits; /* how many times has this ship been hit? */ | |||
| 54 | char symbol; /* symbol for game purposes */ | |||
| 55 | char length; /* length of ship */ | |||
| 56 | signed char x, y; /* coordinates of ship start point */ | |||
| 57 | unsigned char dir; /* direction of `bow' */ | |||
| 58 | bool_Bool placed; /* has it been placed on the board? */ | |||
| 59 | } ship_t; | |||
| 60 | ||||
| 61 | static void announceopts(void); | |||
| 62 | static int awinna(void); | |||
| 63 | static bool_Bool checkplace(int, ship_t *, int); | |||
| 64 | static int collidecheck(int, int, int); | |||
| 65 | static int cpufire(int, int); | |||
| 66 | static bool_Bool cpushipcanfit(int, int, int, int); | |||
| 67 | static int cputurn(void); | |||
| 68 | static void do_options(int, char *[]); | |||
| 69 | static void error(char *); | |||
| 70 | static int getcoord(int); | |||
| 71 | static ship_t *hitship(int, int); | |||
| 72 | static void initgame(void); | |||
| 73 | static void intro(void); | |||
| 74 | static void placeship(int, ship_t *, int); | |||
| 75 | static int playagain(void); | |||
| 76 | static int plyturn(void); | |||
| 77 | static void prompt(int, const char *, ...) | |||
| 78 | __attribute__((__format__ (printf, 2, 3))); | |||
| 79 | static void randomfire(int *, int *); | |||
| 80 | static void randomplace(int, ship_t *); | |||
| 81 | static int rnd(int); | |||
| 82 | static int scount(int); | |||
| 83 | static int sgetc(char *); | |||
| 84 | __dead__attribute__((__noreturn__)) static void uninitgame(int); | |||
| 85 | __dead__attribute__((__noreturn__)) void usage(void); | |||
| 86 | ||||
| 87 | /* | |||
| 88 | * Constants for tuning the random-fire algorithm. It prefers moves that | |||
| 89 | * diagonal-stripe the board with a stripe separation of srchstep. If | |||
| 90 | * no such preferred moves are found, srchstep is decremented. | |||
| 91 | */ | |||
| 92 | #define BEGINSTEP3 3 /* initial value of srchstep */ | |||
| 93 | ||||
| 94 | /* miscellaneous constants */ | |||
| 95 | #define SHIPTYPES5 5 | |||
| 96 | #define OTHER(1-turn) (1-turn) | |||
| 97 | #define PLAYER0 0 | |||
| 98 | #define COMPUTER1 1 | |||
| 99 | #define MARK_HIT'H' 'H' | |||
| 100 | #define MARK_MISS'o' 'o' | |||
| 101 | #define CTRLC'\003' '\003' /* used as terminate command */ | |||
| 102 | #define FF'\014' '\014' /* used as redraw command */ | |||
| 103 | ||||
| 104 | /* coordinate handling */ | |||
| 105 | #define BWIDTH10 10 | |||
| 106 | #define BDEPTH10 10 | |||
| 107 | ||||
| 108 | /* display symbols */ | |||
| 109 | #define SHOWHIT'*' '*' | |||
| 110 | #define SHOWSPLASH' ' ' ' | |||
| 111 | #define IS_SHIP(c)isupper(c) isupper(c) | |||
| 112 | ||||
| 113 | /* how to position us on player board */ | |||
| 114 | #define PYBASE3 3 | |||
| 115 | #define PXBASE3 3 | |||
| 116 | #define PY(y)(3 + (y)) (PYBASE3 + (y)) | |||
| 117 | #define PX(x)(3 + (x)*3) (PXBASE3 + (x)*3) | |||
| 118 | #define pgoto(y, x)(void)wmove(stdscr,(3 + (y)),(3 + (x)*3)) (void)move(PY(y), PX(x))wmove(stdscr,(3 + (y)),(3 + (x)*3)) | |||
| 119 | ||||
| 120 | /* how to position us on cpu board */ | |||
| 121 | #define CYBASE3 3 | |||
| 122 | #define CXBASE48 48 | |||
| 123 | #define CY(y)(3 + (y)) (CYBASE3 + (y)) | |||
| 124 | #define CX(x)(48 + (x)*3) (CXBASE48 + (x)*3) | |||
| 125 | #define CYINV(y)((y) - 3) ((y) - CYBASE3) | |||
| 126 | #define CXINV(x)(((x) - 48) / 3) (((x) - CXBASE48) / 3) | |||
| 127 | #define cgoto(y, x)(void)wmove(stdscr,(3 + (y)),(48 + (x)*3)) (void)move(CY(y), CX(x))wmove(stdscr,(3 + (y)),(48 + (x)*3)) | |||
| 128 | ||||
| 129 | #define ONBOARD(x, y)(x >= 0 && x < 10 && y >= 0 && y < 10) (x >= 0 && x < BWIDTH10 && y >= 0 && y < BDEPTH10) | |||
| 130 | ||||
| 131 | /* other board locations */ | |||
| 132 | #define COLWIDTH80 80 | |||
| 133 | #define PROMPTLINE21 21 /* prompt line */ | |||
| 134 | #define SYBASE3 + 10 + 3 CYBASE3 + BDEPTH10 + 3 /* move key diagram */ | |||
| 135 | #define SXBASE63 63 | |||
| 136 | #define MYBASE3 + 10 + 3 - 1 SYBASE3 + 10 + 3 - 1 /* diagram caption */ | |||
| 137 | #define MXBASE64 64 | |||
| 138 | #define HYBASE3 + 10 + 3 - 1 SYBASE3 + 10 + 3 - 1 /* help area */ | |||
| 139 | #define HXBASE0 0 | |||
| 140 | ||||
| 141 | /* this will need to be changed if BWIDTH changes */ | |||
| 142 | static char numbers[] = " 0 1 2 3 4 5 6 7 8 9"; | |||
| 143 | ||||
| 144 | static char carrier[] = "Aircraft Carrier"; | |||
| 145 | static char battle[] = "Battleship"; | |||
| 146 | static char sub[] = "Submarine"; | |||
| 147 | static char destroy[] = "Destroyer"; | |||
| 148 | static char ptboat[] = "PT Boat"; | |||
| 149 | ||||
| 150 | static char name[LOGIN_NAME_MAX32]; | |||
| 151 | static char dftname[] = "stranger"; | |||
| 152 | ||||
| 153 | /* direction constants */ | |||
| 154 | #define E0 0 | |||
| 155 | #define SE1 1 | |||
| 156 | #define S2 2 | |||
| 157 | #define SW3 3 | |||
| 158 | #define W4 4 | |||
| 159 | #define NW5 5 | |||
| 160 | #define N6 6 | |||
| 161 | #define NE7 7 | |||
| 162 | static int xincr[8] = { 1, 1, 0, -1, -1, -1, 0, 1 }; | |||
| 163 | static int yincr[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }; | |||
| 164 | ||||
| 165 | /* current ship position and direction */ | |||
| 166 | static int curx = (BWIDTH10 / 2); | |||
| 167 | static int cury = (BDEPTH10 / 2); | |||
| 168 | ||||
| 169 | ship_t plyship[SHIPTYPES5] = | |||
| 170 | { | |||
| 171 | { carrier, 0, 'A', 5, 0, 0, 0, FALSE0 }, | |||
| 172 | { battle, 0, 'B', 4, 0, 0, 0, FALSE0 }, | |||
| 173 | { destroy, 0, 'D', 3, 0, 0, 0, FALSE0 }, | |||
| 174 | { sub, 0, 'S', 3, 0, 0, 0, FALSE0 }, | |||
| 175 | { ptboat, 0, 'P', 2, 0, 0, 0, FALSE0 } | |||
| 176 | }; | |||
| 177 | ||||
| 178 | ship_t cpuship[SHIPTYPES5] = | |||
| 179 | { | |||
| 180 | { carrier, 0, 'A', 5, 0, 0, 0, FALSE0 }, | |||
| 181 | { battle, 0, 'B', 4, 0, 0, 0, FALSE0 }, | |||
| 182 | { destroy, 0, 'D', 3, 0, 0, 0, FALSE0 }, | |||
| 183 | { sub, 0, 'S', 3, 0, 0, 0, FALSE0 }, | |||
| 184 | { ptboat, 0, 'P', 2, 0, 0, 0, FALSE0 } | |||
| 185 | }; | |||
| 186 | ||||
| 187 | /* The following variables (and associated defines), used for computer | |||
| 188 | * targetting, must be global so that they can be reset for each new game | |||
| 189 | * played without restarting the program. | |||
| 190 | */ | |||
| 191 | #define POSSIBLE(x, y)((x >= 0 && x < 10 && y >= 0 && y < 10) && !hits[1][x][y]) (ONBOARD(x, y)(x >= 0 && x < 10 && y >= 0 && y < 10) && !hits[COMPUTER1][x][y]) | |||
| 192 | #define RANDOM_FIRE0 0 | |||
| 193 | #define RANDOM_HIT1 1 | |||
| 194 | #define HUNT_DIRECT2 2 | |||
| 195 | #define FIRST_PASS3 3 | |||
| 196 | #define REVERSE_JUMP4 4 | |||
| 197 | #define SECOND_PASS5 5 | |||
| 198 | static int next = RANDOM_FIRE0; | |||
| 199 | static int turncount = 0; | |||
| 200 | static int srchstep = BEGINSTEP3; | |||
| 201 | /* Computer needs to keep track of longest and shortest player ships still | |||
| 202 | * not sunk, for better targetting. | |||
| 203 | */ | |||
| 204 | static int cpushortest; | |||
| 205 | static int cpulongest; | |||
| 206 | ||||
| 207 | /* "Hits" board, and main board. */ | |||
| 208 | static char hits[2][BWIDTH10][BDEPTH10], board[2][BWIDTH10][BDEPTH10]; | |||
| 209 | ||||
| 210 | static int turn; /* 0=player, 1=computer */ | |||
| 211 | static int plywon=0, cpuwon=0; /* How many games has each won? */ | |||
| 212 | ||||
| 213 | static int salvo, blitz, closepack; | |||
| 214 | ||||
| 215 | /* end the game, either normally or due to signal */ | |||
| 216 | static void | |||
| 217 | uninitgame(int sig) | |||
| 218 | { | |||
| 219 | clear()wclear(stdscr); | |||
| 220 | (void)refresh()wrefresh(stdscr); | |||
| 221 | (void)resetterm()reset_shell_mode(); | |||
| 222 | (void)echo(); | |||
| 223 | (void)endwin(); | |||
| 224 | exit(sig); | |||
| 225 | } | |||
| 226 | ||||
| 227 | /* announce which game options are enabled */ | |||
| 228 | static void | |||
| 229 | announceopts(void) | |||
| 230 | { | |||
| 231 | if (salvo || blitz || closepack) | |||
| 232 | { | |||
| 233 | (void) printw("Playing optional game ("); | |||
| 234 | if (salvo) | |||
| 235 | (void) printw("salvo, "); | |||
| 236 | else | |||
| 237 | (void) printw("nosalvo, "); | |||
| 238 | if (blitz) | |||
| 239 | (void) printw("blitz "); | |||
| 240 | else | |||
| 241 | (void) printw("noblitz, "); | |||
| 242 | if (closepack) | |||
| 243 | (void) printw("closepack)"); | |||
| 244 | else | |||
| 245 | (void) printw("noclosepack)"); | |||
| 246 | } | |||
| 247 | else | |||
| 248 | (void) printw( | |||
| 249 | "Playing standard game (noblitz, nosalvo, noclosepack)"); | |||
| 250 | } | |||
| 251 | ||||
| 252 | static void | |||
| 253 | intro(void) | |||
| 254 | { | |||
| 255 | char *tmpname; | |||
| 256 | ||||
| 257 | (void) signal(SIGINT2,uninitgame); | |||
| 258 | if(signal(SIGQUIT3,SIG_IGN(void (*)(int))1) != SIG_IGN(void (*)(int))1) | |||
| 259 | (void)signal(SIGQUIT3,uninitgame); | |||
| 260 | ||||
| 261 | if ((tmpname = getlogin()) != NULL((void *)0)) | |||
| 262 | { | |||
| 263 | (void)strlcpy(name, tmpname, sizeof(name)); | |||
| 264 | name[0] = toupper((unsigned char)name[0]); | |||
| 265 | } | |||
| 266 | else | |||
| 267 | (void)strlcpy(name, dftname, sizeof(name)); | |||
| 268 | ||||
| 269 | (void)initscr(); | |||
| 270 | keypad(stdscr, TRUE1); | |||
| 271 | (void)saveterm()def_prog_mode(); | |||
| 272 | (void)nonl(); | |||
| 273 | (void)cbreak(); | |||
| 274 | (void)noecho(); | |||
| 275 | ||||
| 276 | if ((LINES < PROMPTLINE21 + 3) || (COLS < COLWIDTH80)) { | |||
| 277 | endwin(); | |||
| 278 | errx(1, "screen must be at least %dx%d.", PROMPTLINE21 + 3, COLWIDTH80); | |||
| 279 | } | |||
| 280 | ||||
| 281 | #define PR(void)waddnstr(stdscr,void,-1) (void)addstrwaddnstr(stdscr,void,-1) | |||
| 282 | (void)waddnstr(stdscr,void,-1)clear()wclear(stdscr); | |||
| 283 | (void)mvaddstr(4,29,"Welcome to Battleship!")(wmove(stdscr,4,29) == (-1) ? (-1) : waddnstr(stdscr,"Welcome to Battleship!" ,-1)); | |||
| 284 | (void)move(8,0)wmove(stdscr,8,0); | |||
| 285 | PR(void)waddnstr(stdscr," \\\n" ,-1)(" \\\n"); | |||
| 286 | PR(void)waddnstr(stdscr," \\ \\ \\\n" ,-1)(" \\ \\ \\\n"); | |||
| 287 | PR(void)waddnstr(stdscr," \\ \\ \\ \\ \\_____________\n" ,-1)(" \\ \\ \\ \\ \\_____________\n"); | |||
| 288 | PR(void)waddnstr(stdscr," \\ \\ \\_____________ \\ \\/ |\n" ,-1)(" \\ \\ \\_____________ \\ \\/ |\n"); | |||
| 289 | PR(void)waddnstr(stdscr," \\ \\/ \\__/ \\ \\/ |\n" ,-1)(" \\ \\/ \\__/ \\ \\/ |\n"); | |||
| 290 | PR(void)waddnstr(stdscr," \\/ \\/ \\/ \\_____/ |__\n" ,-1)(" \\/ \\/ \\/ \\_____/ |__\n"); | |||
| 291 | PR(void)waddnstr(stdscr," ________________/ /\\/ ..\\/ |\n" ,-1)(" ________________/ /\\/ ..\\/ |\n"); | |||
| 292 | PR(void)waddnstr(stdscr," \\ S.S. Puffy \\/\\___o/ |\n" ,-1)(" \\ S.S. Puffy \\/\\___o/ |\n"); | |||
| 293 | PR(void)waddnstr(stdscr," \\ / /\\ \\ /\n" ,-1)(" \\ / /\\ \\ /\n"); | |||
| 294 | PR(void)waddnstr(stdscr," \\___________________________________________________/\n" ,-1)(" \\___________________________________________________/\n"); | |||
| 295 | ||||
| 296 | (void) mvaddstr(22,27,"Hit any key to continue...")(wmove(stdscr,22,27) == (-1) ? (-1) : waddnstr(stdscr,"Hit any key to continue..." ,-1)); (void)refresh()wrefresh(stdscr); | |||
| 297 | (void) getch()wgetch(stdscr); | |||
| 298 | ||||
| 299 | start_color(); | |||
| 300 | ||||
| 301 | init_pair(COLOR_BLACK0, COLOR_BLACK0, COLOR_BLACK0); | |||
| 302 | init_pair(COLOR_GREEN2, COLOR_GREEN2, COLOR_BLACK0); | |||
| 303 | init_pair(COLOR_RED1, COLOR_RED1, COLOR_BLACK0); | |||
| 304 | init_pair(COLOR_CYAN6, COLOR_CYAN6, COLOR_BLACK0); | |||
| 305 | init_pair(COLOR_WHITE7, COLOR_WHITE7, COLOR_BLACK0); | |||
| 306 | init_pair(COLOR_MAGENTA5, COLOR_MAGENTA5, COLOR_BLACK0); | |||
| 307 | init_pair(COLOR_BLUE4, COLOR_BLUE4, COLOR_BLACK0); | |||
| 308 | init_pair(COLOR_YELLOW3, COLOR_YELLOW3, COLOR_BLACK0); | |||
| 309 | ||||
| 310 | (void) mousemask(BUTTON1_CLICKED((004L) << (((1) - 1) * 6)), (mmask_t *)NULL((void *)0)); | |||
| 311 | } | |||
| 312 | ||||
| 313 | /* print a message at the prompt line */ | |||
| 314 | static void | |||
| 315 | prompt(int n, const char *f, ...) | |||
| 316 | { | |||
| 317 | va_list va; | |||
| 318 | ||||
| 319 | (void) move(PROMPTLINE + n, 0)wmove(stdscr,21 + n,0); | |||
| 320 | (void) clrtoeol()wclrtoeol(stdscr); | |||
| 321 | va_start(va, f)__builtin_va_start(va, f); | |||
| 322 | (void) vw_printwvwprintw(stdscr, f, va); | |||
| 323 | va_end(va)__builtin_va_end(va); | |||
| 324 | (void) refresh()wrefresh(stdscr); | |||
| 325 | } | |||
| 326 | ||||
| 327 | static void | |||
| 328 | error(char *s) | |||
| 329 | { | |||
| 330 | (void) move(PROMPTLINE + 2, 0)wmove(stdscr,21 + 2,0); | |||
| 331 | (void) clrtoeol()wclrtoeol(stdscr); | |||
| 332 | if (s) | |||
| 333 | { | |||
| 334 | (void) addstr(s)waddnstr(stdscr,s,-1); | |||
| 335 | (void) beep(); | |||
| 336 | } | |||
| 337 | } | |||
| 338 | ||||
| 339 | static void | |||
| 340 | placeship(int b, ship_t *ss, int vis) | |||
| 341 | { | |||
| 342 | int l; | |||
| 343 | ||||
| 344 | for(l = 0; l < ss->length; ++l) | |||
| 345 | { | |||
| 346 | int newx = ss->x + l * xincr[ss->dir]; | |||
| 347 | int newy = ss->y + l * yincr[ss->dir]; | |||
| 348 | ||||
| 349 | board[b][newx][newy] = ss->symbol; | |||
| 350 | if (vis) | |||
| 351 | { | |||
| 352 | pgoto(newy, newx)(void)wmove(stdscr,(3 + (newy)),(3 + (newx)*3)); | |||
| 353 | (void) addch((chtype)ss->symbol)waddch(stdscr,(chtype)ss->symbol); | |||
| 354 | } | |||
| 355 | } | |||
| 356 | ss->hits = 0; | |||
| 357 | } | |||
| 358 | ||||
| 359 | static int | |||
| 360 | rnd(int n) | |||
| 361 | { | |||
| 362 | return(arc4random_uniform(n)); | |||
| 363 | } | |||
| 364 | ||||
| 365 | /* generate a valid random ship placement into px,py */ | |||
| 366 | static void | |||
| 367 | randomplace(int b, ship_t *ss) | |||
| 368 | { | |||
| 369 | do { | |||
| 370 | ss->dir = rnd(2) ? E0 : S2; | |||
| 371 | ss->x = rnd(BWIDTH10 - (ss->dir == E0 ? ss->length : 0)); | |||
| 372 | ss->y = rnd(BDEPTH10 - (ss->dir == S2 ? ss->length : 0)); | |||
| 373 | } while | |||
| 374 | (!checkplace(b, ss, FALSE0)); | |||
| 375 | } | |||
| 376 | ||||
| 377 | static void | |||
| 378 | initgame(void) | |||
| 379 | { | |||
| 380 | int i, j, unplaced; | |||
| 381 | ship_t *ss; | |||
| 382 | ||||
| 383 | (void) clear()wclear(stdscr); | |||
| 384 | (void) mvaddstr(0,35,"BATTLESHIPS")(wmove(stdscr,0,35) == (-1) ? (-1) : waddnstr(stdscr,"BATTLESHIPS" ,-1)); | |||
| 385 | (void) move(PROMPTLINE + 2, 0)wmove(stdscr,21 + 2,0); | |||
| 386 | announceopts(); | |||
| 387 | ||||
| 388 | /* Set up global CPU algorithm variables. */ | |||
| 389 | next = RANDOM_FIRE0; | |||
| 390 | turncount = 0; | |||
| 391 | srchstep = BEGINSTEP3; | |||
| 392 | /* set up cpulongest and cpushortest (computer targetting variables) */ | |||
| 393 | cpushortest = cpulongest = cpuship->length; | |||
| 394 | ||||
| 395 | memset(board, 0, sizeof(char) * BWIDTH10 * BDEPTH10 * 2); | |||
| 396 | memset(hits, 0, sizeof(char) * BWIDTH10 * BDEPTH10 * 2); | |||
| 397 | for (i = 0; i < SHIPTYPES5; i++) | |||
| 398 | { | |||
| 399 | ss = cpuship + i; | |||
| 400 | ss->x = ss->y = ss->dir = ss->hits = 0; | |||
| 401 | ss->placed = FALSE0; | |||
| 402 | ss = plyship + i; | |||
| 403 | ss->x = ss->y = ss->dir = ss->hits = 0; | |||
| 404 | ss->placed = FALSE0; | |||
| 405 | ||||
| 406 | if (ss->length > cpulongest) | |||
| 407 | cpulongest = ss->length; | |||
| 408 | if (ss->length < cpushortest) | |||
| 409 | cpushortest = ss->length; | |||
| 410 | } | |||
| 411 | ||||
| 412 | /* draw empty boards */ | |||
| 413 | (void) mvaddstr(PYBASE - 2, PXBASE + 5, "Main Board")(wmove(stdscr,3 - 2,3 + 5) == (-1) ? (-1) : waddnstr(stdscr,"Main Board" ,-1)); | |||
| 414 | (void) mvaddstr(PYBASE - 1, PXBASE - 3,numbers)(wmove(stdscr,3 - 1,3 - 3) == (-1) ? (-1) : waddnstr(stdscr,numbers ,-1)); | |||
| 415 | for(i=0; i < BDEPTH10; ++i) | |||
| 416 | { | |||
| 417 | (void) mvaddch(PYBASE + i, PXBASE - 3, (chtype)(i + 'A'))(wmove(stdscr,3 + i,3 - 3) == (-1) ? (-1) : waddch(stdscr,(chtype )(i + 'A'))); | |||
| 418 | if (has_colors()) | |||
| 419 | attron(COLOR_PAIR(COLOR_BLUE))wattr_on(stdscr, (attr_t)(((4) << ((0) + 8))), ((void * )0)); | |||
| 420 | (void) addch(' ')waddch(stdscr,' '); | |||
| 421 | for (j = 0; j < BWIDTH10; j++) | |||
| 422 | (void) addstr(" . ")waddnstr(stdscr," . ",-1); | |||
| 423 | attrset(0)((stdscr)->_attrs = (0)); | |||
| 424 | (void) addch(' ')waddch(stdscr,' '); | |||
| 425 | (void) addch((chtype)(i + 'A'))waddch(stdscr,(chtype)(i + 'A')); | |||
| 426 | } | |||
| 427 | (void) mvaddstr(PYBASE + BDEPTH, PXBASE - 3,numbers)(wmove(stdscr,3 + 10,3 - 3) == (-1) ? (-1) : waddnstr(stdscr, numbers,-1)); | |||
| 428 | (void) mvaddstr(CYBASE - 2, CXBASE + 7,"Hit/Miss Board")(wmove(stdscr,3 - 2,48 + 7) == (-1) ? (-1) : waddnstr(stdscr, "Hit/Miss Board",-1)); | |||
| 429 | (void) mvaddstr(CYBASE - 1, CXBASE - 3, numbers)(wmove(stdscr,3 - 1,48 - 3) == (-1) ? (-1) : waddnstr(stdscr, numbers,-1)); | |||
| 430 | for(i=0; i < BDEPTH10; ++i) | |||
| 431 | { | |||
| 432 | (void) mvaddch(CYBASE + i, CXBASE - 3, (chtype)(i + 'A'))(wmove(stdscr,3 + i,48 - 3) == (-1) ? (-1) : waddch(stdscr,(chtype )(i + 'A'))); | |||
| 433 | if (has_colors()) | |||
| 434 | attron(COLOR_PAIR(COLOR_BLUE))wattr_on(stdscr, (attr_t)(((4) << ((0) + 8))), ((void * )0)); | |||
| 435 | (void) addch(' ')waddch(stdscr,' '); | |||
| 436 | for (j = 0; j < BWIDTH10; j++) | |||
| 437 | (void) addstr(" . ")waddnstr(stdscr," . ",-1); | |||
| 438 | attrset(0)((stdscr)->_attrs = (0)); | |||
| 439 | (void) addch(' ')waddch(stdscr,' '); | |||
| 440 | (void) addch((chtype)(i + 'A'))waddch(stdscr,(chtype)(i + 'A')); | |||
| 441 | } | |||
| 442 | ||||
| 443 | (void) mvaddstr(CYBASE + BDEPTH,CXBASE - 3,numbers)(wmove(stdscr,3 + 10,48 - 3) == (-1) ? (-1) : waddnstr(stdscr ,numbers,-1)); | |||
| 444 | ||||
| 445 | (void) mvprintw(HYBASE3 + 10 + 3 - 1, HXBASE0, | |||
| 446 | "To position your ships: move the cursor to a spot, then"); | |||
| 447 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+1,HXBASE0, | |||
| 448 | "type the first letter of a ship type to select it, then"); | |||
| 449 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+2,HXBASE0, | |||
| 450 | "type a direction ([hjkl] or [4862]), indicating how the"); | |||
| 451 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+3,HXBASE0, | |||
| 452 | "ship should be pointed. You may also type a ship letter"); | |||
| 453 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+4,HXBASE0, | |||
| 454 | "followed by `r' to position it randomly, or type `R' to"); | |||
| 455 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+5,HXBASE0, | |||
| 456 | "place all remaining ships randomly."); | |||
| 457 | ||||
| 458 | (void) mvaddstr(MYBASE, MXBASE, "Aiming keys:")(wmove(stdscr,3 + 10 + 3 - 1,64) == (-1) ? (-1) : waddnstr(stdscr ,"Aiming keys:",-1)); | |||
| 459 | (void) mvaddstr(SYBASE, SXBASE, "y k u 7 8 9")(wmove(stdscr,3 + 10 + 3,63) == (-1) ? (-1) : waddnstr(stdscr ,"y k u 7 8 9",-1)); | |||
| 460 | (void) mvaddstr(SYBASE+1, SXBASE, " \\|/ \\|/ ")(wmove(stdscr,3 + 10 + 3 +1,63) == (-1) ? (-1) : waddnstr(stdscr ," \\|/ \\|/ ",-1)); | |||
| 461 | (void) mvaddstr(SYBASE+2, SXBASE, "h-+-l 4-+-6")(wmove(stdscr,3 + 10 + 3 +2,63) == (-1) ? (-1) : waddnstr(stdscr ,"h-+-l 4-+-6",-1)); | |||
| 462 | (void) mvaddstr(SYBASE+3, SXBASE, " /|\\ /|\\ ")(wmove(stdscr,3 + 10 + 3 +3,63) == (-1) ? (-1) : waddnstr(stdscr ," /|\\ /|\\ ",-1)); | |||
| 463 | (void) mvaddstr(SYBASE+4, SXBASE, "b j n 1 2 3")(wmove(stdscr,3 + 10 + 3 +4,63) == (-1) ? (-1) : waddnstr(stdscr ,"b j n 1 2 3",-1)); | |||
| 464 | ||||
| 465 | /* have the computer place ships */ | |||
| 466 | for(ss = cpuship; ss < cpuship + SHIPTYPES5; ss++) | |||
| 467 | { | |||
| 468 | randomplace(COMPUTER1, ss); | |||
| 469 | placeship(COMPUTER1, ss, FALSE0); | |||
| 470 | } | |||
| 471 | ||||
| 472 | ss = (ship_t *)NULL((void *)0); | |||
| 473 | do { | |||
| 474 | char docked[SHIPTYPES5 + 2], *cp = docked; | |||
| 475 | int c; | |||
| 476 | ||||
| 477 | /* figure which ships still wait to be placed */ | |||
| 478 | *cp++ = 'R'; | |||
| 479 | for (i = 0; i < SHIPTYPES5; i++) | |||
| 480 | if (!plyship[i].placed) | |||
| 481 | *cp++ = plyship[i].symbol; | |||
| 482 | *cp = '\0'; | |||
| 483 | ||||
| 484 | /* get a command letter */ | |||
| 485 | prompt(1, "Type one of [%s] to pick a ship.", docked+1); | |||
| 486 | do { | |||
| 487 | c = getcoord(PLAYER0); | |||
| 488 | } while | |||
| 489 | (!strchr(docked, c)); | |||
| 490 | ||||
| 491 | if (c == 'R') | |||
| 492 | (void) ungetch('R'); | |||
| 493 | else | |||
| 494 | { | |||
| 495 | /* map that into the corresponding symbol */ | |||
| 496 | for (ss = plyship; ss < plyship + SHIPTYPES5; ss++) | |||
| 497 | if (ss->symbol == c) | |||
| 498 | break; | |||
| 499 | ||||
| 500 | prompt(1, "Type one of [hjklrR] to place your %s.", ss->name); | |||
| 501 | pgoto(cury, curx)(void)wmove(stdscr,(3 + (cury)),(3 + (curx)*3)); | |||
| 502 | } | |||
| 503 | regetchar: | |||
| 504 | c = getch()wgetch(stdscr); | |||
| 505 | switch (c) { | |||
| 506 | case FF'\014': | |||
| 507 | (void)clearok(stdscr, TRUE1); | |||
| 508 | (void)refresh()wrefresh(stdscr); | |||
| 509 | break; | |||
| 510 | case 'r': | |||
| 511 | prompt(1, "Random-placing your %s", ss->name); | |||
| 512 | randomplace(PLAYER0, ss); | |||
| 513 | placeship(PLAYER0, ss, TRUE1); | |||
| 514 | error(NULL((void *)0)); | |||
| 515 | ss->placed = TRUE1; | |||
| 516 | break; | |||
| 517 | case 'R': | |||
| 518 | prompt(1, "Placing the rest of your fleet at random..."); | |||
| 519 | for (ss = plyship; ss < plyship + SHIPTYPES5; ss++) | |||
| 520 | if (!ss->placed) | |||
| 521 | { | |||
| 522 | randomplace(PLAYER0, ss); | |||
| 523 | placeship(PLAYER0, ss, TRUE1); | |||
| 524 | ss->placed = TRUE1; | |||
| 525 | } | |||
| 526 | error(NULL((void *)0)); | |||
| 527 | break; | |||
| 528 | ||||
| 529 | case 'k': case 'j': case 'h': case 'l': | |||
| 530 | case '8': case '2': case '4': case '6': | |||
| 531 | case KEY_LEFT0404: case KEY_RIGHT0405: case KEY_UP0403: case KEY_DOWN0402: | |||
| 532 | ss->x = curx; | |||
| 533 | ss->y = cury; | |||
| 534 | ||||
| 535 | switch(c) | |||
| 536 | { | |||
| 537 | case 'k': case '8': case KEY_UP0403: ss->dir = N6; break; | |||
| 538 | case 'j': case '2': case KEY_DOWN0402: ss->dir = S2; break; | |||
| 539 | case 'h': case '4': case KEY_LEFT0404: ss->dir = W4; break; | |||
| 540 | case 'l': case '6': case KEY_RIGHT0405: ss->dir = E0; break; | |||
| 541 | } | |||
| 542 | ||||
| 543 | if (checkplace(PLAYER0, ss, TRUE1)) | |||
| 544 | { | |||
| 545 | placeship(PLAYER0, ss, TRUE1); | |||
| 546 | error(NULL((void *)0)); | |||
| 547 | ss->placed = TRUE1; | |||
| 548 | } | |||
| 549 | break; | |||
| 550 | default: | |||
| 551 | goto regetchar; | |||
| 552 | } | |||
| 553 | ||||
| 554 | for (unplaced = i = 0; i < SHIPTYPES5; i++) | |||
| 555 | unplaced += !plyship[i].placed; | |||
| 556 | } while | |||
| 557 | (unplaced); | |||
| 558 | ||||
| 559 | turn = rnd(2); | |||
| 560 | ||||
| 561 | (void) mvprintw(HYBASE3 + 10 + 3 - 1, HXBASE0, | |||
| 562 | "To fire, move the cursor to your chosen aiming point "); | |||
| 563 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+1, HXBASE0, | |||
| 564 | "and strike any key other than a motion key. "); | |||
| 565 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+2, HXBASE0, | |||
| 566 | " "); | |||
| 567 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+3, HXBASE0, | |||
| 568 | " "); | |||
| 569 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+4, HXBASE0, | |||
| 570 | " "); | |||
| 571 | (void) mvprintw(HYBASE3 + 10 + 3 - 1+5, HXBASE0, | |||
| 572 | " "); | |||
| 573 | ||||
| 574 | (void) prompt(0, "Press any key to start..."); | |||
| 575 | (void) getch()wgetch(stdscr); | |||
| 576 | } | |||
| 577 | ||||
| 578 | static int | |||
| 579 | getcoord(int atcpu) | |||
| 580 | { | |||
| 581 | int ny, nx, c; | |||
| 582 | ||||
| 583 | if (atcpu
| |||
| 584 | cgoto(cury,curx)(void)wmove(stdscr,(3 + (cury)),(48 + (curx)*3)); | |||
| 585 | else | |||
| 586 | pgoto(cury, curx)(void)wmove(stdscr,(3 + (cury)),(3 + (curx)*3)); | |||
| 587 | (void)refresh()wrefresh(stdscr); | |||
| 588 | for (;;) | |||
| 589 | { | |||
| 590 | if (atcpu
| |||
| 591 | { | |||
| 592 | (void) mvprintw(CYBASE3 + BDEPTH10+1, CXBASE48+11, "(%d, %c)", curx, 'A'+cury); | |||
| 593 | cgoto(cury, curx)(void)wmove(stdscr,(3 + (cury)),(48 + (curx)*3)); | |||
| 594 | } | |||
| 595 | else | |||
| 596 | { | |||
| 597 | (void) mvprintw(PYBASE3 + BDEPTH10+1, PXBASE3+11, "(%d, %c)", curx, 'A'+cury); | |||
| 598 | pgoto(cury, curx)(void)wmove(stdscr,(3 + (cury)),(3 + (curx)*3)); | |||
| 599 | } | |||
| 600 | ||||
| 601 | switch(c = getch()wgetch(stdscr)) | |||
| 602 | { | |||
| 603 | case 'k': case '8': | |||
| 604 | case KEY_UP0403: | |||
| 605 | ny = cury+BDEPTH10-1; nx = curx; | |||
| 606 | break; | |||
| 607 | case 'j': case '2': | |||
| 608 | case KEY_DOWN0402: | |||
| 609 | ny = cury+1; nx = curx; | |||
| 610 | break; | |||
| 611 | case 'h': case '4': | |||
| 612 | case KEY_LEFT0404: | |||
| 613 | ny = cury; nx = curx+BWIDTH10-1; | |||
| 614 | break; | |||
| 615 | case 'l': case '6': | |||
| 616 | case KEY_RIGHT0405: | |||
| 617 | ny = cury; nx = curx+1; | |||
| 618 | break; | |||
| 619 | case 'y': case '7': | |||
| 620 | case KEY_A10534: | |||
| 621 | ny = cury+BDEPTH10-1; nx = curx+BWIDTH10-1; | |||
| 622 | break; | |||
| 623 | case 'b': case '1': | |||
| 624 | case KEY_C10537: | |||
| 625 | ny = cury+1; nx = curx+BWIDTH10-1; | |||
| 626 | break; | |||
| 627 | case 'u': case '9': | |||
| 628 | case KEY_A30535: | |||
| 629 | ny = cury+BDEPTH10-1; nx = curx+1; | |||
| 630 | break; | |||
| 631 | case 'n': case '3': | |||
| 632 | case KEY_C30540: | |||
| 633 | ny = cury+1; nx = curx+1; | |||
| 634 | break; | |||
| 635 | case FF'\014': | |||
| 636 | nx = curx; ny = cury; | |||
| 637 | (void)clearok(stdscr, TRUE1); | |||
| 638 | (void)refresh()wrefresh(stdscr); | |||
| 639 | break; | |||
| 640 | case KEY_MOUSE0631: | |||
| 641 | { | |||
| 642 | MEVENT myevent; | |||
| 643 | ||||
| 644 | getmouse(&myevent); | |||
| 645 | if (atcpu |
18.1 | 'atcpu' is 1 |
21 | The left operand of '%' is a garbage value |
1 | Assuming the condition is false |