Bug Summary

File:src/games/bs/bs.c
Warning:line 668, column 12
The left operand of '%' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name bs.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/games/bs/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/games/bs/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/games/bs/bs.c
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
51typedef 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
61static void announceopts(void);
62static int awinna(void);
63static bool_Bool checkplace(int, ship_t *, int);
64static int collidecheck(int, int, int);
65static int cpufire(int, int);
66static bool_Bool cpushipcanfit(int, int, int, int);
67static int cputurn(void);
68static void do_options(int, char *[]);
69static void error(char *);
70static int getcoord(int);
71static ship_t *hitship(int, int);
72static void initgame(void);
73static void intro(void);
74static void placeship(int, ship_t *, int);
75static int playagain(void);
76static int plyturn(void);
77static void prompt(int, const char *, ...)
78 __attribute__((__format__ (printf, 2, 3)));
79static void randomfire(int *, int *);
80static void randomplace(int, ship_t *);
81static int rnd(int);
82static int scount(int);
83static 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 */
142static char numbers[] = " 0 1 2 3 4 5 6 7 8 9";
143
144static char carrier[] = "Aircraft Carrier";
145static char battle[] = "Battleship";
146static char sub[] = "Submarine";
147static char destroy[] = "Destroyer";
148static char ptboat[] = "PT Boat";
149
150static char name[LOGIN_NAME_MAX32];
151static 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
162static int xincr[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
163static int yincr[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
164
165/* current ship position and direction */
166static int curx = (BWIDTH10 / 2);
167static int cury = (BDEPTH10 / 2);
168
169ship_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
178ship_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 */
204static int cpushortest;
205static int cpulongest;
206
207/* "Hits" board, and main board. */
208static char hits[2][BWIDTH10][BDEPTH10], board[2][BWIDTH10][BDEPTH10];
209
210static int turn; /* 0=player, 1=computer */
211static int plywon=0, cpuwon=0; /* How many games has each won? */
212
213static int salvo, blitz, closepack;
214
215/* end the game, either normally or due to signal */
216static void
217uninitgame(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 */
228static void
229announceopts(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
252static void
253intro(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 */
314static void
315prompt(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
327static void
328error(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
339static void
340placeship(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
359static int
360rnd(int n)
361{
362 return(arc4random_uniform(n));
363}
364
365/* generate a valid random ship placement into px,py */
366static void
367randomplace(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
377static void
378initgame(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 }
503regetchar:
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
578static int
579getcoord(int atcpu)
580{
581 int ny, nx, c;
14
'nx' declared without an initial value
582
583 if (atcpu
14.1
'atcpu' is 1
)
15
Taking true branch
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 (;;)
16
Loop condition is true. Entering loop body
589 {
590 if (atcpu
16.1
'atcpu' is 1
)
17
Taking true branch
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))
18
Control jumps to 'case 409:' at line 640
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
646 && myevent.y >= CY(0)(3 + (0)) && myevent.y < CY(BDEPTH)(3 + (10))
19
Assuming the condition is false
647 && myevent.x >= CX(0)(48 + (0)*3) && myevent.x < CX(BWIDTH)(48 + (10)*3)) 648 { 649 curx = CXINV(myevent.x)(((myevent.x) - 48) / 3); 650 cury = CYINV(myevent.y)((myevent.y) - 3); 651 return(' '); 652 } 653 else 654 beep(); 655 } 656 break;
20
Execution continues on line 668
657 case ERR(-1): 658 uninitgame(1); 659 break; 660 default: 661 if (atcpu) 662 (void) mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, " ")(wmove(stdscr,3 + 10 + 1,48 + 11) == (-1) ? (-1) : waddnstr(stdscr
," ",-1))
; 663 else 664 (void) mvaddstr(PYBASE + BDEPTH + 1, PXBASE + 11, " ")(wmove(stdscr,3 + 10 + 1,3 + 11) == (-1) ? (-1) : waddnstr(stdscr
," ",-1))
; 665 return(c); 666 } 667 668 curx = nx % BWIDTH10;
21
The left operand of '%' is a garbage value
669 cury = ny % BDEPTH10; 670 } 671} 672 673/* is this location on the selected zboard adjacent to a ship? */ 674static int 675collidecheck(int b, int y, int x) 676{ 677 int collide; 678 679 /* anything on the square */ 680 if ((collide = IS_SHIP(board[b][x][y])isupper(board[b][x][y])) != 0) 681 return(collide); 682 683 /* anything on the neighbors */ 684 if (!closepack) 685 { 686 int i; 687 688 for (i = 0; i < 8; i++) 689 { 690 int xend, yend; 691 692 yend = y + yincr[i]; 693 xend = x + xincr[i]; 694 if (ONBOARD(xend, yend)(xend >= 0 && xend < 10 && yend >= 0
&& yend < 10)
) 695 collide += IS_SHIP(board[b][xend][yend])isupper(board[b][xend][yend]); 696 } 697 } 698 return(collide); 699} 700 701static bool_Bool 702checkplace(int b, ship_t *ss, int vis) 703{ 704 int l, xend, yend; 705 706 /* first, check for board edges */ 707 xend = ss->x + (ss->length - 1) * xincr[ss->dir]; 708 yend = ss->y + (ss->length - 1) * yincr[ss->dir]; 709 if (!ONBOARD(xend, yend)(xend >= 0 && xend < 10 && yend >= 0
&& yend < 10)
) 710 { 711 if (vis) 712 switch(rnd(3)) 713 { 714 case 0: 715 error("Ship is hanging from the edge of the world"); 716 break; 717 case 1: 718 error("Try fitting it on the board"); 719 break; 720 case 2: 721 error("Figure I won't find it if you put it there?"); 722 break; 723 } 724 return(FALSE0); 725 } 726 727 for(l = 0; l < ss->length; ++l) 728 { 729 if(collidecheck(b, ss->y+l*yincr[ss->dir], ss->x+l*xincr[ss->dir])) 730 { 731 if (vis) 732 switch(rnd(3)) 733 { 734 case 0: 735 error("There's already a ship there"); 736 break; 737 case 1: 738 error("Collision alert! Aaaaaagh!"); 739 break; 740 case 2: 741 error("Er, Admiral, what about the other ship?"); 742 break; 743 } 744 return(FALSE0); 745 } 746 } 747 return(TRUE1); 748} 749 750static int 751awinna(void) 752{ 753 int i, j; 754 ship_t *ss; 755 756 for(i=0; i<2; ++i) 757 { 758 ss = (i) ? cpuship : plyship; 759 for(j=0; j < SHIPTYPES5; ++j, ++ss) 760 if(ss->length > ss->hits) 761 break; 762 if (j == SHIPTYPES5) 763 return(OTHER(1-turn)); 764 } 765 return(-1); 766} 767 768/* register a hit on the targeted ship */ 769static ship_t * 770hitship(int x, int y) 771{ 772 ship_t *sb, *ss; 773 char sym; 774 int oldx, oldy; 775 776 getyx(stdscr, oldy, oldx)(oldy = ((stdscr) ? (stdscr)->_cury : (-1)), oldx = ((stdscr
) ? (stdscr)->_curx : (-1)))
; 777 sb = (turn) ? plyship : cpuship; 778 if(!(sym = board[OTHER(1-turn)][x][y])) 779 return((ship_t *)NULL((void *)0)); 780 for(ss = sb; ss < sb + SHIPTYPES5; ++ss) 781 if(ss->symbol == sym) 782 { 783 if (++ss->hits < ss->length) /* still afloat? */ 784 return((ship_t *)NULL((void *)0)); 785 else /* sunk! */ 786 { 787 int i, j; 788 789 if (!closepack) 790 for (j = -1; j <= 1; j++) 791 { 792 int bx = ss->x + j * xincr[(ss->dir + 2) % 8]; 793 int by = ss->y + j * yincr[(ss->dir + 2) % 8]; 794 795 for (i = -1; i <= ss->length; ++i) 796 { 797 int x1, y1; 798 799 x1 = bx + i * xincr[ss->dir]; 800 y1 = by + i * yincr[ss->dir]; 801 if (ONBOARD(x1, y1)(x1 >= 0 && x1 < 10 && y1 >= 0 &&
y1 < 10)
) 802 { 803 hits[turn][x1][y1] = MARK_MISS'o'; 804 if (turn == PLAYER0) 805 { 806 cgoto(y1, x1)(void)wmove(stdscr,(3 + (y1)),(48 + (x1)*3)); 807 if (has_colors()) 808 attron(COLOR_PAIR(COLOR_GREEN))wattr_on(stdscr, (attr_t)(((2) << ((0) + 8))), ((void *
)0))
; 809 (void)addch(MARK_MISS)waddch(stdscr,'o'); 810 attrset(0)((stdscr)->_attrs = (0)); 811 } 812 } 813 } 814 } 815 816 for (i = 0; i < ss->length; ++i) 817 { 818 int x1 = ss->x + i * xincr[ss->dir]; 819 int y1 = ss->y + i * yincr[ss->dir]; 820 821 hits[turn][x1][y1] = ss->symbol; 822 if (turn == PLAYER0) 823 { 824 cgoto(y1, x1)(void)wmove(stdscr,(3 + (y1)),(48 + (x1)*3)); 825 (void) addch((chtype)(ss->symbol))waddch(stdscr,(chtype)(ss->symbol)); 826 } 827 } 828 829 (void) move(oldy, oldx)wmove(stdscr,oldy,oldx); 830 return(ss); 831 } 832 } 833 (void) move(oldy, oldx)wmove(stdscr,oldy,oldx); 834 return((ship_t *)NULL((void *)0)); 835} 836 837static int 838plyturn(void) 839{ 840 ship_t *ss; 841 int hit; 842 char *m = NULL((void *)0); 843 844 prompt(1, "Where do you want to shoot? "); 845 for (;;)
12
Loop condition is true. Entering loop body
846 { 847 (void) getcoord(COMPUTER1);
13
Calling 'getcoord'
848 if (hits[PLAYER0][curx][cury]) 849 { 850 prompt(1, "You shelled this spot already! Try again."); 851 beep(); 852 } 853 else 854 break; 855 } 856 hit = IS_SHIP(board[COMPUTER][curx][cury])isupper(board[1][curx][cury]); 857 hits[PLAYER0][curx][cury] = hit ? MARK_HIT'H' : MARK_MISS'o'; 858 cgoto(cury, curx)(void)wmove(stdscr,(3 + (cury)),(48 + (curx)*3)); 859 if (has_colors()) { 860 if (hit) 861 attron(COLOR_PAIR(COLOR_RED))wattr_on(stdscr, (attr_t)(((1) << ((0) + 8))), ((void *
)0))
; 862 else 863 attron(COLOR_PAIR(COLOR_GREEN))wattr_on(stdscr, (attr_t)(((2) << ((0) + 8))), ((void *
)0))
; 864 } 865 (void) addch((chtype)hits[PLAYER][curx][cury])waddch(stdscr,(chtype)hits[0][curx][cury]); 866 attrset(0)((stdscr)->_attrs = (0)); 867 868 prompt(1, "You %s.", hit ? "scored a hit" : "missed"); 869 if(hit && (ss = hitship(curx, cury))) 870 { 871 switch(rnd(5)) 872 { 873 case 0: 874 m = " You sank my %s!"; 875 break; 876 case 1: 877 m = " I have this sinking feeling about my %s...."; 878 break; 879 case 2: 880 m = " My %s has gone to Davy Jones's locker!"; 881 break; 882 case 3: 883 m = " Glub, glub -- my %s is headed for the bottom!"; 884 break; 885 case 4: 886 m = " You'll pick up survivors from my %s, I hope...!"; 887 break; 888 } 889 (void)printw(m, ss->name); 890 (void)beep(); 891 } 892 return(hit); 893} 894 895static int 896sgetc(char *s) 897{ 898 char *s1; 899 int ch; 900 901 (void)refresh()wrefresh(stdscr); 902 for(;;) 903 { 904 ch = getch()wgetch(stdscr); 905 if (islower(ch)) 906 ch = toupper(ch); 907 if (ch == CTRLC'\003') 908 uninitgame(0); 909 for (s1=s; *s1 && ch != *s1; ++s1) 910 continue; 911 if (*s1) 912 { 913 (void) addch((chtype)ch)waddch(stdscr,(chtype)ch); 914 (void) refresh()wrefresh(stdscr); 915 return(ch); 916 } 917 } 918} 919 920/* Checks to see if there's room for a ship of a given length in a given 921 * direction. If direction is negative, check in all directions. Note 922 * that North and South are equivalent, as are East and West. 923 */ 924static bool_Bool 925cpushipcanfit(int x, int y, int length, int direction) 926{ 927 int len = 1; 928 int x1, y1; 929 930 if (direction >= 0) 931 { 932 direction %= 4; 933 while (direction < 8) 934 { 935 x1 = x + xincr[direction]; 936 y1 = y + yincr[direction]; 937 while (POSSIBLE(x1,y1)((x1 >= 0 && x1 < 10 && y1 >= 0 &&
y1 < 10) && !hits[1][x1][y1])
) 938 { 939 len++; 940 x1 += xincr[direction]; 941 y1 += yincr[direction]; 942 } 943 direction += 4; 944 } 945 return (len >= length); 946 } 947 else 948 { 949 return ((cpushipcanfit(x,y,length,E0)) || 950 (cpushipcanfit(x,y,length,S2))); 951 } 952} 953 954/* random-fire routine -- implements simple diagonal-striping strategy */ 955static void 956randomfire(int *px, int *py) 957{ 958 static int huntoffs; /* Offset on search strategy */ 959 int ypossible[BWIDTH10 * BDEPTH10], xpossible[BWIDTH10 * BDEPTH10], nposs; 960 int x, y, i; 961 962 if (turncount++ == 0) 963 huntoffs = rnd(srchstep); 964 965 /* first, list all possible moves on the diagonal stripe */ 966 nposs = 0; 967 for (x = 0; x < BWIDTH10; x++) 968 for (y = 0; y < BDEPTH10; y++) 969 if ((!hits[COMPUTER1][x][y]) && 970 (((x+huntoffs) % srchstep) == (y % srchstep)) && 971 (cpushipcanfit(x,y,cpulongest,-1))) 972 { 973 xpossible[nposs] = x; 974 ypossible[nposs] = y; 975 nposs++; 976 } 977 if (nposs) 978 { 979 i = rnd(nposs); 980 981 *px = xpossible[i]; 982 *py = ypossible[i]; 983 } 984 else if (srchstep > cpulongest) 985 { 986 --srchstep; 987 randomfire(px, py); 988 } 989 else 990 { 991 error("No moves possible?? Help!"); 992 exit(1); 993 } 994} 995 996#define S_MISS0 0 997#define S_HIT1 1 998#define S_SUNK-1 -1 999 1000/* fire away at given location */ 1001static int 1002cpufire(int x, int y) 1003{ 1004 int hit; 1005 bool_Bool sunk; 1006 ship_t *ss = NULL((void *)0); 1007 1008 hits[COMPUTER1][x][y] = (hit = (board[PLAYER0][x][y])) ? MARK_HIT'H' : MARK_MISS'o'; 1009 (void) mvprintw(PROMPTLINE21, 0, 1010 "I shoot at %c%d. I %s!", y + 'A', x, hit ? "hit" : "miss"); 1011 if ((sunk = (hit && (ss = hitship(x, y))))) 1012 (void) printw(" I've sunk your %s", ss->name); 1013 (void)clrtoeol()wclrtoeol(stdscr); 1014 1015 pgoto(y, x)(void)wmove(stdscr,(3 + (y)),(3 + (x)*3)); 1016 if (has_colors()) { 1017 if (hit) 1018 attron(COLOR_PAIR(COLOR_RED))wattr_on(stdscr, (attr_t)(((1) << ((0) + 8))), ((void *
)0))
; 1019 else 1020 attron(COLOR_PAIR(COLOR_GREEN))wattr_on(stdscr, (attr_t)(((2) << ((0) + 8))), ((void *
)0))
; 1021 } 1022 (void) addch((chtype)(hit ? SHOWHIT : SHOWSPLASH))waddch(stdscr,(chtype)(hit ? '*' : ' ')); 1023 attrset(0)((stdscr)->_attrs = (0)); 1024 1025 return(hit ? (sunk ? S_SUNK-1 : S_HIT1) : S_MISS0); 1026} 1027 1028/* 1029 * This code implements a fairly irregular FSM, so please forgive the rampant 1030 * unstructuredness below. The five labels are states which need to be held 1031 * between computer turns. 1032 */ 1033static int 1034cputurn(void) 1035{ 1036 static bool_Bool used[4]; 1037 static ship_t ts; 1038 int navail, x, y, d, n, hit = S_MISS0; 1039 bool_Bool closenoshot = FALSE0; 1040 1041 switch(next) 1042 { 1043 case RANDOM_FIRE0: /* last shot was random and missed */ 1044 refire: 1045 randomfire(&x, &y); 1046 if (!(hit = cpufire(x, y))) 1047 next = RANDOM_FIRE0; 1048 else 1049 { 1050 ts.x = x; ts.y = y; 1051 ts.hits = 1; 1052 next = (hit == S_SUNK-1) ? RANDOM_FIRE0 : RANDOM_HIT1; 1053 } 1054 break; 1055 1056 case RANDOM_HIT1: /* last shot was random and hit */ 1057 used[E0/2] = used[W4/2] = (!(cpushipcanfit(ts.x,ts.y,cpushortest,E0))); 1058 used[S2/2] = used[N6/2] = (!(cpushipcanfit(ts.x,ts.y,cpushortest,S2))); 1059 /* FALLTHROUGH */ 1060 1061 case HUNT_DIRECT2: /* last shot hit, we're looking for ship's long axis */ 1062 for (d = navail = 0; d < 4; d++) 1063 { 1064 x = ts.x + xincr[d*2]; y = ts.y + yincr[d*2]; 1065 if (!used[d] && POSSIBLE(x, y)((x >= 0 && x < 10 && y >= 0 &&
y < 10) && !hits[1][x][y])
) 1066 navail++; 1067 else 1068 used[d] = TRUE1; 1069 } 1070 if (navail == 0) /* no valid places for shots adjacent... */ 1071 goto refire; /* ...so we must random-fire */ 1072 else 1073 { 1074 for (d = 0, n = rnd(navail) + 1; n; n--,d++) 1075 while (used[d]) 1076 d++; 1077 d--; 1078 1079 x = ts.x + xincr[d*2]; 1080 y = ts.y + yincr[d*2]; 1081 1082 if (!(hit = cpufire(x, y))) 1083 next = HUNT_DIRECT2; 1084 else 1085 { 1086 ts.x = x; ts.y = y; ts.dir = d*2; ts.hits++; 1087 next = (hit == S_SUNK-1) ? RANDOM_FIRE0 : FIRST_PASS3; 1088 } 1089 } 1090 break; 1091 1092 case FIRST_PASS3: /* we have a start and a direction now */ 1093 x = ts.x + xincr[ts.dir]; 1094 y = ts.y + yincr[ts.dir]; 1095 if (POSSIBLE(x, y)((x >= 0 && x < 10 && y >= 0 &&
y < 10) && !hits[1][x][y])
) 1096 { 1097 if ((hit = cpufire(x, y))) 1098 { 1099 ts.x = x; ts.y = y; ts.hits++; 1100 next = (hit == S_SUNK-1) ? RANDOM_FIRE0 : FIRST_PASS3; 1101 } 1102 else 1103 next = REVERSE_JUMP4; 1104 break; 1105 } 1106 else 1107 next = REVERSE_JUMP4; 1108 /* FALL THROUGH */ 1109 1110 case REVERSE_JUMP4: /* nail down the ship's other end */ 1111 ts.dir = (ts.dir + 4) % 8; 1112 ts.x += (ts.hits-1) * xincr[ts.dir]; 1113 ts.y += (ts.hits-1) * yincr[ts.dir]; 1114 /* FALL THROUGH */ 1115 1116 case SECOND_PASS5: /* kill squares not caught on first pass */ 1117 x = ts.x + xincr[ts.dir]; 1118 y = ts.y + yincr[ts.dir]; 1119 if (POSSIBLE(x, y)((x >= 0 && x < 10 && y >= 0 &&
y < 10) && !hits[1][x][y])
) 1120 { 1121 if ((hit = cpufire(x, y))) 1122 { 1123 ts.x = x; ts.y = y; ts.hits++; 1124 next = (hit == S_SUNK-1) ? RANDOM_FIRE0 : SECOND_PASS5; 1125 } 1126 else 1127 { 1128 /* The only way to get here is if closepack is on; otherwise, 1129 * we _have_ sunk the ship. I set hit to S_SUNK just to get 1130 * the additional closepack logic at the end of the switch. 1131 */ 1132/*assert closepack*/ 1133if (!closepack) error("Assertion failed: not closepack 1"); 1134 hit = S_SUNK-1; 1135 next = RANDOM_FIRE0; 1136 } 1137 } 1138 else 1139 { 1140/*assert closepack*/ 1141if (!closepack) error("Assertion failed: not closepack 2"); 1142 hit = S_SUNK-1; 1143 closenoshot = TRUE1; /* Didn't shoot yet! */ 1144 next = RANDOM_FIRE0; 1145 } 1146 break; 1147 } /* switch(next) */ 1148 1149 if (hit == S_SUNK-1) 1150 { 1151 /* Update cpulongest and cpushortest. We could increase srchstep 1152 * if it's smaller than cpushortest but that makes strategic sense 1153 * only if we've been doing continuous diagonal stripes, and that's 1154 * less interesting to watch. 1155 */ 1156 ship_t *sp = plyship; 1157 1158 cpushortest = cpulongest; 1159 cpulongest = 0; 1160 for (d=0 ; d < SHIPTYPES5; d++, sp++) 1161 { 1162 if (sp->hits < sp->length) 1163 { 1164 cpushortest = (cpushortest < sp->length) ? cpushortest : sp->length; 1165 cpulongest = (cpulongest > sp->length) ? cpulongest : sp->length; 1166 } 1167 } 1168 /* Now, if we're in closepack mode, we may have knocked off part of 1169 * another ship, in which case we shouldn't do RANDOM_FIRE. A 1170 * more robust implementation would probably do this check regardless 1171 * of whether closepack was set or not. 1172 * Note that MARK_HIT is set only for ships that aren't sunk; 1173 * hitship() changes the marker to the ship's character when the 1174 * ship is sunk. 1175 */ 1176 if (closepack) 1177 { 1178 ts.hits = 0; 1179 for (x = 0; x < BWIDTH10; x++) 1180 for (y = 0; y < BDEPTH10; y++) 1181 { 1182 if (hits[COMPUTER1][x][y] == MARK_HIT'H') 1183 { 1184 /* So we found part of another ship. It may have more 1185 * than one hit on it. Check to see if it does. If no 1186 * hit does, take the last MARK_HIT and be RANDOM_HIT. 1187 */ 1188 ts.x = x; ts.y = y; ts.hits = 1; 1189 for (d = 0; d < 8; d += 2) 1190 { 1191 while ((ONBOARD(ts.x, ts.y)(ts.x >= 0 && ts.x < 10 && ts.y >= 0
&& ts.y < 10)
) && 1192 (hits[COMPUTER1][(int)ts.x][(int)ts.y] == MARK_HIT'H')) 1193 { 1194 ts.x += xincr[d]; ts.y += yincr[d]; ts.hits++; 1195 } 1196 if ((--ts.hits > 1) && (ONBOARD(ts.x, ts.y)(ts.x >= 0 && ts.x < 10 && ts.y >= 0
&& ts.y < 10)
) && 1197 (hits[COMPUTER1][(int)ts.x][(int)ts.y] == 0)) 1198 { 1199 ts.dir = d; 1200 ts.x -= xincr[d]; ts.y -= yincr[d]; 1201 d = 100; /* use as a flag */ 1202 x = BWIDTH10; y = BDEPTH10; /* end the loop */ 1203 } else { 1204 ts.x = x; ts.y = y; ts.hits = 1; 1205 } 1206 1207 } 1208 } 1209 if (ts.hits) 1210 { 1211 next = (d >= 100) ? FIRST_PASS3 : RANDOM_HIT1; 1212 } else 1213 next = RANDOM_FIRE0; 1214 } 1215 } 1216 if (closenoshot) 1217 { 1218 return(cputurn()); 1219 } 1220 } 1221 1222 /* check for continuation and/or winner */ 1223 if (salvo) 1224 { 1225 (void)refresh()wrefresh(stdscr); 1226 (void)sleep(1); 1227 } 1228 1229 return(hit); 1230} 1231 1232static int 1233playagain(void) 1234{ 1235 int j; 1236 ship_t *ss; 1237 1238 for (ss = cpuship; ss < cpuship + SHIPTYPES5; ss++) 1239 for(j = 0; j < ss->length; j++) 1240 { 1241 cgoto(ss->y + j * yincr[ss->dir], ss->x + j * xincr[ss->dir])(void)wmove(stdscr,(3 + (ss->y + j * yincr[ss->dir])),(
48 + (ss->x + j * xincr[ss->dir])*3))
; 1242 (void) addch((chtype)ss->symbol)waddch(stdscr,(chtype)ss->symbol); 1243 } 1244 1245 if(awinna()) 1246 ++cpuwon; 1247 else 1248 ++plywon; 1249 j = 18 + strlen(name); 1250 /* If you play a hundred games or more at a go, you deserve a badly 1251 * centred score output. 1252 */ 1253 if(plywon >= 10) 1254 ++j; 1255 if(cpuwon >= 10) 1256 ++j; 1257 (void) mvprintw(1,(COLWIDTH80-j)/2, 1258 "%s: %d Computer: %d",name,plywon,cpuwon); 1259 1260 prompt(2, (awinna()) ? "Want to be humiliated again, %s [yn]? " 1261 : "Going to give me a chance for revenge, %s [yn]? ",name); 1262 return(sgetc("YN") == 'Y'); 1263} 1264 1265__dead__attribute__((__noreturn__)) void 1266usage(void) 1267{ 1268 (void) fprintf(stderr(&__sF[2]), "usage: %s [-b | -s] [-c]\n", getprogname()); 1269 (void) fprintf(stderr(&__sF[2]), "\tWhere the options are:\n"); 1270 (void) fprintf(stderr(&__sF[2]), "\t-b : play a blitz game\n"); 1271 (void) fprintf(stderr(&__sF[2]), "\t-s : play a salvo game\n"); 1272 (void) fprintf(stderr(&__sF[2]), "\t-c : ships may be adjacent\n"); 1273 exit(1); 1274} 1275 1276static void 1277do_options(int c, char *op[]) 1278{ 1279 int ch; 1280 1281 while ((ch = getopt(c, op, "bchs")) != -1) { 1282 switch (ch) { 1283 case 'b': 1284 blitz = 1; 1285 if (salvo == 1) 1286 { 1287 (void) fprintf(stderr(&__sF[2]), 1288 "Bad Arg: -b and -s are mutually exclusive\n"); 1289 exit(1); 1290 } 1291 break; 1292 case 's': 1293 salvo = 1; 1294 if (blitz == 1) 1295 { 1296 (void) fprintf(stderr(&__sF[2]), 1297 "Bad Arg: -s and -b are mutually exclusive\n"); 1298 exit(1); 1299 } 1300 break; 1301 case 'c': 1302 closepack = 1; 1303 break; 1304 case 'h': 1305 default: 1306 (void) usage(); 1307 exit(1); 1308 } 1309 } 1310 if (op[optind] != NULL((void *)0)) 1311 (void) usage(); 1312} 1313 1314static int 1315scount(int who) 1316{ 1317 int i, shots; 1318 ship_t *sp; 1319 1320 if (who) 1321 sp = cpuship; /* count cpu shots */ 1322 else 1323 sp = plyship; /* count player shots */ 1324 1325 for (i = 0, shots = 0; i < SHIPTYPES5; i++, sp++) 1326 { 1327 if (sp->hits >= sp->length) 1328 continue; /* dead ship */ 1329 else 1330 shots++; 1331 } 1332 return(shots); 1333} 1334 1335int 1336main(int argc, char *argv[]) 1337{ 1338 do_options(argc, argv); 1339 1340 intro(); 1341 1342 if (pledge("stdio tty", NULL((void *)0)) == -1)
1
Assuming the condition is false
2
Taking false branch
1343 err(1, "pledge"); 1344 1345 do { 1346 initgame(); 1347 while(awinna() == -1)
3
Assuming the condition is true
4
Loop condition is true. Entering loop body
1348 { 1349 if (!blitz)
5
Assuming 'blitz' is 0
6
Taking true branch
1350 { 1351 if (!salvo)
7
Assuming 'salvo' is 0
8
Taking true branch
1352 { 1353 if(turn)
9
Assuming 'turn' is 0
10
Taking false branch
1354 (void) cputurn(); 1355 else 1356 (void) plyturn();
11
Calling 'plyturn'
1357 } 1358 else /* salvo */ 1359 { 1360 int i; 1361 1362 i = scount(turn); 1363 while (i--) 1364 { 1365 if (turn) 1366 { 1367 if (cputurn() && awinna() != -1) 1368 i = 0; 1369 } 1370 else 1371 { 1372 if (plyturn() && awinna() != -1) 1373 i = 0; 1374 } 1375 } 1376 } 1377 } 1378 else /* blitz */ 1379 while(turn ? cputurn() : plyturn()) 1380 { 1381 if (turn) /* Pause between successive computer shots */ 1382 { 1383 (void)refresh()wrefresh(stdscr); 1384 (void)sleep(1); 1385 } 1386 if (awinna() != -1) 1387 break; 1388 } 1389 turn = OTHER(1-turn); 1390 } 1391 } while 1392 (playagain()); 1393 uninitgame(0); 1394 return 0; 1395}