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