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 $
36 * $DragonFly: src/games/wump/wump.c,v 1.6 2007/04/18 18:32:12 swildner Exp $
40 * A very new version of the age old favorite Hunt-The-Wumpus game that has
41 * been a part of the BSD distribution of Unix for longer than us old folk
42 * would care to remember.
46 #include <sys/types.h>
53 #include "pathnames.h"
55 /* some defines to spec out what our wumpus cave should look like */
57 #define MAX_ARROW_SHOT_DISTANCE 6 /* +1 for '0' stopper */
58 #define MAX_LINKS_IN_ROOM 25 /* a complex cave */
60 #define MAX_ROOMS_IN_CAVE 250
61 #define ROOMS_IN_CAVE 20
62 #define MIN_ROOMS_IN_CAVE 10
64 #define LINKS_IN_ROOM 3
65 #define NUMBER_OF_ARROWS 5
69 #define EASY 1 /* levels of play */
72 /* some macro definitions for cleaner output */
74 #define plural(n) (n == 1 ? "" : "s")
76 /* simple cave data structure; +1 so we can index from '1' not '0' */
78 int tunnel
[MAX_LINKS_IN_ROOM
];
79 int has_a_pit
, has_a_bat
;
80 } cave
[MAX_ROOMS_IN_CAVE
+1];
83 * global variables so we can keep track of where the player is, how
84 * many arrows they still have, where el wumpo is, and so on...
86 int player_loc
= -1; /* player location */
87 int wumpus_loc
= -1; /* The Bad Guy location */
88 int level
= EASY
; /* level of play */
89 int arrows_left
; /* arrows unshot */
95 int pit_num
= PIT_COUNT
; /* # pits in cave */
96 int bat_num
= BAT_COUNT
; /* # bats */
97 int room_num
= ROOMS_IN_CAVE
; /* # rooms in cave */
98 int link_num
= LINKS_IN_ROOM
; /* links per room */
99 int arrow_num
= NUMBER_OF_ARROWS
; /* arrow inventory */
101 char answer
[20]; /* user input */
103 int bats_nearby(void);
104 void cave_init(void);
105 void clear_things_in_cave(void);
106 void display_room_stats(void);
107 int getans(const char *);
108 void initialize_things_in_cave(void);
109 void instructions(void);
110 int int_compare(const void *, const void *);
112 void kill_wump(void);
114 void move_wump(void);
115 void no_arrows(void);
117 int pit_nearby(void);
118 void pit_survive(void);
120 void shoot_self(void);
121 int take_action(void);
123 void wump_kill(void);
124 int wump_nearby(void);
127 main(int argc
, char **argv
)
131 /* Revoke setgid privileges */
135 while ((c
= getopt(argc
, argv
, "a:b:hp:r:t:d")) != -1)
137 while ((c
= getopt(argc
, argv
, "a:b:hp:r:t:")) != -1)
141 arrow_num
= atoi(optarg
);
144 bat_num
= atoi(optarg
);
155 pit_num
= atoi(optarg
);
158 room_num
= atoi(optarg
);
159 if (room_num
< MIN_ROOMS_IN_CAVE
) {
160 (void)fprintf(stderr
,
161 "No self-respecting wumpus would live in such a small cave!\n");
164 if (room_num
> MAX_ROOMS_IN_CAVE
) {
165 (void)fprintf(stderr
,
166 "Even wumpii can't furnish caves that large!\n");
171 link_num
= atoi(optarg
);
173 (void)fprintf(stderr
,
174 "Wumpii like extra doors in their caves!\n");
183 if (link_num
> MAX_LINKS_IN_ROOM
||
184 link_num
> room_num
- (room_num
/ 4)) {
185 (void)fprintf(stderr
,
186 "Too many tunnels! The cave collapsed!\n(Fortunately, the wumpus escaped!)\n");
191 bat_num
+= ((random() % (room_num
/ 2)) + 1);
192 pit_num
+= ((random() % (room_num
/ 2)) + 1);
195 if (bat_num
> room_num
/ 2) {
196 (void)fprintf(stderr
,
197 "The wumpus refused to enter the cave, claiming it was too crowded!\n");
201 if (pit_num
> room_num
/ 2) {
202 (void)fprintf(stderr
,
203 "The wumpus refused to enter the cave, claiming it was too dangerous!\n");
210 /* and we're OFF! da dum, da dum, da dum, da dum... */
212 "\nYou're in a cave with %d rooms and %d tunnels leading from each room.\n\
213 There are %d bat%s and %d pit%s scattered throughout the cave, and your\n\
214 quiver holds %d custom super anti-evil Wumpus arrows. Good luck.\n",
215 room_num
, link_num
, bat_num
, plural(bat_num
), pit_num
,
216 plural(pit_num
), arrow_num
);
219 initialize_things_in_cave();
220 arrows_left
= arrow_num
;
222 display_room_stats();
223 (void)printf("Move or shoot? (m-s) ");
224 (void)fflush(stdout
);
225 if (!fgets(answer
, sizeof(answer
), stdin
))
227 } while (!take_action());
229 if (!getans("\nCare to play another game? (y-n) "))
231 if (getans("In the same cave? (y-n) "))
232 clear_things_in_cave();
241 display_room_stats(void)
246 * Routine will explain what's going on with the current room, as well
247 * as describe whether there are pits, bats, & wumpii nearby. It's
248 * all pretty mindless, really.
251 "\nYou are in room %d of the cave, and have %d arrow%s left.\n",
252 player_loc
, arrows_left
, plural(arrows_left
));
255 (void)printf("*rustle* *rustle* (must be bats nearby)\n");
257 (void)printf("*whoosh* (I feel a draft from some pits).\n");
259 (void)printf("*sniff* (I can smell the evil Wumpus nearby!)\n");
261 (void)printf("There are tunnels to rooms %d, ",
262 cave
[player_loc
].tunnel
[0]);
264 for (i
= 1; i
< link_num
- 1; i
++)
265 if (cave
[player_loc
].tunnel
[i
] <= room_num
)
266 (void)printf("%d, ", cave
[player_loc
].tunnel
[i
]);
267 (void)printf("and %d.\n", cave
[player_loc
].tunnel
[link_num
- 1]);
274 * Do the action specified by the player, either 'm'ove, 's'hoot
275 * or something exceptionally bizarre and strange! Returns 1
276 * iff the player died during this turn, otherwise returns 0.
281 return(move_to(answer
+ 1));
283 case 's': /* shoot */
284 return(shoot(answer
+ 1));
292 if (random() % 15 == 1)
293 (void)printf("Que pasa?\n");
295 (void)printf("I don't understand!\n");
300 move_to(char *room_number
)
302 int i
, just_moved_by_bats
, next_room
, tunnel_available
;
305 * This is responsible for moving the player into another room in the
306 * cave as per their directions. If room_number is a null string,
307 * then we'll prompt the user for the next room to go into. Once
308 * we've moved into the room, we'll check for things like bats, pits,
309 * and so on. This routine returns 1 if something occurs that kills
310 * the player and 0 otherwise...
312 tunnel_available
= just_moved_by_bats
= 0;
313 next_room
= atoi(room_number
);
315 /* crap for magic tunnels */
316 if (next_room
== room_num
+ 1 &&
317 cave
[player_loc
].tunnel
[link_num
-1] != next_room
)
320 while (next_room
< 1 || next_room
> room_num
+ 1) {
321 if (next_room
< 0 && next_room
!= -1)
322 (void)printf("Sorry, but we're constrained to a semi-Euclidean cave!\n");
323 if (next_room
> room_num
+ 1)
324 (void)printf("What? The cave surely isn't quite that big!\n");
325 if (next_room
== room_num
+ 1 &&
326 cave
[player_loc
].tunnel
[link_num
-1] != next_room
) {
327 (void)printf("What? The cave isn't that big!\n");
330 (void)printf("To which room do you wish to move? ");
331 (void)fflush(stdout
);
332 if (!fgets(answer
, sizeof(answer
), stdin
))
334 next_room
= atoi(answer
);
337 /* now let's see if we can move to that room or not */
338 tunnel_available
= 0;
339 for (i
= 0; i
< link_num
; i
++)
340 if (cave
[player_loc
].tunnel
[i
] == next_room
)
341 tunnel_available
= 1;
343 if (!tunnel_available
) {
344 (void)printf("*Oof!* (You hit the wall)\n");
345 if (random() % 6 == 1) {
346 (void)printf("Your colorful comments awaken the wumpus!\n");
348 if (wumpus_loc
== player_loc
) {
356 /* now let's move into that room and check it out for dangers */
357 if (next_room
== room_num
+ 1)
358 jump(next_room
= (random() % room_num
) + 1);
360 player_loc
= next_room
;
362 if (next_room
== wumpus_loc
) { /* uh oh... */
366 if (cave
[next_room
].has_a_pit
) {
367 if (random() % 12 < 2) {
376 if (cave
[next_room
].has_a_bat
) {
378 "*flap* *flap* *flap* (humongous bats pick you up and move you%s!)\n",
379 just_moved_by_bats
? " again": "");
380 next_room
= player_loc
= (random() % room_num
) + 1;
381 just_moved_by_bats
= 1;
391 shoot(char *room_list
)
393 int chance
, next
, roomcnt
;
394 int j
, arrow_location
, wumplink
, ok
;
398 * Implement shooting arrows. Arrows are shot by the player indicating
399 * a space-separated list of rooms that the arrow should pass through;
400 * if any of the rooms they specify are not accessible via tunnel from
401 * the room the arrow is in, it will instead fly randomly into another
402 * room. If the player hits the wumpus, this routine will indicate
403 * such. If it misses, this routine will *move* the wumpus one room.
404 * If it's the last arrow, the player then dies... Returns 1 if the
405 * player has won or died, 0 if nothing has happened.
407 arrow_location
= player_loc
;
408 for (roomcnt
= 1;; ++roomcnt
, room_list
= NULL
) {
409 if (!(p
= strtok(room_list
, " \t\n"))) {
412 "The arrow falls to the ground at your feet!\n");
419 "The arrow wavers in its flight and and can go no further!\n");
423 for (j
= 0, ok
= 0; j
< link_num
; j
++)
424 if (cave
[arrow_location
].tunnel
[j
] == next
)
428 if (next
> room_num
) {
430 "A faint gleam tells you the arrow has gone through a magic tunnel!\n");
431 arrow_location
= (random() % room_num
) + 1;
433 arrow_location
= next
;
435 wumplink
= (random() % link_num
);
436 if (wumplink
== player_loc
)
438 "*thunk* The arrow can't find a way from %d to %d and flys back into\n\
440 arrow_location
, next
);
441 else if (cave
[arrow_location
].tunnel
[wumplink
] > room_num
)
443 "*thunk* The arrow flys randomly into a magic tunnel, thence into\n\
445 cave
[arrow_location
].tunnel
[wumplink
]);
448 "*thunk* The arrow can't find a way from %d to %d and flys randomly\n\
450 arrow_location
, next
,
451 cave
[arrow_location
].tunnel
[wumplink
]);
452 arrow_location
= cave
[arrow_location
].tunnel
[wumplink
];
455 chance
= random() % 10;
456 if (roomcnt
== 3 && chance
< 2) {
458 "Your bowstring breaks! *twaaaaaang*\n\
459 The arrow is weakly shot and can go no further!\n");
461 } else if (roomcnt
== 4 && chance
< 6) {
463 "The arrow wavers in its flight and and can go no further!\n");
469 * now we've gotten into the new room let us see if El Wumpo is
470 * in the same room ... if so we've a HIT and the player WON!
472 if (arrow_location
== wumpus_loc
) {
477 if (arrow_location
== player_loc
) {
482 if (!--arrows_left
) {
488 /* each time you shoot, it's more likely the wumpus moves */
489 static int lastchance
= 2;
491 if (random() % level
== EASY
? 12 : 9 < (lastchance
+= 2)) {
493 if (wumpus_loc
== player_loc
)
495 lastchance
= random() % 3;
505 int i
, j
, k
, wumplink
;
509 * This does most of the interesting work in this program actually!
510 * In this routine we'll initialize the Wumpus cave to have all rooms
511 * linking to all others by stepping through our data structure once,
512 * recording all forward links and backwards links too. The parallel
513 * "linkcount" data structure ensures that no room ends up with more
514 * than three links, regardless of the quality of the random number
515 * generator that we're using.
519 /* initialize the cave first off. */
520 for (i
= 1; i
<= room_num
; ++i
)
521 for (j
= 0; j
< link_num
; ++j
)
522 cave
[i
].tunnel
[j
] = -1;
524 /* choose a random 'hop' delta for our guaranteed link */
525 while (!(delta
= random() % room_num
));
527 for (i
= 1; i
<= room_num
; ++i
) {
528 wumplink
= ((i
+ delta
) % room_num
) + 1; /* connection */
529 cave
[i
].tunnel
[0] = wumplink
; /* forw link */
530 cave
[wumplink
].tunnel
[1] = i
; /* back link */
532 /* now fill in the rest of the cave with random connections */
533 for (i
= 1; i
<= room_num
; i
++)
534 for (j
= 2; j
< link_num
; j
++) {
535 if (cave
[i
].tunnel
[j
] != -1)
537 try_again
: wumplink
= (random() % room_num
) + 1;
538 /* skip duplicates */
539 for (k
= 0; k
< j
; k
++)
540 if (cave
[i
].tunnel
[k
] == wumplink
)
542 cave
[i
].tunnel
[j
] = wumplink
;
543 if (random() % 2 == 1)
545 for (k
= 0; k
< link_num
; ++k
) {
546 /* if duplicate, skip it */
547 if (cave
[wumplink
].tunnel
[k
] == i
)
550 /* if open link, use it, force exit */
551 if (cave
[wumplink
].tunnel
[k
] == -1) {
552 cave
[wumplink
].tunnel
[k
] = i
;
558 * now that we're done, sort the tunnels in each of the rooms to
559 * make it easier on the intrepid adventurer.
561 for (i
= 1; i
<= room_num
; ++i
)
562 qsort(cave
[i
].tunnel
, (u_int
)link_num
,
563 sizeof(cave
[i
].tunnel
[0]), int_compare
);
567 for (i
= 1; i
<= room_num
; ++i
) {
568 (void)printf("<room %d has tunnels to ", i
);
569 for (j
= 0; j
< link_num
; ++j
)
570 (void)printf("%d ", cave
[i
].tunnel
[j
]);
577 clear_things_in_cave(void)
582 * remove bats and pits from the current cave in preparation for us
583 * adding new ones via the initialize_things_in_cave() routines.
585 for (i
= 1; i
<= room_num
; ++i
)
586 cave
[i
].has_a_bat
= cave
[i
].has_a_pit
= 0;
590 initialize_things_in_cave(void)
594 /* place some bats, pits, the wumpus, and the player. */
595 for (i
= 0; i
< bat_num
; ++i
) {
597 loc
= (random() % room_num
) + 1;
598 } while (cave
[loc
].has_a_bat
);
599 cave
[loc
].has_a_bat
= 1;
602 (void)printf("<bat in room %d>\n", loc
);
606 for (i
= 0; i
< pit_num
; ++i
) {
608 loc
= (random() % room_num
) + 1;
609 } while (cave
[loc
].has_a_pit
&& cave
[loc
].has_a_bat
);
610 cave
[loc
].has_a_pit
= 1;
613 (void)printf("<pit in room %d>\n", loc
);
617 wumpus_loc
= (random() % room_num
) + 1;
620 (void)printf("<wumpus in room %d>\n", loc
);
624 player_loc
= (random() % room_num
) + 1;
625 } while (player_loc
== wumpus_loc
|| (level
== HARD
?
626 (link_num
/ room_num
< 0.4 ? wump_nearby() : 0) : 0));
630 getans(const char *prompt
)
635 * simple routine to ask the yes/no question specified until the user
636 * answers yes or no, then return 1 if they said 'yes' and 0 if they
640 (void)printf("%s", prompt
);
641 (void)fflush(stdout
);
642 if (!fgets(buf
, sizeof(buf
), stdin
))
644 if (*buf
== 'N' || *buf
== 'n')
646 if (*buf
== 'Y' || *buf
== 'y')
649 "I don't understand your answer; please enter 'y' or 'n'!\n");
659 /* check for bats in the immediate vicinity */
660 for (i
= 0; i
< link_num
; ++i
)
661 if (cave
[cave
[player_loc
].tunnel
[i
]].has_a_bat
)
671 /* check for pits in the immediate vicinity */
672 for (i
= 0; i
< link_num
; ++i
)
673 if (cave
[cave
[player_loc
].tunnel
[i
]].has_a_pit
)
683 /* check for a wumpus within TWO caves of where we are */
684 for (i
= 0; i
< link_num
; ++i
) {
685 if (cave
[player_loc
].tunnel
[i
] == wumpus_loc
)
687 for (j
= 0; j
< link_num
; ++j
)
688 if (cave
[cave
[player_loc
].tunnel
[i
]].tunnel
[j
] ==
698 wumpus_loc
= cave
[wumpus_loc
].tunnel
[random() % link_num
];
702 int_compare(const void *va
, const void *vb
)
709 return(a
< b
? -1 : 1);
721 * read the instructions file, if needed, and show the user how to
724 if (!getans("Instructions? (y-n) "))
727 if (access(_PATH_WUMPINFO
, R_OK
)) {
729 "Sorry, but the instruction file seems to have disappeared in a\n\
730 puff of greasy black smoke! (poof)\n");
734 if (!isatty(STDOUT_FILENO
))
737 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
740 switch (pid
= fork()) {
742 if ((fd
= open(_PATH_WUMPINFO
, O_RDONLY
)) == -1)
743 err(1, "open %s", _PATH_WUMPINFO
);
744 if (dup2(fd
, STDIN_FILENO
) == -1)
746 (void)execl("/bin/sh", "sh", "-c", pager
, NULL
);
747 err(1, "exec sh -c %s", pager
);
751 (void)waitpid(pid
, &status
, 0);
759 (void)fprintf(stderr
,
760 "usage: wump [-h] [-a arrows] [-b bats] [-p pits] [-r rooms] [-t tunnels]\n");
770 "*ROAR* *chomp* *snurfle* *chomp*!\n\
771 Much to the delight of the Wumpus, you walked right into his mouth,\n\
772 making you one of the easiest dinners he's ever had! For you, however,\n\
773 it's a rather unpleasant death. The only good thing is that it's been\n\
774 so long since the evil Wumpus cleaned his teeth that you immediately\n\
775 passed out from the stench!\n");
782 "*thwock!* *groan* *crash*\n\n\
783 A horrible roar fills the cave, and you realize, with a smile, that you\n\
784 have slain the evil Wumpus and won the game! You don't want to tarry for\n\
785 long, however, because not only is the Wumpus famous, but the stench of\n\
786 dead Wumpus is also quite well known, a stench plenty enough to slay the\n\
787 mightiest adventurer at a single whiff!!\n");
794 "\nYou turn and look at your quiver, and realize with a sinking feeling\n\
795 that you've just shot your last arrow (figuratively, too). Sensing this\n\
796 with its psychic powers, the evil Wumpus rampagees through the cave, finds\n\
797 you, and with a mighty *ROAR* eats you alive!\n");
804 "\n*Thwack!* A sudden piercing feeling informs you that the ricochet\n\
805 of your wild arrow has resulted in it wedging in your side, causing\n\
806 extreme agony. The evil Wumpus, with its psychic powers, realizes this\n\
807 and immediately rushes to your side, not to help, alas, but to EAT YOU!\n\
815 "\nWith a jaunty step you enter the magic tunnel. As you do, you\n\
816 notice that the walls are shimmering and glowing. Suddenly you feel\n\
817 a very curious, warm sensation and find yourself in room %d!!\n", where
);
824 "*AAAUUUUGGGGGHHHHHhhhhhhhhhh...*\n\
825 The whistling sound and updraft as you walked into this room of the\n\
826 cave apparently wasn't enough to clue you in to the presence of the\n\
827 bottomless pit. You have a lot of time to reflect on this error as\n\
828 you fall many miles to the core of the earth. Look on the bright side;\n\
829 you can at least find out if Jules Verne was right...\n");
836 "Without conscious thought you grab for the side of the cave and manage\n\
837 to grasp onto a rocky outcrop. Beneath your feet stretches the limitless\n\
838 depths of a bottomless pit! Rock crumbles beneath your feet!\n");