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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
38 * @(#)wump.c 8.1 (Berkeley) 5/31/93
39 * $FreeBSD: src/games/wump/wump.c,v 1.13.2.1 2000/08/17 06:24:54 jhb Exp $
40 * $DragonFly: src/games/wump/wump.c,v 1.6 2007/04/18 18:32:12 swildner Exp $
44 * A very new version of the age old favorite Hunt-The-Wumpus game that has
45 * been a part of the BSD distribution of Unix for longer than us old folk
46 * would care to remember.
50 #include <sys/types.h>
57 #include "pathnames.h"
59 /* some defines to spec out what our wumpus cave should look like */
61 #define MAX_ARROW_SHOT_DISTANCE 6 /* +1 for '0' stopper */
62 #define MAX_LINKS_IN_ROOM 25 /* a complex cave */
64 #define MAX_ROOMS_IN_CAVE 250
65 #define ROOMS_IN_CAVE 20
66 #define MIN_ROOMS_IN_CAVE 10
68 #define LINKS_IN_ROOM 3
69 #define NUMBER_OF_ARROWS 5
73 #define EASY 1 /* levels of play */
76 /* some macro definitions for cleaner output */
78 #define plural(n) (n == 1 ? "" : "s")
80 /* simple cave data structure; +1 so we can index from '1' not '0' */
82 int tunnel
[MAX_LINKS_IN_ROOM
];
83 int has_a_pit
, has_a_bat
;
84 } cave
[MAX_ROOMS_IN_CAVE
+1];
87 * global variables so we can keep track of where the player is, how
88 * many arrows they still have, where el wumpo is, and so on...
90 int player_loc
= -1; /* player location */
91 int wumpus_loc
= -1; /* The Bad Guy location */
92 int level
= EASY
; /* level of play */
93 int arrows_left
; /* arrows unshot */
99 int pit_num
= PIT_COUNT
; /* # pits in cave */
100 int bat_num
= BAT_COUNT
; /* # bats */
101 int room_num
= ROOMS_IN_CAVE
; /* # rooms in cave */
102 int link_num
= LINKS_IN_ROOM
; /* links per room */
103 int arrow_num
= NUMBER_OF_ARROWS
; /* arrow inventory */
105 char answer
[20]; /* user input */
107 int bats_nearby(void);
108 void cave_init(void);
109 void clear_things_in_cave(void);
110 void display_room_stats (void);
111 int getans (const char *prompt
);
112 void initialize_things_in_cave(void);
113 void instructions(void);
114 int int_compare (const void *va
, const void *vb
);
115 void jump(int where
);
116 void kill_wump(void);
117 int move_to (char *room_number
);
118 void move_wump(void);
119 void no_arrows(void);
121 int pit_nearby(void);
122 void pit_survive(void);
123 int shoot (char *room_list
);
124 void shoot_self(void);
125 int take_action(void);
127 void wump_kill(void);
128 int wump_nearby(void);
131 main(int argc
, char **argv
)
139 while ((c
= getopt(argc
, argv
, "a:b:hp:r:t:d")) != -1)
141 while ((c
= getopt(argc
, argv
, "a:b:hp:r:t:")) != -1)
145 arrow_num
= atoi(optarg
);
148 bat_num
= atoi(optarg
);
159 pit_num
= atoi(optarg
);
162 room_num
= atoi(optarg
);
163 if (room_num
< MIN_ROOMS_IN_CAVE
) {
164 (void)fprintf(stderr
,
165 "No self-respecting wumpus would live in such a small cave!\n");
168 if (room_num
> MAX_ROOMS_IN_CAVE
) {
169 (void)fprintf(stderr
,
170 "Even wumpii can't furnish caves that large!\n");
175 link_num
= atoi(optarg
);
177 (void)fprintf(stderr
,
178 "Wumpii like extra doors in their caves!\n");
187 if (link_num
> MAX_LINKS_IN_ROOM
||
188 link_num
> room_num
- (room_num
/ 4)) {
189 (void)fprintf(stderr
,
190 "Too many tunnels! The cave collapsed!\n(Fortunately, the wumpus escaped!)\n");
195 bat_num
+= ((random() % (room_num
/ 2)) + 1);
196 pit_num
+= ((random() % (room_num
/ 2)) + 1);
199 if (bat_num
> room_num
/ 2) {
200 (void)fprintf(stderr
,
201 "The wumpus refused to enter the cave, claiming it was too crowded!\n");
205 if (pit_num
> room_num
/ 2) {
206 (void)fprintf(stderr
,
207 "The wumpus refused to enter the cave, claiming it was too dangerous!\n");
214 /* and we're OFF! da dum, da dum, da dum, da dum... */
216 "\nYou're in a cave with %d rooms and %d tunnels leading from each room.\n\
217 There are %d bat%s and %d pit%s scattered throughout the cave, and your\n\
218 quiver holds %d custom super anti-evil Wumpus arrows. Good luck.\n",
219 room_num
, link_num
, bat_num
, plural(bat_num
), pit_num
,
220 plural(pit_num
), arrow_num
);
223 initialize_things_in_cave();
224 arrows_left
= arrow_num
;
226 display_room_stats();
227 (void)printf("Move or shoot? (m-s) ");
228 (void)fflush(stdout
);
229 if (!fgets(answer
, sizeof(answer
), stdin
))
231 } while (!take_action());
233 if (!getans("\nCare to play another game? (y-n) "))
235 if (getans("In the same cave? (y-n) "))
236 clear_things_in_cave();
245 display_room_stats(void)
250 * Routine will explain what's going on with the current room, as well
251 * as describe whether there are pits, bats, & wumpii nearby. It's
252 * all pretty mindless, really.
255 "\nYou are in room %d of the cave, and have %d arrow%s left.\n",
256 player_loc
, arrows_left
, plural(arrows_left
));
259 (void)printf("*rustle* *rustle* (must be bats nearby)\n");
261 (void)printf("*whoosh* (I feel a draft from some pits).\n");
263 (void)printf("*sniff* (I can smell the evil Wumpus nearby!)\n");
265 (void)printf("There are tunnels to rooms %d, ",
266 cave
[player_loc
].tunnel
[0]);
268 for (i
= 1; i
< link_num
- 1; i
++)
269 if (cave
[player_loc
].tunnel
[i
] <= room_num
)
270 (void)printf("%d, ", cave
[player_loc
].tunnel
[i
]);
271 (void)printf("and %d.\n", cave
[player_loc
].tunnel
[link_num
- 1]);
278 * Do the action specified by the player, either 'm'ove, 's'hoot
279 * or something exceptionally bizarre and strange! Returns 1
280 * iff the player died during this turn, otherwise returns 0.
285 return(move_to(answer
+ 1));
287 case 's': /* shoot */
288 return(shoot(answer
+ 1));
296 if (random() % 15 == 1)
297 (void)printf("Que pasa?\n");
299 (void)printf("I don't understand!\n");
304 move_to(char *room_number
)
306 int i
, just_moved_by_bats
, next_room
, tunnel_available
;
309 * This is responsible for moving the player into another room in the
310 * cave as per their directions. If room_number is a null string,
311 * then we'll prompt the user for the next room to go into. Once
312 * we've moved into the room, we'll check for things like bats, pits,
313 * and so on. This routine returns 1 if something occurs that kills
314 * the player and 0 otherwise...
316 tunnel_available
= just_moved_by_bats
= 0;
317 next_room
= atoi(room_number
);
319 /* crap for magic tunnels */
320 if (next_room
== room_num
+ 1 &&
321 cave
[player_loc
].tunnel
[link_num
-1] != next_room
)
324 while (next_room
< 1 || next_room
> room_num
+ 1) {
325 if (next_room
< 0 && next_room
!= -1)
326 (void)printf("Sorry, but we're constrained to a semi-Euclidean cave!\n");
327 if (next_room
> room_num
+ 1)
328 (void)printf("What? The cave surely isn't quite that big!\n");
329 if (next_room
== room_num
+ 1 &&
330 cave
[player_loc
].tunnel
[link_num
-1] != next_room
) {
331 (void)printf("What? The cave isn't that big!\n");
334 (void)printf("To which room do you wish to move? ");
335 (void)fflush(stdout
);
336 if (!fgets(answer
, sizeof(answer
), stdin
))
338 next_room
= atoi(answer
);
341 /* now let's see if we can move to that room or not */
342 tunnel_available
= 0;
343 for (i
= 0; i
< link_num
; i
++)
344 if (cave
[player_loc
].tunnel
[i
] == next_room
)
345 tunnel_available
= 1;
347 if (!tunnel_available
) {
348 (void)printf("*Oof!* (You hit the wall)\n");
349 if (random() % 6 == 1) {
350 (void)printf("Your colorful comments awaken the wumpus!\n");
352 if (wumpus_loc
== player_loc
) {
360 /* now let's move into that room and check it out for dangers */
361 if (next_room
== room_num
+ 1)
362 jump(next_room
= (random() % room_num
) + 1);
364 player_loc
= next_room
;
366 if (next_room
== wumpus_loc
) { /* uh oh... */
370 if (cave
[next_room
].has_a_pit
) {
371 if (random() % 12 < 2) {
380 if (cave
[next_room
].has_a_bat
) {
382 "*flap* *flap* *flap* (humongous bats pick you up and move you%s!)\n",
383 just_moved_by_bats
? " again": "");
384 next_room
= player_loc
= (random() % room_num
) + 1;
385 just_moved_by_bats
= 1;
395 shoot(char *room_list
)
397 int chance
, next
, roomcnt
;
398 int j
, arrow_location
, wumplink
, ok
;
402 * Implement shooting arrows. Arrows are shot by the player indicating
403 * a space-separated list of rooms that the arrow should pass through;
404 * if any of the rooms they specify are not accessible via tunnel from
405 * the room the arrow is in, it will instead fly randomly into another
406 * room. If the player hits the wumpus, this routine will indicate
407 * such. If it misses, this routine will *move* the wumpus one room.
408 * If it's the last arrow, the player then dies... Returns 1 if the
409 * player has won or died, 0 if nothing has happened.
411 arrow_location
= player_loc
;
412 for (roomcnt
= 1;; ++roomcnt
, room_list
= NULL
) {
413 if (!(p
= strtok(room_list
, " \t\n"))) {
416 "The arrow falls to the ground at your feet!\n");
423 "The arrow wavers in its flight and and can go no further!\n");
427 for (j
= 0, ok
= 0; j
< link_num
; j
++)
428 if (cave
[arrow_location
].tunnel
[j
] == next
)
432 if (next
> room_num
) {
434 "A faint gleam tells you the arrow has gone through a magic tunnel!\n");
435 arrow_location
= (random() % room_num
) + 1;
437 arrow_location
= next
;
439 wumplink
= (random() % link_num
);
440 if (wumplink
== player_loc
)
442 "*thunk* The arrow can't find a way from %d to %d and flys back into\n\
444 arrow_location
, next
);
445 else if (cave
[arrow_location
].tunnel
[wumplink
] > room_num
)
447 "*thunk* The arrow flys randomly into a magic tunnel, thence into\n\
449 cave
[arrow_location
].tunnel
[wumplink
]);
452 "*thunk* The arrow can't find a way from %d to %d and flys randomly\n\
454 arrow_location
, next
,
455 cave
[arrow_location
].tunnel
[wumplink
]);
456 arrow_location
= cave
[arrow_location
].tunnel
[wumplink
];
459 chance
= random() % 10;
460 if (roomcnt
== 3 && chance
< 2) {
462 "Your bowstring breaks! *twaaaaaang*\n\
463 The arrow is weakly shot and can go no further!\n");
465 } else if (roomcnt
== 4 && chance
< 6) {
467 "The arrow wavers in its flight and and can go no further!\n");
473 * now we've gotten into the new room let us see if El Wumpo is
474 * in the same room ... if so we've a HIT and the player WON!
476 if (arrow_location
== wumpus_loc
) {
481 if (arrow_location
== player_loc
) {
486 if (!--arrows_left
) {
492 /* each time you shoot, it's more likely the wumpus moves */
493 static int lastchance
= 2;
495 if (random() % level
== EASY
? 12 : 9 < (lastchance
+= 2)) {
497 if (wumpus_loc
== player_loc
)
499 lastchance
= random() % 3;
509 int i
, j
, k
, wumplink
;
513 * This does most of the interesting work in this program actually!
514 * In this routine we'll initialize the Wumpus cave to have all rooms
515 * linking to all others by stepping through our data structure once,
516 * recording all forward links and backwards links too. The parallel
517 * "linkcount" data structure ensures that no room ends up with more
518 * than three links, regardless of the quality of the random number
519 * generator that we're using.
523 /* initialize the cave first off. */
524 for (i
= 1; i
<= room_num
; ++i
)
525 for (j
= 0; j
< link_num
; ++j
)
526 cave
[i
].tunnel
[j
] = -1;
528 /* choose a random 'hop' delta for our guaranteed link */
529 while (!(delta
= random() % room_num
));
531 for (i
= 1; i
<= room_num
; ++i
) {
532 wumplink
= ((i
+ delta
) % room_num
) + 1; /* connection */
533 cave
[i
].tunnel
[0] = wumplink
; /* forw link */
534 cave
[wumplink
].tunnel
[1] = i
; /* back link */
536 /* now fill in the rest of the cave with random connections */
537 for (i
= 1; i
<= room_num
; i
++)
538 for (j
= 2; j
< link_num
; j
++) {
539 if (cave
[i
].tunnel
[j
] != -1)
541 try_again
: wumplink
= (random() % room_num
) + 1;
542 /* skip duplicates */
543 for (k
= 0; k
< j
; k
++)
544 if (cave
[i
].tunnel
[k
] == wumplink
)
546 cave
[i
].tunnel
[j
] = wumplink
;
547 if (random() % 2 == 1)
549 for (k
= 0; k
< link_num
; ++k
) {
550 /* if duplicate, skip it */
551 if (cave
[wumplink
].tunnel
[k
] == i
)
554 /* if open link, use it, force exit */
555 if (cave
[wumplink
].tunnel
[k
] == -1) {
556 cave
[wumplink
].tunnel
[k
] = i
;
562 * now that we're done, sort the tunnels in each of the rooms to
563 * make it easier on the intrepid adventurer.
565 for (i
= 1; i
<= room_num
; ++i
)
566 qsort(cave
[i
].tunnel
, (u_int
)link_num
,
567 sizeof(cave
[i
].tunnel
[0]), int_compare
);
571 for (i
= 1; i
<= room_num
; ++i
) {
572 (void)printf("<room %d has tunnels to ", i
);
573 for (j
= 0; j
< link_num
; ++j
)
574 (void)printf("%d ", cave
[i
].tunnel
[j
]);
581 clear_things_in_cave(void)
586 * remove bats and pits from the current cave in preparation for us
587 * adding new ones via the initialize_things_in_cave() routines.
589 for (i
= 1; i
<= room_num
; ++i
)
590 cave
[i
].has_a_bat
= cave
[i
].has_a_pit
= 0;
594 initialize_things_in_cave(void)
598 /* place some bats, pits, the wumpus, and the player. */
599 for (i
= 0; i
< bat_num
; ++i
) {
601 loc
= (random() % room_num
) + 1;
602 } while (cave
[loc
].has_a_bat
);
603 cave
[loc
].has_a_bat
= 1;
606 (void)printf("<bat in room %d>\n", loc
);
610 for (i
= 0; i
< pit_num
; ++i
) {
612 loc
= (random() % room_num
) + 1;
613 } while (cave
[loc
].has_a_pit
&& cave
[loc
].has_a_bat
);
614 cave
[loc
].has_a_pit
= 1;
617 (void)printf("<pit in room %d>\n", loc
);
621 wumpus_loc
= (random() % room_num
) + 1;
624 (void)printf("<wumpus in room %d>\n", loc
);
628 player_loc
= (random() % room_num
) + 1;
629 } while (player_loc
== wumpus_loc
|| (level
== HARD
?
630 (link_num
/ room_num
< 0.4 ? wump_nearby() : 0) : 0));
634 getans(const char *prompt
)
639 * simple routine to ask the yes/no question specified until the user
640 * answers yes or no, then return 1 if they said 'yes' and 0 if they
644 (void)printf("%s", prompt
);
645 (void)fflush(stdout
);
646 if (!fgets(buf
, sizeof(buf
), stdin
))
648 if (*buf
== 'N' || *buf
== 'n')
650 if (*buf
== 'Y' || *buf
== 'y')
653 "I don't understand your answer; please enter 'y' or 'n'!\n");
663 /* check for bats in the immediate vicinity */
664 for (i
= 0; i
< link_num
; ++i
)
665 if (cave
[cave
[player_loc
].tunnel
[i
]].has_a_bat
)
675 /* check for pits in the immediate vicinity */
676 for (i
= 0; i
< link_num
; ++i
)
677 if (cave
[cave
[player_loc
].tunnel
[i
]].has_a_pit
)
687 /* check for a wumpus within TWO caves of where we are */
688 for (i
= 0; i
< link_num
; ++i
) {
689 if (cave
[player_loc
].tunnel
[i
] == wumpus_loc
)
691 for (j
= 0; j
< link_num
; ++j
)
692 if (cave
[cave
[player_loc
].tunnel
[i
]].tunnel
[j
] ==
702 wumpus_loc
= cave
[wumpus_loc
].tunnel
[random() % link_num
];
706 int_compare(const void *va
, const void *vb
)
713 return(a
< b
? -1 : 1);
725 * read the instructions file, if needed, and show the user how to
728 if (!getans("Instructions? (y-n) "))
731 if (access(_PATH_WUMPINFO
, R_OK
)) {
733 "Sorry, but the instruction file seems to have disappeared in a\n\
734 puff of greasy black smoke! (poof)\n");
741 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
744 switch (pid
= fork()) {
746 if ((fd
= open(_PATH_WUMPINFO
, O_RDONLY
)) == -1)
747 err(1, "open %s", _PATH_WUMPINFO
);
748 if (dup2(fd
, 0) == -1)
750 (void)execl("/bin/sh", "sh", "-c", pager
, (char *) NULL
);
751 err(1, "exec sh -c %s", pager
);
755 (void)waitpid(pid
, &status
, 0);
763 (void)fprintf(stderr
,
764 "usage: wump [-h] [-a arrows] [-b bats] [-p pits] [-r rooms] [-t tunnels]\n");
774 "*ROAR* *chomp* *snurfle* *chomp*!\n\
775 Much to the delight of the Wumpus, you walked right into his mouth,\n\
776 making you one of the easiest dinners he's ever had! For you, however,\n\
777 it's a rather unpleasant death. The only good thing is that it's been\n\
778 so long since the evil Wumpus cleaned his teeth that you immediately\n\
779 passed out from the stench!\n");
786 "*thwock!* *groan* *crash*\n\n\
787 A horrible roar fills the cave, and you realize, with a smile, that you\n\
788 have slain the evil Wumpus and won the game! You don't want to tarry for\n\
789 long, however, because not only is the Wumpus famous, but the stench of\n\
790 dead Wumpus is also quite well known, a stench plenty enough to slay the\n\
791 mightiest adventurer at a single whiff!!\n");
798 "\nYou turn and look at your quiver, and realize with a sinking feeling\n\
799 that you've just shot your last arrow (figuratively, too). Sensing this\n\
800 with its psychic powers, the evil Wumpus rampagees through the cave, finds\n\
801 you, and with a mighty *ROAR* eats you alive!\n");
808 "\n*Thwack!* A sudden piercing feeling informs you that the ricochet\n\
809 of your wild arrow has resulted in it wedging in your side, causing\n\
810 extreme agony. The evil Wumpus, with its psychic powers, realizes this\n\
811 and immediately rushes to your side, not to help, alas, but to EAT YOU!\n\
819 "\nWith a jaunty step you enter the magic tunnel. As you do, you\n\
820 notice that the walls are shimmering and glowing. Suddenly you feel\n\
821 a very curious, warm sensation and find yourself in room %d!!\n", where
);
828 "*AAAUUUUGGGGGHHHHHhhhhhhhhhh...*\n\
829 The whistling sound and updraft as you walked into this room of the\n\
830 cave apparently wasn't enough to clue you in to the presence of the\n\
831 bottomless pit. You have a lot of time to reflect on this error as\n\
832 you fall many miles to the core of the earth. Look on the bright side;\n\
833 you can at least find out if Jules Verne was right...\n");
840 "Without conscious thought you grab for the side of the cave and manage\n\
841 to grasp onto a rocky outcrop. Beneath your feet stretches the limitless\n\
842 depths of a bottomless pit! Rock crumbles beneath your feet!\n");