Home | History | Annotate | Line # | Download | only in wump
wump.c revision 1.5
      1 /*	$NetBSD: wump.c,v 1.5 1997/10/12 03:36:42 lukem 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.5 1997/10/12 03:36:42 lukem 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));
    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 		if (cave[next_room].has_a_bat) {
    390 			(void)printf(
    391 "*flap*  *flap*  *flap*  (humongous bats pick you up and move you%s!)\n",
    392 			    just_moved_by_bats ? " again": "");
    393 			next_room = player_loc = (random() % room_num) + 1;
    394 			just_moved_by_bats = 1;
    395 		}
    396 
    397 		else
    398 			break;
    399 	}
    400 	return(0);
    401 }
    402 
    403 int
    404 shoot(room_list)
    405 	char *room_list;
    406 {
    407 	int chance, next, roomcnt;
    408 	int j, arrow_location, link, ok;
    409 	char *p;
    410 
    411 	/*
    412 	 * Implement shooting arrows.  Arrows are shot by the player indicating
    413 	 * a space-separated list of rooms that the arrow should pass through;
    414 	 * if any of the rooms they specify are not accessible via tunnel from
    415 	 * the room the arrow is in, it will instead fly randomly into another
    416 	 * room.  If the player hits the wumpus, this routine will indicate
    417 	 * such.  If it misses, this routine will *move* the wumpus one room.
    418 	 * If it's the last arrow, the player then dies...  Returns 1 if the
    419 	 * player has won or died, 0 if nothing has happened.
    420 	 */
    421 	arrow_location = player_loc;
    422 	for (roomcnt = 1;; ++roomcnt, room_list = NULL) {
    423 		if (!(p = strtok(room_list, " \t\n")))
    424 			if (roomcnt == 1) {
    425 				(void)printf(
    426 			"The arrow falls to the ground at your feet!\n");
    427 				return(0);
    428 			} else
    429 				break;
    430 		if (roomcnt > 5) {
    431 			(void)printf(
    432 "The arrow wavers in its flight and and can go no further!\n");
    433 			break;
    434 		}
    435 		next = atoi(p);
    436 		for (j = 0, ok = 0; j < link_num; j++)
    437 			if (cave[arrow_location].tunnel[j] == next)
    438 				ok = 1;
    439 
    440 		if (ok) {
    441 			if (next > room_num) {
    442 				(void)printf(
    443 "A faint gleam tells you the arrow has gone through a magic tunnel!\n");
    444 				arrow_location = (random() % room_num) + 1;
    445 			} else
    446 				arrow_location = next;
    447 		} else {
    448 			link = (random() % link_num);
    449 			if (link == player_loc)
    450 				(void)printf(
    451 "*thunk*  The arrow can't find a way from %d to %d and flys back into\n\
    452 your room!\n",
    453 				    arrow_location, next);
    454 			else if (cave[arrow_location].tunnel[link] > room_num)
    455 				(void)printf(
    456 "*thunk*  The arrow flys randomly into a magic tunnel, thence into\n\
    457 room %d!\n",
    458 				    cave[arrow_location].tunnel[link]);
    459 			else
    460 				(void)printf(
    461 "*thunk*  The arrow can't find a way from %d to %d and flys randomly\n\
    462 into room %d!\n",
    463 				    arrow_location, next,
    464 				    cave[arrow_location].tunnel[link]);
    465 			arrow_location = cave[arrow_location].tunnel[link];
    466 			break;
    467 		}
    468 		chance = random() % 10;
    469 		if (roomcnt == 3 && chance < 2) {
    470 			(void)printf(
    471 "Your bowstring breaks!  *twaaaaaang*\n\
    472 The arrow is weakly shot and can go no further!\n");
    473 			break;
    474 		} else if (roomcnt == 4 && chance < 6) {
    475 			(void)printf(
    476 "The arrow wavers in its flight and and can go no further!\n");
    477 			break;
    478 		}
    479 	}
    480 
    481 	/*
    482 	 * now we've gotten into the new room let us see if El Wumpo is
    483 	 * in the same room ... if so we've a HIT and the player WON!
    484 	 */
    485 	if (arrow_location == wumpus_loc) {
    486 		kill_wump();
    487 		return(1);
    488 	}
    489 
    490 	if (arrow_location == player_loc) {
    491 		shoot_self();
    492 		return(1);
    493 	}
    494 
    495 	if (!--arrows_left) {
    496 		no_arrows();
    497 		return(1);
    498 	}
    499 
    500 	{
    501 		/* each time you shoot, it's more likely the wumpus moves */
    502 		static int lastchance = 2;
    503 
    504 		if (random() % level == EASY ? 12 : 9 < (lastchance += 2)) {
    505 			move_wump();
    506 			if (wumpus_loc == player_loc)
    507 				wump_kill();
    508 			lastchance = random() % 3;
    509 
    510 		}
    511 	}
    512 	return(0);
    513 }
    514 
    515 void
    516 cave_init()
    517 {
    518 	int i, j, k, link;
    519 	int delta;
    520 
    521 	/*
    522 	 * This does most of the interesting work in this program actually!
    523 	 * In this routine we'll initialize the Wumpus cave to have all rooms
    524 	 * linking to all others by stepping through our data structure once,
    525 	 * recording all forward links and backwards links too.  The parallel
    526 	 * "linkcount" data structure ensures that no room ends up with more
    527 	 * than three links, regardless of the quality of the random number
    528 	 * generator that we're using.
    529 	 */
    530 	srandom((int)time((time_t *)0));
    531 
    532 	/* initialize the cave first off. */
    533 	for (i = 1; i <= room_num; ++i)
    534 		for (j = 0; j < link_num ; ++j)
    535 			cave[i].tunnel[j] = -1;
    536 
    537 	/* choose a random 'hop' delta for our guaranteed link */
    538 	while (!(delta = random() % room_num));
    539 
    540 	for (i = 1; i <= room_num; ++i) {
    541 		link = ((i + delta) % room_num) + 1;	/* connection */
    542 		cave[i].tunnel[0] = link;		/* forw link */
    543 		cave[link].tunnel[1] = i;		/* back link */
    544 	}
    545 	/* now fill in the rest of the cave with random connections */
    546 	for (i = 1; i <= room_num; i++)
    547 		for (j = 2; j < link_num ; j++) {
    548 			if (cave[i].tunnel[j] != -1)
    549 				continue;
    550 try_again:		link = (random() % room_num) + 1;
    551 			/* skip duplicates */
    552 			for (k = 0; k < j; k++)
    553 				if (cave[i].tunnel[k] == link)
    554 					goto try_again;
    555 			cave[i].tunnel[j] = link;
    556 			if (random() % 2 == 1)
    557 				continue;
    558 			for (k = 0; k < link_num; ++k) {
    559 				/* if duplicate, skip it */
    560 				if (cave[link].tunnel[k] == i)
    561 					k = link_num;
    562 
    563 				/* if open link, use it, force exit */
    564 				if (cave[link].tunnel[k] == -1) {
    565 					cave[link].tunnel[k] = i;
    566 					k = link_num;
    567 				}
    568 			}
    569 		}
    570 	/*
    571 	 * now that we're done, sort the tunnels in each of the rooms to
    572 	 * make it easier on the intrepid adventurer.
    573 	 */
    574 	for (i = 1; i <= room_num; ++i)
    575 		qsort(cave[i].tunnel, (u_int)link_num,
    576 		    sizeof(cave[i].tunnel[0]), int_compare);
    577 
    578 #ifdef DEBUG
    579 	if (debug)
    580 		for (i = 1; i <= room_num; ++i) {
    581 			(void)printf("<room %d  has tunnels to ", i);
    582 			for (j = 0; j < link_num; ++j)
    583 				(void)printf("%d ", cave[i].tunnel[j]);
    584 			(void)printf(">\n");
    585 		}
    586 #endif
    587 }
    588 
    589 void
    590 clear_things_in_cave()
    591 {
    592 	int i;
    593 
    594 	/*
    595 	 * remove bats and pits from the current cave in preparation for us
    596 	 * adding new ones via the initialize_things_in_cave() routines.
    597 	 */
    598 	for (i = 1; i <= room_num; ++i)
    599 		cave[i].has_a_bat = cave[i].has_a_pit = 0;
    600 }
    601 
    602 void
    603 initialize_things_in_cave()
    604 {
    605 	int i, loc;
    606 
    607 	/* place some bats, pits, the wumpus, and the player. */
    608 	for (i = 0; i < bat_num; ++i) {
    609 		do {
    610 			loc = (random() % room_num) + 1;
    611 		} while (cave[loc].has_a_bat);
    612 		cave[loc].has_a_bat = 1;
    613 #ifdef DEBUG
    614 		if (debug)
    615 			(void)printf("<bat in room %d>\n", loc);
    616 #endif
    617 	}
    618 
    619 	for (i = 0; i < pit_num; ++i) {
    620 		do {
    621 			loc = (random() % room_num) + 1;
    622 		} while (cave[loc].has_a_pit && cave[loc].has_a_bat);
    623 		cave[loc].has_a_pit = 1;
    624 #ifdef DEBUG
    625 		if (debug)
    626 			(void)printf("<pit in room %d>\n", loc);
    627 #endif
    628 	}
    629 
    630 	wumpus_loc = (random() % room_num) + 1;
    631 #ifdef DEBUG
    632 	if (debug)
    633 		(void)printf("<wumpus in room %d>\n", loc);
    634 #endif
    635 
    636 	do {
    637 		player_loc = (random() % room_num) + 1;
    638 	} while (player_loc == wumpus_loc || (level == HARD ?
    639 	    (link_num / room_num < 0.4 ? wump_nearby() : 0) : 0));
    640 }
    641 
    642 int
    643 getans(prompt)
    644 	const char *prompt;
    645 {
    646 	char buf[20];
    647 
    648 	/*
    649 	 * simple routine to ask the yes/no question specified until the user
    650 	 * answers yes or no, then return 1 if they said 'yes' and 0 if they
    651 	 * answered 'no'.
    652 	 */
    653 	for (;;) {
    654 		(void)printf("%s", prompt);
    655 		(void)fflush(stdout);
    656 		if (!fgets(buf, sizeof(buf), stdin))
    657 			return(0);
    658 		if (*buf == 'N' || *buf == 'n')
    659 			return(0);
    660 		if (*buf == 'Y' || *buf == 'y')
    661 			return(1);
    662 		(void)printf(
    663 "I don't understand your answer; please enter 'y' or 'n'!\n");
    664 	}
    665 	/* NOTREACHED */
    666 }
    667 
    668 int
    669 bats_nearby()
    670 {
    671 	int i;
    672 
    673 	/* check for bats in the immediate vicinity */
    674 	for (i = 0; i < link_num; ++i)
    675 		if (cave[cave[player_loc].tunnel[i]].has_a_bat)
    676 			return(1);
    677 	return(0);
    678 }
    679 
    680 int
    681 pit_nearby()
    682 {
    683 	int i;
    684 
    685 	/* check for pits in the immediate vicinity */
    686 	for (i = 0; i < link_num; ++i)
    687 		if (cave[cave[player_loc].tunnel[i]].has_a_pit)
    688 			return(1);
    689 	return(0);
    690 }
    691 
    692 int
    693 wump_nearby()
    694 {
    695 	int i, j;
    696 
    697 	/* check for a wumpus within TWO caves of where we are */
    698 	for (i = 0; i < link_num; ++i) {
    699 		if (cave[player_loc].tunnel[i] == wumpus_loc)
    700 			return(1);
    701 		for (j = 0; j < link_num; ++j)
    702 			if (cave[cave[player_loc].tunnel[i]].tunnel[j] ==
    703 			    wumpus_loc)
    704 				return(1);
    705 	}
    706 	return(0);
    707 }
    708 
    709 void
    710 move_wump()
    711 {
    712 	wumpus_loc = cave[wumpus_loc].tunnel[random() % link_num];
    713 }
    714 
    715 int
    716 int_compare(a, b)
    717 	const void *a, *b;
    718 {
    719 	return(*(int *)a < *(int *)b ? -1 : 1);
    720 }
    721 
    722 void
    723 instructions()
    724 {
    725 	char buf[120], *p;
    726 
    727 	/*
    728 	 * read the instructions file, if needed, and show the user how to
    729 	 * play this game!
    730 	 */
    731 	if (!getans("Instructions? (y-n) "))
    732 		return;
    733 
    734 	if (access(_PATH_WUMPINFO, R_OK)) {
    735 		(void)printf(
    736 "Sorry, but the instruction file seems to have disappeared in a\n\
    737 puff of greasy black smoke! (poof)\n");
    738 		return;
    739 	}
    740 
    741 	if (!(p = getenv("PAGER")) ||
    742 	    strlen(p) > sizeof(buf) + strlen(_PATH_WUMPINFO) + 5)
    743 		p = _PATH_PAGER;
    744 
    745 	(void)sprintf(buf, "%s %s", p, _PATH_WUMPINFO);
    746 	(void)system(buf);
    747 }
    748 
    749 void
    750 usage()
    751 {
    752 	(void)fprintf(stderr,
    753 "usage: wump [-h] [-a arrows] [-b bats] [-p pits] [-r rooms] [-t tunnels]\n");
    754 	exit(1);
    755 }
    756 
    757 /* messages */
    758 
    759 void
    760 wump_kill()
    761 {
    762 	(void)printf(
    763 "*ROAR* *chomp* *snurfle* *chomp*!\n\
    764 Much to the delight of the Wumpus, you walked right into his mouth,\n\
    765 making you one of the easiest dinners he's ever had!  For you, however,\n\
    766 it's a rather unpleasant death.  The only good thing is that it's been\n\
    767 so long since the evil Wumpus cleaned his teeth that you immediately\n\
    768 passed out from the stench!\n");
    769 }
    770 
    771 void
    772 kill_wump()
    773 {
    774 	(void)printf(
    775 "*thwock!* *groan* *crash*\n\n\
    776 A horrible roar fills the cave, and you realize, with a smile, that you\n\
    777 have slain the evil Wumpus and won the game!  You don't want to tarry for\n\
    778 long, however, because not only is the Wumpus famous, but the stench of\n\
    779 dead Wumpus is also quite well known, a stench plenty enough to slay the\n\
    780 mightiest adventurer at a single whiff!!\n");
    781 }
    782 
    783 void
    784 no_arrows()
    785 {
    786 	(void)printf(
    787 "\nYou turn and look at your quiver, and realize with a sinking feeling\n\
    788 that you've just shot your last arrow (figuratively, too).  Sensing this\n\
    789 with its psychic powers, the evil Wumpus rampagees through the cave, finds\n\
    790 you, and with a mighty *ROAR* eats you alive!\n");
    791 }
    792 
    793 void
    794 shoot_self()
    795 {
    796 	(void)printf(
    797 "\n*Thwack!*  A sudden piercing feeling informs you that the ricochet\n\
    798 of your wild arrow has resulted in it wedging in your side, causing\n\
    799 extreme agony.  The evil Wumpus, with its psychic powers, realizes this\n\
    800 and immediately rushes to your side, not to help, alas, but to EAT YOU!\n\
    801 (*CHOMP*)\n");
    802 }
    803 
    804 void
    805 jump(where)
    806 	int where;
    807 {
    808 	(void)printf(
    809 "\nWith a jaunty step you enter the magic tunnel.  As you do, you\n\
    810 notice that the walls are shimmering and glowing.  Suddenly you feel\n\
    811 a very curious, warm sensation and find yourself in room %d!!\n", where);
    812 }
    813 
    814 void
    815 pit_kill()
    816 {
    817 	(void)printf(
    818 "*AAAUUUUGGGGGHHHHHhhhhhhhhhh...*\n\
    819 The whistling sound and updraft as you walked into this room of the\n\
    820 cave apparently wasn't enough to clue you in to the presence of the\n\
    821 bottomless pit.  You have a lot of time to reflect on this error as\n\
    822 you fall many miles to the core of the earth.  Look on the bright side;\n\
    823 you can at least find out if Jules Verne was right...\n");
    824 }
    825 
    826 void
    827 pit_survive()
    828 {
    829 	(void)printf(
    830 "Without conscious thought you grab for the side of the cave and manage\n\
    831 to grasp onto a rocky outcrop.  Beneath your feet stretches the limitless\n\
    832 depths of a bottomless pit!  Rock crumbles beneath your feet!\n");
    833 }
    834