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