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