Home | History | Annotate | Line # | Download | only in wump
wump.c revision 1.10
      1 /*	$NetBSD: wump.c,v 1.10 1999/09/09 17:30:20 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.10 1999/09/09 17:30:20 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	getans __P((const char *));
    124 void	initialize_things_in_cave __P((void));
    125 void	instructions __P((void));
    126 int	int_compare __P((const void *, const void *));
    127 void	jump __P((int));
    128 void	kill_wump __P((void));
    129 int	main __P((int, char **));
    130 int	move_to __P((const char *));
    131 void	move_wump __P((void));
    132 void	no_arrows __P((void));
    133 void	pit_kill __P((void));
    134 int	pit_nearby __P((void));
    135 void	pit_survive __P((void));
    136 int	shoot __P((char *));
    137 void	shoot_self __P((void));
    138 int	take_action __P((void));
    139 void	usage __P((void)) __attribute__((__noreturn__));
    140 void	wump_kill __P((void));
    141 int	wump_nearby __P((void));
    142 
    143 int
    144 main(argc, argv)
    145 	int argc;
    146 	char **argv;
    147 {
    148 	int c;
    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 				break;
    243 		} while (!take_action());
    244 
    245 		if (!getans("\nCare to play another game? (y-n) "))
    246 			exit(0);
    247 		if (getans("In the same cave? (y-n) "))
    248 			clear_things_in_cave();
    249 		else
    250 			cave_init();
    251 	}
    252 	/* NOTREACHED */
    253 	return (0);
    254 }
    255 
    256 void
    257 display_room_stats()
    258 {
    259 	int i;
    260 
    261 	/*
    262 	 * Routine will explain what's going on with the current room, as well
    263 	 * as describe whether there are pits, bats, & wumpii nearby.  It's
    264 	 * all pretty mindless, really.
    265 	 */
    266 	(void)printf(
    267 "\nYou are in room %d of the cave, and have %d arrow%s left.\n",
    268 	    player_loc, arrows_left, plural(arrows_left));
    269 
    270 	if (bats_nearby())
    271 		(void)printf("*rustle* *rustle* (must be bats nearby)\n");
    272 	if (pit_nearby())
    273 		(void)printf("*whoosh* (I feel a draft from some pits).\n");
    274 	if (wump_nearby())
    275 		(void)printf("*sniff* (I can smell the evil Wumpus nearby!)\n");
    276 
    277 	(void)printf("There are tunnels to rooms %d, ",
    278 	   cave[player_loc].tunnel[0]);
    279 
    280 	for (i = 1; i < link_num - 1; i++)
    281 		if (cave[player_loc].tunnel[i] <= room_num)
    282 			(void)printf("%d, ", cave[player_loc].tunnel[i]);
    283 	(void)printf("and %d.\n", cave[player_loc].tunnel[link_num - 1]);
    284 }
    285 
    286 int
    287 take_action()
    288 {
    289 	/*
    290 	 * Do the action specified by the player, either 'm'ove, 's'hoot
    291 	 * or something exceptionally bizarre and strange!  Returns 1
    292 	 * iff the player died during this turn, otherwise returns 0.
    293 	 */
    294 	switch (*answer) {
    295 		case 'M':
    296 		case 'm':			/* move */
    297 			return(move_to(answer + 1));
    298 		case 'S':
    299 		case 's':			/* shoot */
    300 			return(shoot(answer + 1));
    301 		case 'Q':
    302 		case 'q':
    303 		case 'x':
    304 			exit(0);
    305 		case '\n':
    306 			return(0);
    307 		}
    308 	if (random() % 15 == 1)
    309 		(void)printf("Que pasa?\n");
    310 	else
    311 		(void)printf("I don't understand!\n");
    312 	return(0);
    313 }
    314 
    315 int
    316 move_to(room_number)
    317 	const char *room_number;
    318 {
    319 	int i, just_moved_by_bats, next_room, tunnel_available;
    320 
    321 	/*
    322 	 * This is responsible for moving the player into another room in the
    323 	 * cave as per their directions.  If room_number is a null string,
    324 	 * then we'll prompt the user for the next room to go into.   Once
    325 	 * we've moved into the room, we'll check for things like bats, pits,
    326 	 * and so on.  This routine returns 1 if something occurs that kills
    327 	 * the player and 0 otherwise...
    328 	 */
    329 	tunnel_available = just_moved_by_bats = 0;
    330 	next_room = atoi(room_number);
    331 
    332 	/* crap for magic tunnels */
    333 	if (next_room == room_num + 1 &&
    334 	    cave[player_loc].tunnel[link_num-1] != next_room)
    335 		++next_room;
    336 
    337 	while (next_room < 1 || next_room > room_num + 1) {
    338 		if (next_room < 0 && next_room != -1)
    339 (void)printf("Sorry, but we're constrained to a semi-Euclidean cave!\n");
    340 		if (next_room > room_num + 1)
    341 (void)printf("What?  The cave surely isn't quite that big!\n");
    342 		if (next_room == room_num + 1 &&
    343 		    cave[player_loc].tunnel[link_num-1] != next_room) {
    344 			(void)printf("What?  The cave isn't that big!\n");
    345 			++next_room;
    346 		}
    347 		(void)printf("To which room do you wish to move? ");
    348 		(void)fflush(stdout);
    349 		if (!fgets(answer, sizeof(answer), stdin))
    350 			return(1);
    351 		next_room = atoi(answer);
    352 	}
    353 
    354 	/* now let's see if we can move to that room or not */
    355 	tunnel_available = 0;
    356 	for (i = 0; i < link_num; i++)
    357 		if (cave[player_loc].tunnel[i] == next_room)
    358 			tunnel_available = 1;
    359 
    360 	if (!tunnel_available) {
    361 		(void)printf("*Oof!*  (You hit the wall)\n");
    362 		if (random() % 6 == 1) {
    363 (void)printf("Your colorful comments awaken the wumpus!\n");
    364 			move_wump();
    365 			if (wumpus_loc == player_loc) {
    366 				wump_kill();
    367 				return(1);
    368 			}
    369 		}
    370 		return(0);
    371 	}
    372 
    373 	/* now let's move into that room and check it out for dangers */
    374 	if (next_room == room_num + 1)
    375 		jump(next_room = (random() % room_num) + 1);
    376 
    377 	player_loc = next_room;
    378 	for (;;) {
    379 		if (next_room == wumpus_loc) {		/* uh oh... */
    380 			wump_kill();
    381 			return(1);
    382 		}
    383 		if (cave[next_room].has_a_pit) {
    384 			if (random() % 12 < 2) {
    385 				pit_survive();
    386 				return(0);
    387 			} else {
    388 				pit_kill();
    389 				return(1);
    390 			}
    391 		}
    392 
    393 		if (cave[next_room].has_a_bat) {
    394 			(void)printf(
    395 "*flap*  *flap*  *flap*  (humongous bats pick you up and move you%s!)\n",
    396 			    just_moved_by_bats ? " again": "");
    397 			next_room = player_loc = (random() % room_num) + 1;
    398 			just_moved_by_bats = 1;
    399 		}
    400 
    401 		else
    402 			break;
    403 	}
    404 	return(0);
    405 }
    406 
    407 int
    408 shoot(room_list)
    409 	char *room_list;
    410 {
    411 	int chance, next, roomcnt;
    412 	int j, arrow_location, link, ok;
    413 	char *p;
    414 
    415 	/*
    416 	 * Implement shooting arrows.  Arrows are shot by the player indicating
    417 	 * a space-separated list of rooms that the arrow should pass through;
    418 	 * if any of the rooms they specify are not accessible via tunnel from
    419 	 * the room the arrow is in, it will instead fly randomly into another
    420 	 * room.  If the player hits the wumpus, this routine will indicate
    421 	 * such.  If it misses, this routine will *move* the wumpus one room.
    422 	 * If it's the last arrow, the player then dies...  Returns 1 if the
    423 	 * player has won or died, 0 if nothing has happened.
    424 	 */
    425 	arrow_location = player_loc;
    426 	for (roomcnt = 1;; ++roomcnt, room_list = NULL) {
    427 		if (!(p = strtok(room_list, " \t\n"))) {
    428 			if (roomcnt == 1) {
    429 				(void)printf(
    430 			"The arrow falls to the ground at your feet!\n");
    431 				return(0);
    432 			} else
    433 				break;
    434 		}
    435 		if (roomcnt > 5) {
    436 			(void)printf(
    437 "The arrow wavers in its flight and and can go no further!\n");
    438 			break;
    439 		}
    440 		next = atoi(p);
    441 		for (j = 0, ok = 0; j < link_num; j++)
    442 			if (cave[arrow_location].tunnel[j] == next)
    443 				ok = 1;
    444 
    445 		if (ok) {
    446 			if (next > room_num) {
    447 				(void)printf(
    448 "A faint gleam tells you the arrow has gone through a magic tunnel!\n");
    449 				arrow_location = (random() % room_num) + 1;
    450 			} else
    451 				arrow_location = next;
    452 		} else {
    453 			link = (random() % link_num);
    454 			if (link == player_loc)
    455 				(void)printf(
    456 "*thunk*  The arrow can't find a way from %d to %d and flys back into\n\
    457 your room!\n",
    458 				    arrow_location, next);
    459 			else if (cave[arrow_location].tunnel[link] > room_num)
    460 				(void)printf(
    461 "*thunk*  The arrow flys randomly into a magic tunnel, thence into\n\
    462 room %d!\n",
    463 				    cave[arrow_location].tunnel[link]);
    464 			else
    465 				(void)printf(
    466 "*thunk*  The arrow can't find a way from %d to %d and flys randomly\n\
    467 into room %d!\n",
    468 				    arrow_location, next,
    469 				    cave[arrow_location].tunnel[link]);
    470 			arrow_location = cave[arrow_location].tunnel[link];
    471 			break;
    472 		}
    473 		chance = random() % 10;
    474 		if (roomcnt == 3 && chance < 2) {
    475 			(void)printf(
    476 "Your bowstring breaks!  *twaaaaaang*\n\
    477 The arrow is weakly shot and can go no further!\n");
    478 			break;
    479 		} else if (roomcnt == 4 && chance < 6) {
    480 			(void)printf(
    481 "The arrow wavers in its flight and and can go no further!\n");
    482 			break;
    483 		}
    484 	}
    485 
    486 	/*
    487 	 * now we've gotten into the new room let us see if El Wumpo is
    488 	 * in the same room ... if so we've a HIT and the player WON!
    489 	 */
    490 	if (arrow_location == wumpus_loc) {
    491 		kill_wump();
    492 		return(1);
    493 	}
    494 
    495 	if (arrow_location == player_loc) {
    496 		shoot_self();
    497 		return(1);
    498 	}
    499 
    500 	if (!--arrows_left) {
    501 		no_arrows();
    502 		return(1);
    503 	}
    504 
    505 	{
    506 		/* each time you shoot, it's more likely the wumpus moves */
    507 		static int lastchance = 2;
    508 
    509 		if (random() % level == EASY ? 12 : 9 < (lastchance += 2)) {
    510 			move_wump();
    511 			if (wumpus_loc == player_loc)
    512 				wump_kill();
    513 			lastchance = random() % 3;
    514 
    515 		}
    516 	}
    517 	return(0);
    518 }
    519 
    520 void
    521 cave_init()
    522 {
    523 	int i, j, k, link;
    524 	int delta;
    525 
    526 	/*
    527 	 * This does most of the interesting work in this program actually!
    528 	 * In this routine we'll initialize the Wumpus cave to have all rooms
    529 	 * linking to all others by stepping through our data structure once,
    530 	 * recording all forward links and backwards links too.  The parallel
    531 	 * "linkcount" data structure ensures that no room ends up with more
    532 	 * than three links, regardless of the quality of the random number
    533 	 * generator that we're using.
    534 	 */
    535 	srandom((int)time((time_t *)0));
    536 
    537 	/* initialize the cave first off. */
    538 	for (i = 1; i <= room_num; ++i)
    539 		for (j = 0; j < link_num ; ++j)
    540 			cave[i].tunnel[j] = -1;
    541 
    542 	/* choose a random 'hop' delta for our guaranteed link */
    543 	while (!(delta = random() % room_num));
    544 
    545 	for (i = 1; i <= room_num; ++i) {
    546 		link = ((i + delta) % room_num) + 1;	/* connection */
    547 		cave[i].tunnel[0] = link;		/* forw link */
    548 		cave[link].tunnel[1] = i;		/* back link */
    549 	}
    550 	/* now fill in the rest of the cave with random connections */
    551 	for (i = 1; i <= room_num; i++)
    552 		for (j = 2; j < link_num ; j++) {
    553 			if (cave[i].tunnel[j] != -1)
    554 				continue;
    555 try_again:		link = (random() % room_num) + 1;
    556 			/* skip duplicates */
    557 			for (k = 0; k < j; k++)
    558 				if (cave[i].tunnel[k] == link)
    559 					goto try_again;
    560 			cave[i].tunnel[j] = link;
    561 			if (random() % 2 == 1)
    562 				continue;
    563 			for (k = 0; k < link_num; ++k) {
    564 				/* if duplicate, skip it */
    565 				if (cave[link].tunnel[k] == i)
    566 					k = link_num;
    567 
    568 				/* if open link, use it, force exit */
    569 				if (cave[link].tunnel[k] == -1) {
    570 					cave[link].tunnel[k] = i;
    571 					k = link_num;
    572 				}
    573 			}
    574 		}
    575 	/*
    576 	 * now that we're done, sort the tunnels in each of the rooms to
    577 	 * make it easier on the intrepid adventurer.
    578 	 */
    579 	for (i = 1; i <= room_num; ++i)
    580 		qsort(cave[i].tunnel, (u_int)link_num,
    581 		    sizeof(cave[i].tunnel[0]), int_compare);
    582 
    583 #ifdef DEBUG
    584 	if (debug)
    585 		for (i = 1; i <= room_num; ++i) {
    586 			(void)printf("<room %d  has tunnels to ", i);
    587 			for (j = 0; j < link_num; ++j)
    588 				(void)printf("%d ", cave[i].tunnel[j]);
    589 			(void)printf(">\n");
    590 		}
    591 #endif
    592 }
    593 
    594 void
    595 clear_things_in_cave()
    596 {
    597 	int i;
    598 
    599 	/*
    600 	 * remove bats and pits from the current cave in preparation for us
    601 	 * adding new ones via the initialize_things_in_cave() routines.
    602 	 */
    603 	for (i = 1; i <= room_num; ++i)
    604 		cave[i].has_a_bat = cave[i].has_a_pit = 0;
    605 }
    606 
    607 void
    608 initialize_things_in_cave()
    609 {
    610 	int i, loc;
    611 
    612 	/* place some bats, pits, the wumpus, and the player. */
    613 	for (i = 0; i < bat_num; ++i) {
    614 		do {
    615 			loc = (random() % room_num) + 1;
    616 		} while (cave[loc].has_a_bat);
    617 		cave[loc].has_a_bat = 1;
    618 #ifdef DEBUG
    619 		if (debug)
    620 			(void)printf("<bat in room %d>\n", loc);
    621 #endif
    622 	}
    623 
    624 	for (i = 0; i < pit_num; ++i) {
    625 		do {
    626 			loc = (random() % room_num) + 1;
    627 		} while (cave[loc].has_a_pit && cave[loc].has_a_bat);
    628 		cave[loc].has_a_pit = 1;
    629 #ifdef DEBUG
    630 		if (debug)
    631 			(void)printf("<pit in room %d>\n", loc);
    632 #endif
    633 	}
    634 
    635 	wumpus_loc = (random() % room_num) + 1;
    636 #ifdef DEBUG
    637 	if (debug)
    638 		(void)printf("<wumpus in room %d>\n", loc);
    639 #endif
    640 
    641 	do {
    642 		player_loc = (random() % room_num) + 1;
    643 	} while (player_loc == wumpus_loc || (level == HARD ?
    644 	    (link_num / room_num < 0.4 ? wump_nearby() : 0) : 0));
    645 }
    646 
    647 int
    648 getans(prompt)
    649 	const char *prompt;
    650 {
    651 	char buf[20];
    652 
    653 	/*
    654 	 * simple routine to ask the yes/no question specified until the user
    655 	 * answers yes or no, then return 1 if they said 'yes' and 0 if they
    656 	 * answered 'no'.
    657 	 */
    658 	for (;;) {
    659 		(void)printf("%s", prompt);
    660 		(void)fflush(stdout);
    661 		if (!fgets(buf, sizeof(buf), stdin))
    662 			return(0);
    663 		if (*buf == 'N' || *buf == 'n')
    664 			return(0);
    665 		if (*buf == 'Y' || *buf == 'y')
    666 			return(1);
    667 		(void)printf(
    668 "I don't understand your answer; please enter 'y' or 'n'!\n");
    669 	}
    670 	/* NOTREACHED */
    671 }
    672 
    673 int
    674 bats_nearby()
    675 {
    676 	int i;
    677 
    678 	/* check for bats in the immediate vicinity */
    679 	for (i = 0; i < link_num; ++i)
    680 		if (cave[cave[player_loc].tunnel[i]].has_a_bat)
    681 			return(1);
    682 	return(0);
    683 }
    684 
    685 int
    686 pit_nearby()
    687 {
    688 	int i;
    689 
    690 	/* check for pits in the immediate vicinity */
    691 	for (i = 0; i < link_num; ++i)
    692 		if (cave[cave[player_loc].tunnel[i]].has_a_pit)
    693 			return(1);
    694 	return(0);
    695 }
    696 
    697 int
    698 wump_nearby()
    699 {
    700 	int i, j;
    701 
    702 	/* check for a wumpus within TWO caves of where we are */
    703 	for (i = 0; i < link_num; ++i) {
    704 		if (cave[player_loc].tunnel[i] == wumpus_loc)
    705 			return(1);
    706 		for (j = 0; j < link_num; ++j)
    707 			if (cave[cave[player_loc].tunnel[i]].tunnel[j] ==
    708 			    wumpus_loc)
    709 				return(1);
    710 	}
    711 	return(0);
    712 }
    713 
    714 void
    715 move_wump()
    716 {
    717 	wumpus_loc = cave[wumpus_loc].tunnel[random() % link_num];
    718 }
    719 
    720 int
    721 int_compare(a, b)
    722 	const void *a, *b;
    723 {
    724 	return(*(const int *)a < *(const int *)b ? -1 : 1);
    725 }
    726 
    727 void
    728 instructions()
    729 {
    730 	const char *pager;
    731 	pid_t pid;
    732 	int status;
    733 	int fd;
    734 
    735 	/*
    736 	 * read the instructions file, if needed, and show the user how to
    737 	 * play this game!
    738 	 */
    739 	if (!getans("Instructions? (y-n) "))
    740 		return;
    741 
    742 	if (access(_PATH_WUMPINFO, R_OK)) {
    743 		(void)printf(
    744 "Sorry, but the instruction file seems to have disappeared in a\n\
    745 puff of greasy black smoke! (poof)\n");
    746 		return;
    747 	}
    748 
    749 	if (!isatty(1))
    750 		pager = "cat";
    751 	else {
    752 		if (!(pager = getenv("PAGER")) || (*pager == 0))
    753 			pager = _PATH_PAGER;
    754 	}
    755 	switch (pid = fork()) {
    756 	case 0: /* child */
    757 		if ((fd = open(_PATH_WUMPINFO, O_RDONLY)) == -1)
    758 			err(1, "open %s", _PATH_WUMPINFO);
    759 		if (dup2(fd, 0) == -1)
    760 			err(1, "dup2");
    761 		(void)execl("/bin/sh", "sh", "-c", pager, NULL);
    762 		err(1, "exec sh -c %s", pager);
    763 	case -1:
    764 		err(1, "fork");
    765 	default:
    766 		(void)waitpid(pid, &status, 0);
    767 		break;
    768 	}
    769 }
    770 
    771 void
    772 usage()
    773 {
    774 	(void)fprintf(stderr,
    775 "usage: wump [-h] [-a arrows] [-b bats] [-p pits] [-r rooms] [-t tunnels]\n");
    776 	exit(1);
    777 }
    778 
    779 /* messages */
    780 
    781 void
    782 wump_kill()
    783 {
    784 	(void)printf(
    785 "*ROAR* *chomp* *snurfle* *chomp*!\n\
    786 Much to the delight of the Wumpus, you walked right into his mouth,\n\
    787 making you one of the easiest dinners he's ever had!  For you, however,\n\
    788 it's a rather unpleasant death.  The only good thing is that it's been\n\
    789 so long since the evil Wumpus cleaned his teeth that you immediately\n\
    790 passed out from the stench!\n");
    791 }
    792 
    793 void
    794 kill_wump()
    795 {
    796 	(void)printf(
    797 "*thwock!* *groan* *crash*\n\n\
    798 A horrible roar fills the cave, and you realize, with a smile, that you\n\
    799 have slain the evil Wumpus and won the game!  You don't want to tarry for\n\
    800 long, however, because not only is the Wumpus famous, but the stench of\n\
    801 dead Wumpus is also quite well known, a stench plenty enough to slay the\n\
    802 mightiest adventurer at a single whiff!!\n");
    803 }
    804 
    805 void
    806 no_arrows()
    807 {
    808 	(void)printf(
    809 "\nYou turn and look at your quiver, and realize with a sinking feeling\n\
    810 that you've just shot your last arrow (figuratively, too).  Sensing this\n\
    811 with its psychic powers, the evil Wumpus rampagees through the cave, finds\n\
    812 you, and with a mighty *ROAR* eats you alive!\n");
    813 }
    814 
    815 void
    816 shoot_self()
    817 {
    818 	(void)printf(
    819 "\n*Thwack!*  A sudden piercing feeling informs you that the ricochet\n\
    820 of your wild arrow has resulted in it wedging in your side, causing\n\
    821 extreme agony.  The evil Wumpus, with its psychic powers, realizes this\n\
    822 and immediately rushes to your side, not to help, alas, but to EAT YOU!\n\
    823 (*CHOMP*)\n");
    824 }
    825 
    826 void
    827 jump(where)
    828 	int where;
    829 {
    830 	(void)printf(
    831 "\nWith a jaunty step you enter the magic tunnel.  As you do, you\n\
    832 notice that the walls are shimmering and glowing.  Suddenly you feel\n\
    833 a very curious, warm sensation and find yourself in room %d!!\n", where);
    834 }
    835 
    836 void
    837 pit_kill()
    838 {
    839 	(void)printf(
    840 "*AAAUUUUGGGGGHHHHHhhhhhhhhhh...*\n\
    841 The whistling sound and updraft as you walked into this room of the\n\
    842 cave apparently wasn't enough to clue you in to the presence of the\n\
    843 bottomless pit.  You have a lot of time to reflect on this error as\n\
    844 you fall many miles to the core of the earth.  Look on the bright side;\n\
    845 you can at least find out if Jules Verne was right...\n");
    846 }
    847 
    848 void
    849 pit_survive()
    850 {
    851 	(void)printf(
    852 "Without conscious thought you grab for the side of the cave and manage\n\
    853 to grasp onto a rocky outcrop.  Beneath your feet stretches the limitless\n\
    854 depths of a bottomless pit!  Rock crumbles beneath your feet!\n");
    855 }
    856