wump.c revision 1.17 1 1.17 jsm /* $NetBSD: wump.c,v 1.17 2005/02/15 12:56:20 jsm Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1989, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd * All rights reserved.
7 1.1 cgd *
8 1.1 cgd * This code is derived from software contributed to Berkeley by
9 1.1 cgd * Dave Taylor, of Intuitive Systems.
10 1.1 cgd *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.15 agc * 3. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.5 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.5 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
39 1.5 lukem The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.3 cgd #if 0
44 1.3 cgd static char sccsid[] = "@(#)wump.c 8.1 (Berkeley) 5/31/93";
45 1.3 cgd #else
46 1.17 jsm __RCSID("$NetBSD: wump.c,v 1.17 2005/02/15 12:56:20 jsm Exp $");
47 1.3 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * A very new version of the age old favorite Hunt-The-Wumpus game that has
52 1.1 cgd * been a part of the BSD distribution of Unix for longer than us old folk
53 1.1 cgd * would care to remember.
54 1.1 cgd */
55 1.1 cgd
56 1.8 hubertf #include <err.h>
57 1.1 cgd #include <sys/types.h>
58 1.1 cgd #include <sys/file.h>
59 1.8 hubertf #include <sys/wait.h>
60 1.1 cgd #include <stdio.h>
61 1.5 lukem #include <stdlib.h>
62 1.4 cgd #include <string.h>
63 1.10 jsm #include <time.h>
64 1.5 lukem #include <unistd.h>
65 1.1 cgd #include "pathnames.h"
66 1.1 cgd
67 1.1 cgd /* some defines to spec out what our wumpus cave should look like */
68 1.1 cgd
69 1.1 cgd #define MAX_ARROW_SHOT_DISTANCE 6 /* +1 for '0' stopper */
70 1.1 cgd #define MAX_LINKS_IN_ROOM 25 /* a complex cave */
71 1.1 cgd
72 1.1 cgd #define MAX_ROOMS_IN_CAVE 250
73 1.1 cgd #define ROOMS_IN_CAVE 20
74 1.1 cgd #define MIN_ROOMS_IN_CAVE 10
75 1.1 cgd
76 1.1 cgd #define LINKS_IN_ROOM 3
77 1.1 cgd #define NUMBER_OF_ARROWS 5
78 1.1 cgd #define PIT_COUNT 3
79 1.1 cgd #define BAT_COUNT 3
80 1.1 cgd
81 1.1 cgd #define EASY 1 /* levels of play */
82 1.1 cgd #define HARD 2
83 1.1 cgd
84 1.1 cgd /* some macro definitions for cleaner output */
85 1.1 cgd
86 1.1 cgd #define plural(n) (n == 1 ? "" : "s")
87 1.1 cgd
88 1.1 cgd /* simple cave data structure; +1 so we can index from '1' not '0' */
89 1.1 cgd struct room_record {
90 1.1 cgd int tunnel[MAX_LINKS_IN_ROOM];
91 1.1 cgd int has_a_pit, has_a_bat;
92 1.1 cgd } cave[MAX_ROOMS_IN_CAVE+1];
93 1.1 cgd
94 1.1 cgd /*
95 1.1 cgd * global variables so we can keep track of where the player is, how
96 1.1 cgd * many arrows they still have, where el wumpo is, and so on...
97 1.1 cgd */
98 1.1 cgd int player_loc = -1; /* player location */
99 1.1 cgd int wumpus_loc = -1; /* The Bad Guy location */
100 1.1 cgd int level = EASY; /* level of play */
101 1.1 cgd int arrows_left; /* arrows unshot */
102 1.1 cgd
103 1.1 cgd #ifdef DEBUG
104 1.1 cgd int debug = 0;
105 1.1 cgd #endif
106 1.1 cgd
107 1.1 cgd int pit_num = PIT_COUNT; /* # pits in cave */
108 1.1 cgd int bat_num = BAT_COUNT; /* # bats */
109 1.1 cgd int room_num = ROOMS_IN_CAVE; /* # rooms in cave */
110 1.1 cgd int link_num = LINKS_IN_ROOM; /* links per room */
111 1.1 cgd int arrow_num = NUMBER_OF_ARROWS; /* arrow inventory */
112 1.1 cgd
113 1.1 cgd char answer[20]; /* user input */
114 1.1 cgd
115 1.16 jsm int bats_nearby(void);
116 1.16 jsm void cave_init(void);
117 1.16 jsm void clear_things_in_cave(void);
118 1.16 jsm void display_room_stats(void);
119 1.16 jsm int gcd(int, int);
120 1.16 jsm int getans(const char *);
121 1.16 jsm void initialize_things_in_cave(void);
122 1.16 jsm void instructions(void);
123 1.16 jsm int int_compare(const void *, const void *);
124 1.16 jsm void jump(int);
125 1.16 jsm void kill_wump(void);
126 1.16 jsm int main(int, char **);
127 1.16 jsm int move_to(const char *);
128 1.16 jsm void move_wump(void);
129 1.16 jsm void no_arrows(void);
130 1.16 jsm void pit_kill(void);
131 1.16 jsm int pit_nearby(void);
132 1.16 jsm void pit_survive(void);
133 1.16 jsm int shoot(char *);
134 1.16 jsm void shoot_self(void);
135 1.16 jsm int take_action(void);
136 1.16 jsm void usage(void) __attribute__((__noreturn__));
137 1.16 jsm void wump_kill(void);
138 1.16 jsm int wump_nearby(void);
139 1.5 lukem
140 1.5 lukem int
141 1.1 cgd main(argc, argv)
142 1.1 cgd int argc;
143 1.1 cgd char **argv;
144 1.1 cgd {
145 1.1 cgd int c;
146 1.12 jsm
147 1.12 jsm /* Revoke setgid privileges */
148 1.13 mycroft setgid(getgid());
149 1.1 cgd
150 1.1 cgd #ifdef DEBUG
151 1.5 lukem while ((c = getopt(argc, argv, "a:b:hp:r:t:d")) != -1)
152 1.1 cgd #else
153 1.5 lukem while ((c = getopt(argc, argv, "a:b:hp:r:t:")) != -1)
154 1.1 cgd #endif
155 1.1 cgd switch (c) {
156 1.1 cgd case 'a':
157 1.1 cgd arrow_num = atoi(optarg);
158 1.1 cgd break;
159 1.1 cgd case 'b':
160 1.1 cgd bat_num = atoi(optarg);
161 1.1 cgd break;
162 1.1 cgd #ifdef DEBUG
163 1.1 cgd case 'd':
164 1.1 cgd debug = 1;
165 1.1 cgd break;
166 1.1 cgd #endif
167 1.1 cgd case 'h':
168 1.1 cgd level = HARD;
169 1.1 cgd break;
170 1.1 cgd case 'p':
171 1.1 cgd pit_num = atoi(optarg);
172 1.1 cgd break;
173 1.1 cgd case 'r':
174 1.1 cgd room_num = atoi(optarg);
175 1.1 cgd if (room_num < MIN_ROOMS_IN_CAVE) {
176 1.1 cgd (void)fprintf(stderr,
177 1.1 cgd "No self-respecting wumpus would live in such a small cave!\n");
178 1.1 cgd exit(1);
179 1.1 cgd }
180 1.1 cgd if (room_num > MAX_ROOMS_IN_CAVE) {
181 1.1 cgd (void)fprintf(stderr,
182 1.1 cgd "Even wumpii can't furnish caves that large!\n");
183 1.1 cgd exit(1);
184 1.1 cgd }
185 1.1 cgd break;
186 1.1 cgd case 't':
187 1.1 cgd link_num = atoi(optarg);
188 1.1 cgd if (link_num < 2) {
189 1.1 cgd (void)fprintf(stderr,
190 1.1 cgd "Wumpii like extra doors in their caves!\n");
191 1.1 cgd exit(1);
192 1.1 cgd }
193 1.1 cgd break;
194 1.1 cgd case '?':
195 1.1 cgd default:
196 1.1 cgd usage();
197 1.1 cgd }
198 1.1 cgd
199 1.1 cgd if (link_num > MAX_LINKS_IN_ROOM ||
200 1.1 cgd link_num > room_num - (room_num / 4)) {
201 1.1 cgd (void)fprintf(stderr,
202 1.1 cgd "Too many tunnels! The cave collapsed!\n(Fortunately, the wumpus escaped!)\n");
203 1.1 cgd exit(1);
204 1.1 cgd }
205 1.1 cgd
206 1.1 cgd if (level == HARD) {
207 1.1 cgd bat_num += ((random() % (room_num / 2)) + 1);
208 1.1 cgd pit_num += ((random() % (room_num / 2)) + 1);
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd if (bat_num > room_num / 2) {
212 1.1 cgd (void)fprintf(stderr,
213 1.1 cgd "The wumpus refused to enter the cave, claiming it was too crowded!\n");
214 1.1 cgd exit(1);
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd if (pit_num > room_num / 2) {
218 1.1 cgd (void)fprintf(stderr,
219 1.1 cgd "The wumpus refused to enter the cave, claiming it was too dangerous!\n");
220 1.1 cgd exit(1);
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd instructions();
224 1.1 cgd cave_init();
225 1.1 cgd
226 1.1 cgd /* and we're OFF! da dum, da dum, da dum, da dum... */
227 1.1 cgd (void)printf(
228 1.1 cgd "\nYou're in a cave with %d rooms and %d tunnels leading from each room.\n\
229 1.1 cgd There are %d bat%s and %d pit%s scattered throughout the cave, and your\n\
230 1.1 cgd quiver holds %d custom super anti-evil Wumpus arrows. Good luck.\n",
231 1.1 cgd room_num, link_num, bat_num, plural(bat_num), pit_num,
232 1.1 cgd plural(pit_num), arrow_num);
233 1.1 cgd
234 1.1 cgd for (;;) {
235 1.1 cgd initialize_things_in_cave();
236 1.1 cgd arrows_left = arrow_num;
237 1.1 cgd do {
238 1.1 cgd display_room_stats();
239 1.1 cgd (void)printf("Move or shoot? (m-s) ");
240 1.1 cgd (void)fflush(stdout);
241 1.1 cgd if (!fgets(answer, sizeof(answer), stdin))
242 1.1 cgd break;
243 1.1 cgd } while (!take_action());
244 1.1 cgd
245 1.1 cgd if (!getans("\nCare to play another game? (y-n) "))
246 1.1 cgd exit(0);
247 1.1 cgd if (getans("In the same cave? (y-n) "))
248 1.1 cgd clear_things_in_cave();
249 1.1 cgd else
250 1.1 cgd cave_init();
251 1.1 cgd }
252 1.1 cgd /* NOTREACHED */
253 1.5 lukem return (0);
254 1.1 cgd }
255 1.1 cgd
256 1.5 lukem void
257 1.1 cgd display_room_stats()
258 1.1 cgd {
259 1.5 lukem int i;
260 1.1 cgd
261 1.1 cgd /*
262 1.1 cgd * Routine will explain what's going on with the current room, as well
263 1.1 cgd * as describe whether there are pits, bats, & wumpii nearby. It's
264 1.1 cgd * all pretty mindless, really.
265 1.1 cgd */
266 1.1 cgd (void)printf(
267 1.1 cgd "\nYou are in room %d of the cave, and have %d arrow%s left.\n",
268 1.1 cgd player_loc, arrows_left, plural(arrows_left));
269 1.1 cgd
270 1.1 cgd if (bats_nearby())
271 1.1 cgd (void)printf("*rustle* *rustle* (must be bats nearby)\n");
272 1.1 cgd if (pit_nearby())
273 1.1 cgd (void)printf("*whoosh* (I feel a draft from some pits).\n");
274 1.1 cgd if (wump_nearby())
275 1.1 cgd (void)printf("*sniff* (I can smell the evil Wumpus nearby!)\n");
276 1.1 cgd
277 1.1 cgd (void)printf("There are tunnels to rooms %d, ",
278 1.1 cgd cave[player_loc].tunnel[0]);
279 1.1 cgd
280 1.1 cgd for (i = 1; i < link_num - 1; i++)
281 1.1 cgd if (cave[player_loc].tunnel[i] <= room_num)
282 1.1 cgd (void)printf("%d, ", cave[player_loc].tunnel[i]);
283 1.1 cgd (void)printf("and %d.\n", cave[player_loc].tunnel[link_num - 1]);
284 1.1 cgd }
285 1.1 cgd
286 1.5 lukem int
287 1.1 cgd take_action()
288 1.1 cgd {
289 1.1 cgd /*
290 1.1 cgd * Do the action specified by the player, either 'm'ove, 's'hoot
291 1.1 cgd * or something exceptionally bizarre and strange! Returns 1
292 1.1 cgd * iff the player died during this turn, otherwise returns 0.
293 1.1 cgd */
294 1.1 cgd switch (*answer) {
295 1.1 cgd case 'M':
296 1.1 cgd case 'm': /* move */
297 1.1 cgd return(move_to(answer + 1));
298 1.1 cgd case 'S':
299 1.1 cgd case 's': /* shoot */
300 1.1 cgd return(shoot(answer + 1));
301 1.1 cgd case 'Q':
302 1.1 cgd case 'q':
303 1.1 cgd case 'x':
304 1.1 cgd exit(0);
305 1.1 cgd case '\n':
306 1.1 cgd return(0);
307 1.1 cgd }
308 1.1 cgd if (random() % 15 == 1)
309 1.1 cgd (void)printf("Que pasa?\n");
310 1.1 cgd else
311 1.1 cgd (void)printf("I don't understand!\n");
312 1.1 cgd return(0);
313 1.1 cgd }
314 1.1 cgd
315 1.5 lukem int
316 1.1 cgd move_to(room_number)
317 1.5 lukem const char *room_number;
318 1.1 cgd {
319 1.1 cgd int i, just_moved_by_bats, next_room, tunnel_available;
320 1.1 cgd
321 1.1 cgd /*
322 1.1 cgd * This is responsible for moving the player into another room in the
323 1.1 cgd * cave as per their directions. If room_number is a null string,
324 1.1 cgd * then we'll prompt the user for the next room to go into. Once
325 1.1 cgd * we've moved into the room, we'll check for things like bats, pits,
326 1.1 cgd * and so on. This routine returns 1 if something occurs that kills
327 1.1 cgd * the player and 0 otherwise...
328 1.1 cgd */
329 1.1 cgd tunnel_available = just_moved_by_bats = 0;
330 1.1 cgd next_room = atoi(room_number);
331 1.1 cgd
332 1.1 cgd /* crap for magic tunnels */
333 1.1 cgd if (next_room == room_num + 1 &&
334 1.1 cgd cave[player_loc].tunnel[link_num-1] != next_room)
335 1.1 cgd ++next_room;
336 1.1 cgd
337 1.1 cgd while (next_room < 1 || next_room > room_num + 1) {
338 1.1 cgd if (next_room < 0 && next_room != -1)
339 1.1 cgd (void)printf("Sorry, but we're constrained to a semi-Euclidean cave!\n");
340 1.1 cgd if (next_room > room_num + 1)
341 1.1 cgd (void)printf("What? The cave surely isn't quite that big!\n");
342 1.1 cgd if (next_room == room_num + 1 &&
343 1.1 cgd cave[player_loc].tunnel[link_num-1] != next_room) {
344 1.1 cgd (void)printf("What? The cave isn't that big!\n");
345 1.1 cgd ++next_room;
346 1.1 cgd }
347 1.1 cgd (void)printf("To which room do you wish to move? ");
348 1.1 cgd (void)fflush(stdout);
349 1.1 cgd if (!fgets(answer, sizeof(answer), stdin))
350 1.1 cgd return(1);
351 1.1 cgd next_room = atoi(answer);
352 1.1 cgd }
353 1.1 cgd
354 1.1 cgd /* now let's see if we can move to that room or not */
355 1.1 cgd tunnel_available = 0;
356 1.1 cgd for (i = 0; i < link_num; i++)
357 1.1 cgd if (cave[player_loc].tunnel[i] == next_room)
358 1.1 cgd tunnel_available = 1;
359 1.1 cgd
360 1.1 cgd if (!tunnel_available) {
361 1.1 cgd (void)printf("*Oof!* (You hit the wall)\n");
362 1.1 cgd if (random() % 6 == 1) {
363 1.1 cgd (void)printf("Your colorful comments awaken the wumpus!\n");
364 1.1 cgd move_wump();
365 1.1 cgd if (wumpus_loc == player_loc) {
366 1.1 cgd wump_kill();
367 1.1 cgd return(1);
368 1.1 cgd }
369 1.1 cgd }
370 1.1 cgd return(0);
371 1.1 cgd }
372 1.1 cgd
373 1.1 cgd /* now let's move into that room and check it out for dangers */
374 1.1 cgd if (next_room == room_num + 1)
375 1.1 cgd jump(next_room = (random() % room_num) + 1);
376 1.1 cgd
377 1.1 cgd player_loc = next_room;
378 1.1 cgd for (;;) {
379 1.1 cgd if (next_room == wumpus_loc) { /* uh oh... */
380 1.1 cgd wump_kill();
381 1.1 cgd return(1);
382 1.1 cgd }
383 1.6 veego if (cave[next_room].has_a_pit) {
384 1.1 cgd if (random() % 12 < 2) {
385 1.1 cgd pit_survive();
386 1.1 cgd return(0);
387 1.1 cgd } else {
388 1.1 cgd pit_kill();
389 1.1 cgd return(1);
390 1.1 cgd }
391 1.6 veego }
392 1.1 cgd
393 1.1 cgd if (cave[next_room].has_a_bat) {
394 1.1 cgd (void)printf(
395 1.1 cgd "*flap* *flap* *flap* (humongous bats pick you up and move you%s!)\n",
396 1.1 cgd just_moved_by_bats ? " again": "");
397 1.1 cgd next_room = player_loc = (random() % room_num) + 1;
398 1.1 cgd just_moved_by_bats = 1;
399 1.1 cgd }
400 1.1 cgd
401 1.1 cgd else
402 1.1 cgd break;
403 1.1 cgd }
404 1.1 cgd return(0);
405 1.1 cgd }
406 1.1 cgd
407 1.5 lukem int
408 1.1 cgd shoot(room_list)
409 1.1 cgd char *room_list;
410 1.1 cgd {
411 1.1 cgd int chance, next, roomcnt;
412 1.1 cgd int j, arrow_location, link, ok;
413 1.5 lukem char *p;
414 1.1 cgd
415 1.1 cgd /*
416 1.1 cgd * Implement shooting arrows. Arrows are shot by the player indicating
417 1.1 cgd * a space-separated list of rooms that the arrow should pass through;
418 1.1 cgd * if any of the rooms they specify are not accessible via tunnel from
419 1.1 cgd * the room the arrow is in, it will instead fly randomly into another
420 1.1 cgd * room. If the player hits the wumpus, this routine will indicate
421 1.1 cgd * such. If it misses, this routine will *move* the wumpus one room.
422 1.1 cgd * If it's the last arrow, the player then dies... Returns 1 if the
423 1.1 cgd * player has won or died, 0 if nothing has happened.
424 1.1 cgd */
425 1.1 cgd arrow_location = player_loc;
426 1.1 cgd for (roomcnt = 1;; ++roomcnt, room_list = NULL) {
427 1.6 veego if (!(p = strtok(room_list, " \t\n"))) {
428 1.1 cgd if (roomcnt == 1) {
429 1.1 cgd (void)printf(
430 1.1 cgd "The arrow falls to the ground at your feet!\n");
431 1.1 cgd return(0);
432 1.1 cgd } else
433 1.1 cgd break;
434 1.6 veego }
435 1.1 cgd if (roomcnt > 5) {
436 1.1 cgd (void)printf(
437 1.1 cgd "The arrow wavers in its flight and and can go no further!\n");
438 1.1 cgd break;
439 1.1 cgd }
440 1.1 cgd next = atoi(p);
441 1.1 cgd for (j = 0, ok = 0; j < link_num; j++)
442 1.1 cgd if (cave[arrow_location].tunnel[j] == next)
443 1.1 cgd ok = 1;
444 1.1 cgd
445 1.1 cgd if (ok) {
446 1.1 cgd if (next > room_num) {
447 1.1 cgd (void)printf(
448 1.1 cgd "A faint gleam tells you the arrow has gone through a magic tunnel!\n");
449 1.1 cgd arrow_location = (random() % room_num) + 1;
450 1.1 cgd } else
451 1.1 cgd arrow_location = next;
452 1.1 cgd } else {
453 1.1 cgd link = (random() % link_num);
454 1.1 cgd if (link == player_loc)
455 1.1 cgd (void)printf(
456 1.1 cgd "*thunk* The arrow can't find a way from %d to %d and flys back into\n\
457 1.1 cgd your room!\n",
458 1.1 cgd arrow_location, next);
459 1.1 cgd else if (cave[arrow_location].tunnel[link] > room_num)
460 1.1 cgd (void)printf(
461 1.1 cgd "*thunk* The arrow flys randomly into a magic tunnel, thence into\n\
462 1.1 cgd room %d!\n",
463 1.1 cgd cave[arrow_location].tunnel[link]);
464 1.1 cgd else
465 1.1 cgd (void)printf(
466 1.1 cgd "*thunk* The arrow can't find a way from %d to %d and flys randomly\n\
467 1.1 cgd into room %d!\n",
468 1.1 cgd arrow_location, next,
469 1.1 cgd cave[arrow_location].tunnel[link]);
470 1.1 cgd arrow_location = cave[arrow_location].tunnel[link];
471 1.1 cgd break;
472 1.1 cgd }
473 1.1 cgd chance = random() % 10;
474 1.1 cgd if (roomcnt == 3 && chance < 2) {
475 1.1 cgd (void)printf(
476 1.1 cgd "Your bowstring breaks! *twaaaaaang*\n\
477 1.1 cgd The arrow is weakly shot and can go no further!\n");
478 1.1 cgd break;
479 1.1 cgd } else if (roomcnt == 4 && chance < 6) {
480 1.1 cgd (void)printf(
481 1.1 cgd "The arrow wavers in its flight and and can go no further!\n");
482 1.1 cgd break;
483 1.1 cgd }
484 1.1 cgd }
485 1.1 cgd
486 1.1 cgd /*
487 1.1 cgd * now we've gotten into the new room let us see if El Wumpo is
488 1.1 cgd * in the same room ... if so we've a HIT and the player WON!
489 1.1 cgd */
490 1.1 cgd if (arrow_location == wumpus_loc) {
491 1.1 cgd kill_wump();
492 1.1 cgd return(1);
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd if (arrow_location == player_loc) {
496 1.1 cgd shoot_self();
497 1.1 cgd return(1);
498 1.1 cgd }
499 1.1 cgd
500 1.1 cgd if (!--arrows_left) {
501 1.1 cgd no_arrows();
502 1.1 cgd return(1);
503 1.1 cgd }
504 1.1 cgd
505 1.1 cgd {
506 1.1 cgd /* each time you shoot, it's more likely the wumpus moves */
507 1.1 cgd static int lastchance = 2;
508 1.1 cgd
509 1.1 cgd if (random() % level == EASY ? 12 : 9 < (lastchance += 2)) {
510 1.1 cgd move_wump();
511 1.1 cgd if (wumpus_loc == player_loc)
512 1.1 cgd wump_kill();
513 1.1 cgd lastchance = random() % 3;
514 1.1 cgd
515 1.1 cgd }
516 1.1 cgd }
517 1.1 cgd return(0);
518 1.1 cgd }
519 1.1 cgd
520 1.14 jsm int
521 1.14 jsm gcd(a, b)
522 1.14 jsm int a, b;
523 1.14 jsm {
524 1.14 jsm int r;
525 1.14 jsm
526 1.14 jsm r = a % b;
527 1.14 jsm if (r == 0)
528 1.14 jsm return (b);
529 1.14 jsm return (gcd(b, r));
530 1.14 jsm }
531 1.14 jsm
532 1.5 lukem void
533 1.1 cgd cave_init()
534 1.1 cgd {
535 1.5 lukem int i, j, k, link;
536 1.5 lukem int delta;
537 1.1 cgd
538 1.1 cgd /*
539 1.1 cgd * This does most of the interesting work in this program actually!
540 1.1 cgd * In this routine we'll initialize the Wumpus cave to have all rooms
541 1.1 cgd * linking to all others by stepping through our data structure once,
542 1.1 cgd * recording all forward links and backwards links too. The parallel
543 1.1 cgd * "linkcount" data structure ensures that no room ends up with more
544 1.1 cgd * than three links, regardless of the quality of the random number
545 1.1 cgd * generator that we're using.
546 1.1 cgd */
547 1.1 cgd srandom((int)time((time_t *)0));
548 1.1 cgd
549 1.1 cgd /* initialize the cave first off. */
550 1.1 cgd for (i = 1; i <= room_num; ++i)
551 1.1 cgd for (j = 0; j < link_num ; ++j)
552 1.1 cgd cave[i].tunnel[j] = -1;
553 1.1 cgd
554 1.14 jsm /*
555 1.14 jsm * Choose a random 'hop' delta for our guaranteed link.
556 1.14 jsm * To keep the cave connected, we need the greatest common divisor
557 1.14 jsm * of (delta + 1) and room_num to be 1.
558 1.14 jsm */
559 1.14 jsm do {
560 1.14 jsm delta = (random() % (room_num - 1)) + 1;
561 1.14 jsm } while (gcd(room_num, delta + 1) != 1);
562 1.1 cgd
563 1.1 cgd for (i = 1; i <= room_num; ++i) {
564 1.1 cgd link = ((i + delta) % room_num) + 1; /* connection */
565 1.1 cgd cave[i].tunnel[0] = link; /* forw link */
566 1.1 cgd cave[link].tunnel[1] = i; /* back link */
567 1.1 cgd }
568 1.1 cgd /* now fill in the rest of the cave with random connections */
569 1.1 cgd for (i = 1; i <= room_num; i++)
570 1.1 cgd for (j = 2; j < link_num ; j++) {
571 1.1 cgd if (cave[i].tunnel[j] != -1)
572 1.1 cgd continue;
573 1.1 cgd try_again: link = (random() % room_num) + 1;
574 1.1 cgd /* skip duplicates */
575 1.1 cgd for (k = 0; k < j; k++)
576 1.1 cgd if (cave[i].tunnel[k] == link)
577 1.1 cgd goto try_again;
578 1.1 cgd cave[i].tunnel[j] = link;
579 1.1 cgd if (random() % 2 == 1)
580 1.1 cgd continue;
581 1.1 cgd for (k = 0; k < link_num; ++k) {
582 1.1 cgd /* if duplicate, skip it */
583 1.1 cgd if (cave[link].tunnel[k] == i)
584 1.1 cgd k = link_num;
585 1.1 cgd
586 1.1 cgd /* if open link, use it, force exit */
587 1.1 cgd if (cave[link].tunnel[k] == -1) {
588 1.1 cgd cave[link].tunnel[k] = i;
589 1.1 cgd k = link_num;
590 1.1 cgd }
591 1.1 cgd }
592 1.1 cgd }
593 1.1 cgd /*
594 1.1 cgd * now that we're done, sort the tunnels in each of the rooms to
595 1.1 cgd * make it easier on the intrepid adventurer.
596 1.1 cgd */
597 1.1 cgd for (i = 1; i <= room_num; ++i)
598 1.1 cgd qsort(cave[i].tunnel, (u_int)link_num,
599 1.1 cgd sizeof(cave[i].tunnel[0]), int_compare);
600 1.1 cgd
601 1.1 cgd #ifdef DEBUG
602 1.1 cgd if (debug)
603 1.1 cgd for (i = 1; i <= room_num; ++i) {
604 1.1 cgd (void)printf("<room %d has tunnels to ", i);
605 1.1 cgd for (j = 0; j < link_num; ++j)
606 1.1 cgd (void)printf("%d ", cave[i].tunnel[j]);
607 1.1 cgd (void)printf(">\n");
608 1.1 cgd }
609 1.1 cgd #endif
610 1.1 cgd }
611 1.1 cgd
612 1.5 lukem void
613 1.1 cgd clear_things_in_cave()
614 1.1 cgd {
615 1.5 lukem int i;
616 1.1 cgd
617 1.1 cgd /*
618 1.1 cgd * remove bats and pits from the current cave in preparation for us
619 1.1 cgd * adding new ones via the initialize_things_in_cave() routines.
620 1.1 cgd */
621 1.1 cgd for (i = 1; i <= room_num; ++i)
622 1.1 cgd cave[i].has_a_bat = cave[i].has_a_pit = 0;
623 1.1 cgd }
624 1.1 cgd
625 1.5 lukem void
626 1.1 cgd initialize_things_in_cave()
627 1.1 cgd {
628 1.5 lukem int i, loc;
629 1.1 cgd
630 1.1 cgd /* place some bats, pits, the wumpus, and the player. */
631 1.1 cgd for (i = 0; i < bat_num; ++i) {
632 1.1 cgd do {
633 1.1 cgd loc = (random() % room_num) + 1;
634 1.1 cgd } while (cave[loc].has_a_bat);
635 1.1 cgd cave[loc].has_a_bat = 1;
636 1.1 cgd #ifdef DEBUG
637 1.1 cgd if (debug)
638 1.1 cgd (void)printf("<bat in room %d>\n", loc);
639 1.1 cgd #endif
640 1.1 cgd }
641 1.1 cgd
642 1.1 cgd for (i = 0; i < pit_num; ++i) {
643 1.1 cgd do {
644 1.1 cgd loc = (random() % room_num) + 1;
645 1.1 cgd } while (cave[loc].has_a_pit && cave[loc].has_a_bat);
646 1.1 cgd cave[loc].has_a_pit = 1;
647 1.1 cgd #ifdef DEBUG
648 1.1 cgd if (debug)
649 1.1 cgd (void)printf("<pit in room %d>\n", loc);
650 1.1 cgd #endif
651 1.1 cgd }
652 1.1 cgd
653 1.1 cgd wumpus_loc = (random() % room_num) + 1;
654 1.1 cgd #ifdef DEBUG
655 1.1 cgd if (debug)
656 1.1 cgd (void)printf("<wumpus in room %d>\n", loc);
657 1.1 cgd #endif
658 1.1 cgd
659 1.1 cgd do {
660 1.1 cgd player_loc = (random() % room_num) + 1;
661 1.1 cgd } while (player_loc == wumpus_loc || (level == HARD ?
662 1.1 cgd (link_num / room_num < 0.4 ? wump_nearby() : 0) : 0));
663 1.1 cgd }
664 1.1 cgd
665 1.5 lukem int
666 1.1 cgd getans(prompt)
667 1.5 lukem const char *prompt;
668 1.1 cgd {
669 1.1 cgd char buf[20];
670 1.1 cgd
671 1.1 cgd /*
672 1.1 cgd * simple routine to ask the yes/no question specified until the user
673 1.1 cgd * answers yes or no, then return 1 if they said 'yes' and 0 if they
674 1.1 cgd * answered 'no'.
675 1.1 cgd */
676 1.1 cgd for (;;) {
677 1.1 cgd (void)printf("%s", prompt);
678 1.1 cgd (void)fflush(stdout);
679 1.1 cgd if (!fgets(buf, sizeof(buf), stdin))
680 1.1 cgd return(0);
681 1.1 cgd if (*buf == 'N' || *buf == 'n')
682 1.1 cgd return(0);
683 1.1 cgd if (*buf == 'Y' || *buf == 'y')
684 1.1 cgd return(1);
685 1.1 cgd (void)printf(
686 1.1 cgd "I don't understand your answer; please enter 'y' or 'n'!\n");
687 1.1 cgd }
688 1.1 cgd /* NOTREACHED */
689 1.1 cgd }
690 1.1 cgd
691 1.5 lukem int
692 1.1 cgd bats_nearby()
693 1.1 cgd {
694 1.5 lukem int i;
695 1.1 cgd
696 1.1 cgd /* check for bats in the immediate vicinity */
697 1.1 cgd for (i = 0; i < link_num; ++i)
698 1.1 cgd if (cave[cave[player_loc].tunnel[i]].has_a_bat)
699 1.1 cgd return(1);
700 1.1 cgd return(0);
701 1.1 cgd }
702 1.1 cgd
703 1.5 lukem int
704 1.1 cgd pit_nearby()
705 1.1 cgd {
706 1.5 lukem int i;
707 1.1 cgd
708 1.1 cgd /* check for pits in the immediate vicinity */
709 1.1 cgd for (i = 0; i < link_num; ++i)
710 1.1 cgd if (cave[cave[player_loc].tunnel[i]].has_a_pit)
711 1.1 cgd return(1);
712 1.1 cgd return(0);
713 1.1 cgd }
714 1.1 cgd
715 1.5 lukem int
716 1.1 cgd wump_nearby()
717 1.1 cgd {
718 1.5 lukem int i, j;
719 1.1 cgd
720 1.1 cgd /* check for a wumpus within TWO caves of where we are */
721 1.1 cgd for (i = 0; i < link_num; ++i) {
722 1.1 cgd if (cave[player_loc].tunnel[i] == wumpus_loc)
723 1.1 cgd return(1);
724 1.1 cgd for (j = 0; j < link_num; ++j)
725 1.1 cgd if (cave[cave[player_loc].tunnel[i]].tunnel[j] ==
726 1.1 cgd wumpus_loc)
727 1.1 cgd return(1);
728 1.1 cgd }
729 1.1 cgd return(0);
730 1.1 cgd }
731 1.1 cgd
732 1.5 lukem void
733 1.1 cgd move_wump()
734 1.1 cgd {
735 1.1 cgd wumpus_loc = cave[wumpus_loc].tunnel[random() % link_num];
736 1.1 cgd }
737 1.1 cgd
738 1.5 lukem int
739 1.1 cgd int_compare(a, b)
740 1.5 lukem const void *a, *b;
741 1.1 cgd {
742 1.9 hubertf return(*(const int *)a < *(const int *)b ? -1 : 1);
743 1.1 cgd }
744 1.1 cgd
745 1.5 lukem void
746 1.1 cgd instructions()
747 1.1 cgd {
748 1.8 hubertf const char *pager;
749 1.8 hubertf pid_t pid;
750 1.8 hubertf int status;
751 1.8 hubertf int fd;
752 1.1 cgd
753 1.1 cgd /*
754 1.1 cgd * read the instructions file, if needed, and show the user how to
755 1.1 cgd * play this game!
756 1.1 cgd */
757 1.1 cgd if (!getans("Instructions? (y-n) "))
758 1.1 cgd return;
759 1.1 cgd
760 1.1 cgd if (access(_PATH_WUMPINFO, R_OK)) {
761 1.1 cgd (void)printf(
762 1.1 cgd "Sorry, but the instruction file seems to have disappeared in a\n\
763 1.1 cgd puff of greasy black smoke! (poof)\n");
764 1.1 cgd return;
765 1.1 cgd }
766 1.1 cgd
767 1.11 kleink if (!isatty(STDOUT_FILENO))
768 1.8 hubertf pager = "cat";
769 1.8 hubertf else {
770 1.8 hubertf if (!(pager = getenv("PAGER")) || (*pager == 0))
771 1.8 hubertf pager = _PATH_PAGER;
772 1.8 hubertf }
773 1.8 hubertf switch (pid = fork()) {
774 1.8 hubertf case 0: /* child */
775 1.8 hubertf if ((fd = open(_PATH_WUMPINFO, O_RDONLY)) == -1)
776 1.8 hubertf err(1, "open %s", _PATH_WUMPINFO);
777 1.11 kleink if (dup2(fd, STDIN_FILENO) == -1)
778 1.8 hubertf err(1, "dup2");
779 1.17 jsm (void)execl("/bin/sh", "sh", "-c", pager, (char *) NULL);
780 1.8 hubertf err(1, "exec sh -c %s", pager);
781 1.8 hubertf case -1:
782 1.8 hubertf err(1, "fork");
783 1.8 hubertf default:
784 1.8 hubertf (void)waitpid(pid, &status, 0);
785 1.8 hubertf break;
786 1.8 hubertf }
787 1.1 cgd }
788 1.1 cgd
789 1.5 lukem void
790 1.1 cgd usage()
791 1.1 cgd {
792 1.1 cgd (void)fprintf(stderr,
793 1.1 cgd "usage: wump [-h] [-a arrows] [-b bats] [-p pits] [-r rooms] [-t tunnels]\n");
794 1.1 cgd exit(1);
795 1.1 cgd }
796 1.1 cgd
797 1.1 cgd /* messages */
798 1.1 cgd
799 1.5 lukem void
800 1.1 cgd wump_kill()
801 1.1 cgd {
802 1.1 cgd (void)printf(
803 1.1 cgd "*ROAR* *chomp* *snurfle* *chomp*!\n\
804 1.1 cgd Much to the delight of the Wumpus, you walked right into his mouth,\n\
805 1.1 cgd making you one of the easiest dinners he's ever had! For you, however,\n\
806 1.1 cgd it's a rather unpleasant death. The only good thing is that it's been\n\
807 1.1 cgd so long since the evil Wumpus cleaned his teeth that you immediately\n\
808 1.1 cgd passed out from the stench!\n");
809 1.1 cgd }
810 1.1 cgd
811 1.5 lukem void
812 1.1 cgd kill_wump()
813 1.1 cgd {
814 1.1 cgd (void)printf(
815 1.1 cgd "*thwock!* *groan* *crash*\n\n\
816 1.1 cgd A horrible roar fills the cave, and you realize, with a smile, that you\n\
817 1.1 cgd have slain the evil Wumpus and won the game! You don't want to tarry for\n\
818 1.1 cgd long, however, because not only is the Wumpus famous, but the stench of\n\
819 1.1 cgd dead Wumpus is also quite well known, a stench plenty enough to slay the\n\
820 1.1 cgd mightiest adventurer at a single whiff!!\n");
821 1.1 cgd }
822 1.1 cgd
823 1.5 lukem void
824 1.1 cgd no_arrows()
825 1.1 cgd {
826 1.1 cgd (void)printf(
827 1.1 cgd "\nYou turn and look at your quiver, and realize with a sinking feeling\n\
828 1.1 cgd that you've just shot your last arrow (figuratively, too). Sensing this\n\
829 1.1 cgd with its psychic powers, the evil Wumpus rampagees through the cave, finds\n\
830 1.1 cgd you, and with a mighty *ROAR* eats you alive!\n");
831 1.1 cgd }
832 1.1 cgd
833 1.5 lukem void
834 1.1 cgd shoot_self()
835 1.1 cgd {
836 1.1 cgd (void)printf(
837 1.1 cgd "\n*Thwack!* A sudden piercing feeling informs you that the ricochet\n\
838 1.1 cgd of your wild arrow has resulted in it wedging in your side, causing\n\
839 1.1 cgd extreme agony. The evil Wumpus, with its psychic powers, realizes this\n\
840 1.1 cgd and immediately rushes to your side, not to help, alas, but to EAT YOU!\n\
841 1.1 cgd (*CHOMP*)\n");
842 1.1 cgd }
843 1.1 cgd
844 1.5 lukem void
845 1.1 cgd jump(where)
846 1.1 cgd int where;
847 1.1 cgd {
848 1.1 cgd (void)printf(
849 1.1 cgd "\nWith a jaunty step you enter the magic tunnel. As you do, you\n\
850 1.1 cgd notice that the walls are shimmering and glowing. Suddenly you feel\n\
851 1.1 cgd a very curious, warm sensation and find yourself in room %d!!\n", where);
852 1.1 cgd }
853 1.1 cgd
854 1.5 lukem void
855 1.1 cgd pit_kill()
856 1.1 cgd {
857 1.1 cgd (void)printf(
858 1.1 cgd "*AAAUUUUGGGGGHHHHHhhhhhhhhhh...*\n\
859 1.1 cgd The whistling sound and updraft as you walked into this room of the\n\
860 1.1 cgd cave apparently wasn't enough to clue you in to the presence of the\n\
861 1.1 cgd bottomless pit. You have a lot of time to reflect on this error as\n\
862 1.1 cgd you fall many miles to the core of the earth. Look on the bright side;\n\
863 1.1 cgd you can at least find out if Jules Verne was right...\n");
864 1.1 cgd }
865 1.1 cgd
866 1.5 lukem void
867 1.1 cgd pit_survive()
868 1.1 cgd {
869 1.1 cgd (void)printf(
870 1.1 cgd "Without conscious thought you grab for the side of the cave and manage\n\
871 1.1 cgd to grasp onto a rocky outcrop. Beneath your feet stretches the limitless\n\
872 1.1 cgd depths of a bottomless pit! Rock crumbles beneath your feet!\n");
873 1.1 cgd }
874