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