2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Dave Taylor, of Intuitive Systems.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)wump.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/games/wump/wump.c,v 1.13.2.1 2000/08/17 06:24:54 jhb Exp $
39 * A very new version of the age old favorite Hunt-The-Wumpus game that has
40 * been a part of the BSD distribution of Unix for longer than us old folk
41 * would care to remember.
45 #include <sys/types.h>
52 #include "pathnames.h"
54 /* some defines to spec out what our wumpus cave should look like */
56 #define MAX_ARROW_SHOT_DISTANCE 6 /* +1 for '0' stopper */
57 #define MAX_LINKS_IN_ROOM 25 /* a complex cave */
59 #define MAX_ROOMS_IN_CAVE 250
60 #define ROOMS_IN_CAVE 20
61 #define MIN_ROOMS_IN_CAVE 10
63 #define LINKS_IN_ROOM 3
64 #define NUMBER_OF_ARROWS 5
68 #define EASY 1 /* levels of play */
71 /* some macro definitions for cleaner output */
73 #define plural(n) (n == 1 ? "" : "s")
75 /* simple cave data structure; +1 so we can index from '1' not '0' */
76 static struct room_record
{
77 int tunnel
[MAX_LINKS_IN_ROOM
];
78 int has_a_pit
, has_a_bat
;
79 } cave
[MAX_ROOMS_IN_CAVE
+1];
82 * global variables so we can keep track of where the player is, how
83 * many arrows they still have, where el wumpo is, and so on...
85 static int player_loc
= -1; /* player location */
86 static int wumpus_loc
= -1; /* The Bad Guy location */
87 static int level
= EASY
; /* level of play */
88 static int arrows_left
; /* arrows unshot */
94 static int pit_num
= PIT_COUNT
; /* # pits in cave */
95 static int bat_num
= BAT_COUNT
; /* # bats */
96 static int room_num
= ROOMS_IN_CAVE
; /* # rooms in cave */
97 static int link_num
= LINKS_IN_ROOM
; /* links per room */
98 static int arrow_num
= NUMBER_OF_ARROWS
; /* arrow inventory */
100 static char answer
[20]; /* user input */
102 static int bats_nearby(void);
103 static void cave_init(void);
104 static void clear_things_in_cave(void);
105 static void display_room_stats(void);
106 static int getans(const char *);
107 static void initialize_things_in_cave(void);
108 static void instructions(void);
109 static int int_compare(const void *, const void *);
110 static void jump(int);
111 static void kill_wump(void);
112 static int move_to(char *);
113 static void move_wump(void);
114 static void no_arrows(void);
115 static void pit_kill(void);
116 static int pit_nearby(void);
117 static void pit_survive(void);
118 static int shoot(char *);
119 static void shoot_self(void);
120 static int take_action(void);
121 static void usage(void) __dead2
;
122 static void wump_kill(void);
123 static int wump_nearby(void);
126 main(int argc
, char **argv
)
130 /* Revoke setgid privileges */
134 while ((c
= getopt(argc
, argv
, "a:b:hp:r:t:d")) != -1)
136 while ((c
= getopt(argc
, argv
, "a:b:hp:r:t:")) != -1)
140 arrow_num
= atoi(optarg
);
143 bat_num
= atoi(optarg
);
154 pit_num
= atoi(optarg
);
157 room_num
= atoi(optarg
);
158 if (room_num
< MIN_ROOMS_IN_CAVE
) {
160 "No self-respecting wumpus would live in such a small cave!\n");
163 if (room_num
> MAX_ROOMS_IN_CAVE
) {
165 "Even wumpii can't furnish caves that large!\n");
170 link_num
= atoi(optarg
);
173 "Wumpii like extra doors in their caves!\n");
182 if (link_num
> MAX_LINKS_IN_ROOM
||
183 link_num
> room_num
- (room_num
/ 4)) {
185 "Too many tunnels! The cave collapsed!\n(Fortunately, the wumpus escaped!)\n");
190 bat_num
+= ((random() % (room_num
/ 2)) + 1);
191 pit_num
+= ((random() % (room_num
/ 2)) + 1);
194 if (bat_num
> room_num
/ 2) {
196 "The wumpus refused to enter the cave, claiming it was too crowded!\n");
200 if (pit_num
> room_num
/ 2) {
202 "The wumpus refused to enter the cave, claiming it was too dangerous!\n");
209 /* and we're OFF! da dum, da dum, da dum, da dum... */
211 "\nYou're in a cave with %d rooms and %d tunnels leading from each room.\n\
212 There are %d bat%s and %d pit%s scattered throughout the cave, and your\n\
213 quiver holds %d custom super anti-evil Wumpus arrows. Good luck.\n",
214 room_num
, link_num
, bat_num
, plural(bat_num
), pit_num
,
215 plural(pit_num
), arrow_num
);
218 initialize_things_in_cave();
219 arrows_left
= arrow_num
;
221 display_room_stats();
222 printf("Move or shoot? (m-s) ");
224 if (!fgets(answer
, sizeof(answer
), stdin
))
226 } while (!take_action());
228 if (!getans("\nCare to play another game? (y-n) "))
230 if (getans("In the same cave? (y-n) "))
231 clear_things_in_cave();
240 display_room_stats(void)
245 * Routine will explain what's going on with the current room, as well
246 * as describe whether there are pits, bats, & wumpii nearby. It's
247 * all pretty mindless, really.
250 "\nYou are in room %d of the cave, and have %d arrow%s left.\n",
251 player_loc
, arrows_left
, plural(arrows_left
));
254 printf("*rustle* *rustle* (must be bats nearby)\n");
256 printf("*whoosh* (I feel a draft from some pits).\n");
258 printf("*sniff* (I can smell the evil Wumpus nearby!)\n");
260 printf("There are tunnels to rooms %d, ",
261 cave
[player_loc
].tunnel
[0]);
263 for (i
= 1; i
< link_num
- 1; i
++)
264 if (cave
[player_loc
].tunnel
[i
] <= room_num
)
265 printf("%d, ", cave
[player_loc
].tunnel
[i
]);
266 printf("and %d.\n", cave
[player_loc
].tunnel
[link_num
- 1]);
273 * Do the action specified by the player, either 'm'ove, 's'hoot
274 * or something exceptionally bizarre and strange! Returns 1
275 * iff the player died during this turn, otherwise returns 0.
280 return(move_to(answer
+ 1));
282 case 's': /* shoot */
283 return(shoot(answer
+ 1));
291 if (random() % 15 == 1)
292 printf("Que pasa?\n");
294 printf("I don't understand!\n");
299 move_to(char *room_number
)
301 int i
, just_moved_by_bats
, next_room
, tunnel_available
;
304 * This is responsible for moving the player into another room in the
305 * cave as per their directions. If room_number is a null string,
306 * then we'll prompt the user for the next room to go into. Once
307 * we've moved into the room, we'll check for things like bats, pits,
308 * and so on. This routine returns 1 if something occurs that kills
309 * the player and 0 otherwise...
311 tunnel_available
= just_moved_by_bats
= 0;
312 next_room
= atoi(room_number
);
314 /* crap for magic tunnels */
315 if (next_room
== room_num
+ 1 &&
316 cave
[player_loc
].tunnel
[link_num
-1] != next_room
)
319 while (next_room
< 1 || next_room
> room_num
+ 1) {
320 if (next_room
< 0 && next_room
!= -1)
321 printf("Sorry, but we're constrained to a semi-Euclidean cave!\n");
322 if (next_room
> room_num
+ 1)
323 printf("What? The cave surely isn't quite that big!\n");
324 if (next_room
== room_num
+ 1 &&
325 cave
[player_loc
].tunnel
[link_num
-1] != next_room
) {
326 printf("What? The cave isn't that big!\n");
329 printf("To which room do you wish to move? ");
331 if (!fgets(answer
, sizeof(answer
), stdin
))
333 next_room
= atoi(answer
);
336 /* now let's see if we can move to that room or not */
337 tunnel_available
= 0;
338 for (i
= 0; i
< link_num
; i
++)
339 if (cave
[player_loc
].tunnel
[i
] == next_room
)
340 tunnel_available
= 1;
342 if (!tunnel_available
) {
343 printf("*Oof!* (You hit the wall)\n");
344 if (random() % 6 == 1) {
345 printf("Your colorful comments awaken the wumpus!\n");
347 if (wumpus_loc
== player_loc
) {
355 /* now let's move into that room and check it out for dangers */
356 if (next_room
== room_num
+ 1)
357 jump(next_room
= (random() % room_num
) + 1);
359 player_loc
= next_room
;
361 if (next_room
== wumpus_loc
) { /* uh oh... */
365 if (cave
[next_room
].has_a_pit
) {
366 if (random() % 12 < 2) {
375 if (cave
[next_room
].has_a_bat
) {
377 "*flap* *flap* *flap* (humongous bats pick you up and move you%s!)\n",
378 just_moved_by_bats
? " again": "");
379 next_room
= player_loc
= (random() % room_num
) + 1;
380 just_moved_by_bats
= 1;
390 shoot(char *room_list
)
392 int chance
, next
, roomcnt
;
393 int j
, arrow_location
, wumplink
, ok
;
397 * Implement shooting arrows. Arrows are shot by the player indicating
398 * a space-separated list of rooms that the arrow should pass through;
399 * if any of the rooms they specify are not accessible via tunnel from
400 * the room the arrow is in, it will instead fly randomly into another
401 * room. If the player hits the wumpus, this routine will indicate
402 * such. If it misses, this routine will *move* the wumpus one room.
403 * If it's the last arrow, the player then dies... Returns 1 if the
404 * player has won or died, 0 if nothing has happened.
406 arrow_location
= player_loc
;
407 for (roomcnt
= 1;; ++roomcnt
, room_list
= NULL
) {
408 if (!(p
= strtok(room_list
, " \t\n"))) {
411 "The arrow falls to the ground at your feet!\n");
418 "The arrow wavers in its flight and can go no further!\n");
422 for (j
= 0, ok
= 0; j
< link_num
; j
++)
423 if (cave
[arrow_location
].tunnel
[j
] == next
)
427 if (next
> room_num
) {
429 "A faint gleam tells you the arrow has gone through a magic tunnel!\n");
430 arrow_location
= (random() % room_num
) + 1;
432 arrow_location
= next
;
434 wumplink
= (random() % link_num
);
435 if (wumplink
== player_loc
)
437 "*thunk* The arrow can't find a way from %d to %d and flys back into\n\
439 arrow_location
, next
);
440 else if (cave
[arrow_location
].tunnel
[wumplink
] > room_num
)
442 "*thunk* The arrow flys randomly into a magic tunnel, thence into\n\
444 cave
[arrow_location
].tunnel
[wumplink
]);
447 "*thunk* The arrow can't find a way from %d to %d and flys randomly\n\
449 arrow_location
, next
,
450 cave
[arrow_location
].tunnel
[wumplink
]);
451 arrow_location
= cave
[arrow_location
].tunnel
[wumplink
];
454 chance
= random() % 10;
455 if (roomcnt
== 3 && chance
< 2) {
457 "Your bowstring breaks! *twaaaaaang*\n\
458 The arrow is weakly shot and can go no further!\n");
460 } else if (roomcnt
== 4 && chance
< 6) {
462 "The arrow wavers in its flight and can go no further!\n");
468 * now we've gotten into the new room let us see if El Wumpo is
469 * in the same room ... if so we've a HIT and the player WON!
471 if (arrow_location
== wumpus_loc
) {
476 if (arrow_location
== player_loc
) {
481 if (!--arrows_left
) {
487 /* each time you shoot, it's more likely the wumpus moves */
488 static int lastchance
= 2;
490 if (random() % (level
== EASY
? 12 : 9) < (lastchance
+= 2)) {
492 if (wumpus_loc
== player_loc
)
494 lastchance
= random() % 3;
504 int i
, j
, k
, wumplink
;
508 * This does most of the interesting work in this program actually!
509 * In this routine we'll initialize the Wumpus cave to have all rooms
510 * linking to all others by stepping through our data structure once,
511 * recording all forward links and backwards links too. The parallel
512 * "linkcount" data structure ensures that no room ends up with more
513 * than three links, regardless of the quality of the random number
514 * generator that we're using.
518 /* initialize the cave first off. */
519 for (i
= 1; i
<= room_num
; ++i
)
520 for (j
= 0; j
< link_num
; ++j
)
521 cave
[i
].tunnel
[j
] = -1;
523 /* choose a random 'hop' delta for our guaranteed link */
524 while (!(delta
= random() % room_num
));
526 for (i
= 1; i
<= room_num
; ++i
) {
527 wumplink
= ((i
+ delta
) % room_num
) + 1; /* connection */
528 cave
[i
].tunnel
[0] = wumplink
; /* forw link */
529 cave
[wumplink
].tunnel
[1] = i
; /* back link */
531 /* now fill in the rest of the cave with random connections */
532 for (i
= 1; i
<= room_num
; i
++)
533 for (j
= 2; j
< link_num
; j
++) {
534 if (cave
[i
].tunnel
[j
] != -1)
536 try_again
: wumplink
= (random() % room_num
) + 1;
537 /* skip duplicates */
538 for (k
= 0; k
< j
; k
++)
539 if (cave
[i
].tunnel
[k
] == wumplink
)
541 cave
[i
].tunnel
[j
] = wumplink
;
542 if (random() % 2 == 1)
544 for (k
= 0; k
< link_num
; ++k
) {
545 /* if duplicate, skip it */
546 if (cave
[wumplink
].tunnel
[k
] == i
)
549 /* if open link, use it, force exit */
550 if (cave
[wumplink
].tunnel
[k
] == -1) {
551 cave
[wumplink
].tunnel
[k
] = i
;
557 * now that we're done, sort the tunnels in each of the rooms to
558 * make it easier on the intrepid adventurer.
560 for (i
= 1; i
<= room_num
; ++i
)
561 qsort(cave
[i
].tunnel
, (u_int
)link_num
,
562 sizeof(cave
[i
].tunnel
[0]), int_compare
);
566 for (i
= 1; i
<= room_num
; ++i
) {
567 printf("<room %d has tunnels to ", i
);
568 for (j
= 0; j
< link_num
; ++j
)
569 printf("%d ", cave
[i
].tunnel
[j
]);
576 clear_things_in_cave(void)
581 * remove bats and pits from the current cave in preparation for us
582 * adding new ones via the initialize_things_in_cave() routines.
584 for (i
= 1; i
<= room_num
; ++i
)
585 cave
[i
].has_a_bat
= cave
[i
].has_a_pit
= 0;
589 initialize_things_in_cave(void)
593 /* place some bats, pits, the wumpus, and the player. */
594 for (i
= 0; i
< bat_num
; ++i
) {
596 loc
= (random() % room_num
) + 1;
597 } while (cave
[loc
].has_a_bat
);
598 cave
[loc
].has_a_bat
= 1;
601 printf("<bat in room %d>\n", loc
);
605 for (i
= 0; i
< pit_num
; ++i
) {
607 loc
= (random() % room_num
) + 1;
608 } while (cave
[loc
].has_a_pit
&& cave
[loc
].has_a_bat
);
609 cave
[loc
].has_a_pit
= 1;
612 printf("<pit in room %d>\n", loc
);
616 wumpus_loc
= (random() % room_num
) + 1;
619 printf("<wumpus in room %d>\n", loc
);
623 player_loc
= (random() % room_num
) + 1;
624 } while (player_loc
== wumpus_loc
|| (level
== HARD
?
625 (link_num
/ room_num
< 0.4 ? wump_nearby() : 0) : 0));
629 getans(const char *prompt
)
634 * simple routine to ask the yes/no question specified until the user
635 * answers yes or no, then return 1 if they said 'yes' and 0 if they
639 printf("%s", prompt
);
641 if (!fgets(buf
, sizeof(buf
), stdin
))
643 if (*buf
== 'N' || *buf
== 'n')
645 if (*buf
== 'Y' || *buf
== 'y')
648 "I don't understand your answer; please enter 'y' or 'n'!\n");
658 /* check for bats in the immediate vicinity */
659 for (i
= 0; i
< link_num
; ++i
)
660 if (cave
[cave
[player_loc
].tunnel
[i
]].has_a_bat
)
670 /* check for pits in the immediate vicinity */
671 for (i
= 0; i
< link_num
; ++i
)
672 if (cave
[cave
[player_loc
].tunnel
[i
]].has_a_pit
)
682 /* check for a wumpus within TWO caves of where we are */
683 for (i
= 0; i
< link_num
; ++i
) {
684 if (cave
[player_loc
].tunnel
[i
] == wumpus_loc
)
686 for (j
= 0; j
< link_num
; ++j
)
687 if (cave
[cave
[player_loc
].tunnel
[i
]].tunnel
[j
] ==
697 wumpus_loc
= cave
[wumpus_loc
].tunnel
[random() % link_num
];
701 int_compare(const void *va
, const void *vb
)
708 return(a
< b
? -1 : 1);
720 * read the instructions file, if needed, and show the user how to
723 if (!getans("Instructions? (y-n) "))
726 if (access(_PATH_WUMPINFO
, R_OK
)) {
728 "Sorry, but the instruction file seems to have disappeared in a\n\
729 puff of greasy black smoke! (poof)\n");
733 if (!isatty(STDOUT_FILENO
))
736 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
739 switch (pid
= fork()) {
741 if ((fd
= open(_PATH_WUMPINFO
, O_RDONLY
)) == -1)
742 err(1, "open %s", _PATH_WUMPINFO
);
743 if (dup2(fd
, STDIN_FILENO
) == -1)
745 execl("/bin/sh", "sh", "-c", pager
, NULL
);
746 err(1, "exec sh -c %s", pager
);
750 waitpid(pid
, &status
, 0);
759 "usage: wump [-h] [-a arrows] [-b bats] [-p pits] [-r rooms] [-t tunnels]\n");
769 "*ROAR* *chomp* *snurfle* *chomp*!\n\
770 Much to the delight of the Wumpus, you walked right into his mouth,\n\
771 making you one of the easiest dinners he's ever had! For you, however,\n\
772 it's a rather unpleasant death. The only good thing is that it's been\n\
773 so long since the evil Wumpus cleaned his teeth that you immediately\n\
774 passed out from the stench!\n");
781 "*thwock!* *groan* *crash*\n\n\
782 A horrible roar fills the cave, and you realize, with a smile, that you\n\
783 have slain the evil Wumpus and won the game! You don't want to tarry for\n\
784 long, however, because not only is the Wumpus famous, but the stench of\n\
785 dead Wumpus is also quite well known, a stench plenty enough to slay the\n\
786 mightiest adventurer at a single whiff!!\n");
793 "\nYou turn and look at your quiver, and realize with a sinking feeling\n\
794 that you've just shot your last arrow (figuratively, too). Sensing this\n\
795 with its psychic powers, the evil Wumpus rampagees through the cave, finds\n\
796 you, and with a mighty *ROAR* eats you alive!\n");
803 "\n*Thwack!* A sudden piercing feeling informs you that the ricochet\n\
804 of your wild arrow has resulted in it wedging in your side, causing\n\
805 extreme agony. The evil Wumpus, with its psychic powers, realizes this\n\
806 and immediately rushes to your side, not to help, alas, but to EAT YOU!\n\
814 "\nWith a jaunty step you enter the magic tunnel. As you do, you\n\
815 notice that the walls are shimmering and glowing. Suddenly you feel\n\
816 a very curious, warm sensation and find yourself in room %d!!\n", where
);
823 "*AAAUUUUGGGGGHHHHHhhhhhhhhhh...*\n\
824 The whistling sound and updraft as you walked into this room of the\n\
825 cave apparently wasn't enough to clue you in to the presence of the\n\
826 bottomless pit. You have a lot of time to reflect on this error as\n\
827 you fall many miles to the core of the earth. Look on the bright side;\n\
828 you can at least find out if Jules Verne was right...\n");
835 "Without conscious thought you grab for the side of the cave and manage\n\
836 to grasp onto a rocky outcrop. Beneath your feet stretches the limitless\n\
837 depths of a bottomless pit! Rock crumbles beneath your feet!\n");