1 /* $NetBSD: wump.c,v 1.12 1999/09/12 09:02:24 jsm Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Dave Taylor, of Intuitive Systems.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
48 static char sccsid
[] = "@(#)wump.c 8.1 (Berkeley) 5/31/93";
50 __RCSID("$NetBSD: wump.c,v 1.12 1999/09/12 09:02:24 jsm Exp $");
55 * A very new version of the age old favorite Hunt-The-Wumpus game that has
56 * been a part of the BSD distribution of Unix for longer than us old folk
57 * would care to remember.
61 #include <sys/types.h>
69 #include "pathnames.h"
71 /* some defines to spec out what our wumpus cave should look like */
73 #define MAX_ARROW_SHOT_DISTANCE 6 /* +1 for '0' stopper */
74 #define MAX_LINKS_IN_ROOM 25 /* a complex cave */
76 #define MAX_ROOMS_IN_CAVE 250
77 #define ROOMS_IN_CAVE 20
78 #define MIN_ROOMS_IN_CAVE 10
80 #define LINKS_IN_ROOM 3
81 #define NUMBER_OF_ARROWS 5
85 #define EASY 1 /* levels of play */
88 /* some macro definitions for cleaner output */
90 #define plural(n) (n == 1 ? "" : "s")
92 /* simple cave data structure; +1 so we can index from '1' not '0' */
94 int tunnel
[MAX_LINKS_IN_ROOM
];
95 int has_a_pit
, has_a_bat
;
96 } cave
[MAX_ROOMS_IN_CAVE
+1];
99 * global variables so we can keep track of where the player is, how
100 * many arrows they still have, where el wumpo is, and so on...
102 int player_loc
= -1; /* player location */
103 int wumpus_loc
= -1; /* The Bad Guy location */
104 int level
= EASY
; /* level of play */
105 int arrows_left
; /* arrows unshot */
111 int pit_num
= PIT_COUNT
; /* # pits in cave */
112 int bat_num
= BAT_COUNT
; /* # bats */
113 int room_num
= ROOMS_IN_CAVE
; /* # rooms in cave */
114 int link_num
= LINKS_IN_ROOM
; /* links per room */
115 int arrow_num
= NUMBER_OF_ARROWS
; /* arrow inventory */
117 char answer
[20]; /* user input */
119 int bats_nearby
__P((void));
120 void cave_init
__P((void));
121 void clear_things_in_cave
__P((void));
122 void display_room_stats
__P((void));
123 int getans
__P((const char *));
124 void initialize_things_in_cave
__P((void));
125 void instructions
__P((void));
126 int int_compare
__P((const void *, const void *));
127 void jump
__P((int));
128 void kill_wump
__P((void));
129 int main
__P((int, char **));
130 int move_to
__P((const char *));
131 void move_wump
__P((void));
132 void no_arrows
__P((void));
133 void pit_kill
__P((void));
134 int pit_nearby
__P((void));
135 void pit_survive
__P((void));
136 int shoot
__P((char *));
137 void shoot_self
__P((void));
138 int take_action
__P((void));
139 void usage
__P((void)) __attribute__((__noreturn__
));
140 void wump_kill
__P((void));
141 int wump_nearby
__P((void));
150 /* Revoke setgid privileges */
151 setregid(getgid(), getgid());
154 while ((c
= getopt(argc
, argv
, "a:b:hp:r:t:d")) != -1)
156 while ((c
= getopt(argc
, argv
, "a:b:hp:r:t:")) != -1)
160 arrow_num
= atoi(optarg
);
163 bat_num
= atoi(optarg
);
174 pit_num
= atoi(optarg
);
177 room_num
= atoi(optarg
);
178 if (room_num
< MIN_ROOMS_IN_CAVE
) {
179 (void)fprintf(stderr
,
180 "No self-respecting wumpus would live in such a small cave!\n");
183 if (room_num
> MAX_ROOMS_IN_CAVE
) {
184 (void)fprintf(stderr
,
185 "Even wumpii can't furnish caves that large!\n");
190 link_num
= atoi(optarg
);
192 (void)fprintf(stderr
,
193 "Wumpii like extra doors in their caves!\n");
202 if (link_num
> MAX_LINKS_IN_ROOM
||
203 link_num
> room_num
- (room_num
/ 4)) {
204 (void)fprintf(stderr
,
205 "Too many tunnels! The cave collapsed!\n(Fortunately, the wumpus escaped!)\n");
210 bat_num
+= ((random() % (room_num
/ 2)) + 1);
211 pit_num
+= ((random() % (room_num
/ 2)) + 1);
214 if (bat_num
> room_num
/ 2) {
215 (void)fprintf(stderr
,
216 "The wumpus refused to enter the cave, claiming it was too crowded!\n");
220 if (pit_num
> room_num
/ 2) {
221 (void)fprintf(stderr
,
222 "The wumpus refused to enter the cave, claiming it was too dangerous!\n");
229 /* and we're OFF! da dum, da dum, da dum, da dum... */
231 "\nYou're in a cave with %d rooms and %d tunnels leading from each room.\n\
232 There are %d bat%s and %d pit%s scattered throughout the cave, and your\n\
233 quiver holds %d custom super anti-evil Wumpus arrows. Good luck.\n",
234 room_num
, link_num
, bat_num
, plural(bat_num
), pit_num
,
235 plural(pit_num
), arrow_num
);
238 initialize_things_in_cave();
239 arrows_left
= arrow_num
;
241 display_room_stats();
242 (void)printf("Move or shoot? (m-s) ");
243 (void)fflush(stdout
);
244 if (!fgets(answer
, sizeof(answer
), stdin
))
246 } while (!take_action());
248 if (!getans("\nCare to play another game? (y-n) "))
250 if (getans("In the same cave? (y-n) "))
251 clear_things_in_cave();
265 * Routine will explain what's going on with the current room, as well
266 * as describe whether there are pits, bats, & wumpii nearby. It's
267 * all pretty mindless, really.
270 "\nYou are in room %d of the cave, and have %d arrow%s left.\n",
271 player_loc
, arrows_left
, plural(arrows_left
));
274 (void)printf("*rustle* *rustle* (must be bats nearby)\n");
276 (void)printf("*whoosh* (I feel a draft from some pits).\n");
278 (void)printf("*sniff* (I can smell the evil Wumpus nearby!)\n");
280 (void)printf("There are tunnels to rooms %d, ",
281 cave
[player_loc
].tunnel
[0]);
283 for (i
= 1; i
< link_num
- 1; i
++)
284 if (cave
[player_loc
].tunnel
[i
] <= room_num
)
285 (void)printf("%d, ", cave
[player_loc
].tunnel
[i
]);
286 (void)printf("and %d.\n", cave
[player_loc
].tunnel
[link_num
- 1]);
293 * Do the action specified by the player, either 'm'ove, 's'hoot
294 * or something exceptionally bizarre and strange! Returns 1
295 * iff the player died during this turn, otherwise returns 0.
300 return(move_to(answer
+ 1));
302 case 's': /* shoot */
303 return(shoot(answer
+ 1));
311 if (random() % 15 == 1)
312 (void)printf("Que pasa?\n");
314 (void)printf("I don't understand!\n");
320 const char *room_number
;
322 int i
, just_moved_by_bats
, next_room
, tunnel_available
;
325 * This is responsible for moving the player into another room in the
326 * cave as per their directions. If room_number is a null string,
327 * then we'll prompt the user for the next room to go into. Once
328 * we've moved into the room, we'll check for things like bats, pits,
329 * and so on. This routine returns 1 if something occurs that kills
330 * the player and 0 otherwise...
332 tunnel_available
= just_moved_by_bats
= 0;
333 next_room
= atoi(room_number
);
335 /* crap for magic tunnels */
336 if (next_room
== room_num
+ 1 &&
337 cave
[player_loc
].tunnel
[link_num
-1] != next_room
)
340 while (next_room
< 1 || next_room
> room_num
+ 1) {
341 if (next_room
< 0 && next_room
!= -1)
342 (void)printf("Sorry, but we're constrained to a semi-Euclidean cave!\n");
343 if (next_room
> room_num
+ 1)
344 (void)printf("What? The cave surely isn't quite that big!\n");
345 if (next_room
== room_num
+ 1 &&
346 cave
[player_loc
].tunnel
[link_num
-1] != next_room
) {
347 (void)printf("What? The cave isn't that big!\n");
350 (void)printf("To which room do you wish to move? ");
351 (void)fflush(stdout
);
352 if (!fgets(answer
, sizeof(answer
), stdin
))
354 next_room
= atoi(answer
);
357 /* now let's see if we can move to that room or not */
358 tunnel_available
= 0;
359 for (i
= 0; i
< link_num
; i
++)
360 if (cave
[player_loc
].tunnel
[i
] == next_room
)
361 tunnel_available
= 1;
363 if (!tunnel_available
) {
364 (void)printf("*Oof!* (You hit the wall)\n");
365 if (random() % 6 == 1) {
366 (void)printf("Your colorful comments awaken the wumpus!\n");
368 if (wumpus_loc
== player_loc
) {
376 /* now let's move into that room and check it out for dangers */
377 if (next_room
== room_num
+ 1)
378 jump(next_room
= (random() % room_num
) + 1);
380 player_loc
= next_room
;
382 if (next_room
== wumpus_loc
) { /* uh oh... */
386 if (cave
[next_room
].has_a_pit
) {
387 if (random() % 12 < 2) {
396 if (cave
[next_room
].has_a_bat
) {
398 "*flap* *flap* *flap* (humongous bats pick you up and move you%s!)\n",
399 just_moved_by_bats
? " again": "");
400 next_room
= player_loc
= (random() % room_num
) + 1;
401 just_moved_by_bats
= 1;
414 int chance
, next
, roomcnt
;
415 int j
, arrow_location
, link
, ok
;
419 * Implement shooting arrows. Arrows are shot by the player indicating
420 * a space-separated list of rooms that the arrow should pass through;
421 * if any of the rooms they specify are not accessible via tunnel from
422 * the room the arrow is in, it will instead fly randomly into another
423 * room. If the player hits the wumpus, this routine will indicate
424 * such. If it misses, this routine will *move* the wumpus one room.
425 * If it's the last arrow, the player then dies... Returns 1 if the
426 * player has won or died, 0 if nothing has happened.
428 arrow_location
= player_loc
;
429 for (roomcnt
= 1;; ++roomcnt
, room_list
= NULL
) {
430 if (!(p
= strtok(room_list
, " \t\n"))) {
433 "The arrow falls to the ground at your feet!\n");
440 "The arrow wavers in its flight and and can go no further!\n");
444 for (j
= 0, ok
= 0; j
< link_num
; j
++)
445 if (cave
[arrow_location
].tunnel
[j
] == next
)
449 if (next
> room_num
) {
451 "A faint gleam tells you the arrow has gone through a magic tunnel!\n");
452 arrow_location
= (random() % room_num
) + 1;
454 arrow_location
= next
;
456 link
= (random() % link_num
);
457 if (link
== player_loc
)
459 "*thunk* The arrow can't find a way from %d to %d and flys back into\n\
461 arrow_location
, next
);
462 else if (cave
[arrow_location
].tunnel
[link
] > room_num
)
464 "*thunk* The arrow flys randomly into a magic tunnel, thence into\n\
466 cave
[arrow_location
].tunnel
[link
]);
469 "*thunk* The arrow can't find a way from %d to %d and flys randomly\n\
471 arrow_location
, next
,
472 cave
[arrow_location
].tunnel
[link
]);
473 arrow_location
= cave
[arrow_location
].tunnel
[link
];
476 chance
= random() % 10;
477 if (roomcnt
== 3 && chance
< 2) {
479 "Your bowstring breaks! *twaaaaaang*\n\
480 The arrow is weakly shot and can go no further!\n");
482 } else if (roomcnt
== 4 && chance
< 6) {
484 "The arrow wavers in its flight and and can go no further!\n");
490 * now we've gotten into the new room let us see if El Wumpo is
491 * in the same room ... if so we've a HIT and the player WON!
493 if (arrow_location
== wumpus_loc
) {
498 if (arrow_location
== player_loc
) {
503 if (!--arrows_left
) {
509 /* each time you shoot, it's more likely the wumpus moves */
510 static int lastchance
= 2;
512 if (random() % level
== EASY
? 12 : 9 < (lastchance
+= 2)) {
514 if (wumpus_loc
== player_loc
)
516 lastchance
= random() % 3;
530 * This does most of the interesting work in this program actually!
531 * In this routine we'll initialize the Wumpus cave to have all rooms
532 * linking to all others by stepping through our data structure once,
533 * recording all forward links and backwards links too. The parallel
534 * "linkcount" data structure ensures that no room ends up with more
535 * than three links, regardless of the quality of the random number
536 * generator that we're using.
538 srandom((int)time((time_t *)0));
540 /* initialize the cave first off. */
541 for (i
= 1; i
<= room_num
; ++i
)
542 for (j
= 0; j
< link_num
; ++j
)
543 cave
[i
].tunnel
[j
] = -1;
545 /* choose a random 'hop' delta for our guaranteed link */
546 while (!(delta
= random() % room_num
));
548 for (i
= 1; i
<= room_num
; ++i
) {
549 link
= ((i
+ delta
) % room_num
) + 1; /* connection */
550 cave
[i
].tunnel
[0] = link
; /* forw link */
551 cave
[link
].tunnel
[1] = i
; /* back link */
553 /* now fill in the rest of the cave with random connections */
554 for (i
= 1; i
<= room_num
; i
++)
555 for (j
= 2; j
< link_num
; j
++) {
556 if (cave
[i
].tunnel
[j
] != -1)
558 try_again
: link
= (random() % room_num
) + 1;
559 /* skip duplicates */
560 for (k
= 0; k
< j
; k
++)
561 if (cave
[i
].tunnel
[k
] == link
)
563 cave
[i
].tunnel
[j
] = link
;
564 if (random() % 2 == 1)
566 for (k
= 0; k
< link_num
; ++k
) {
567 /* if duplicate, skip it */
568 if (cave
[link
].tunnel
[k
] == i
)
571 /* if open link, use it, force exit */
572 if (cave
[link
].tunnel
[k
] == -1) {
573 cave
[link
].tunnel
[k
] = i
;
579 * now that we're done, sort the tunnels in each of the rooms to
580 * make it easier on the intrepid adventurer.
582 for (i
= 1; i
<= room_num
; ++i
)
583 qsort(cave
[i
].tunnel
, (u_int
)link_num
,
584 sizeof(cave
[i
].tunnel
[0]), int_compare
);
588 for (i
= 1; i
<= room_num
; ++i
) {
589 (void)printf("<room %d has tunnels to ", i
);
590 for (j
= 0; j
< link_num
; ++j
)
591 (void)printf("%d ", cave
[i
].tunnel
[j
]);
598 clear_things_in_cave()
603 * remove bats and pits from the current cave in preparation for us
604 * adding new ones via the initialize_things_in_cave() routines.
606 for (i
= 1; i
<= room_num
; ++i
)
607 cave
[i
].has_a_bat
= cave
[i
].has_a_pit
= 0;
611 initialize_things_in_cave()
615 /* place some bats, pits, the wumpus, and the player. */
616 for (i
= 0; i
< bat_num
; ++i
) {
618 loc
= (random() % room_num
) + 1;
619 } while (cave
[loc
].has_a_bat
);
620 cave
[loc
].has_a_bat
= 1;
623 (void)printf("<bat in room %d>\n", loc
);
627 for (i
= 0; i
< pit_num
; ++i
) {
629 loc
= (random() % room_num
) + 1;
630 } while (cave
[loc
].has_a_pit
&& cave
[loc
].has_a_bat
);
631 cave
[loc
].has_a_pit
= 1;
634 (void)printf("<pit in room %d>\n", loc
);
638 wumpus_loc
= (random() % room_num
) + 1;
641 (void)printf("<wumpus in room %d>\n", loc
);
645 player_loc
= (random() % room_num
) + 1;
646 } while (player_loc
== wumpus_loc
|| (level
== HARD
?
647 (link_num
/ room_num
< 0.4 ? wump_nearby() : 0) : 0));
657 * simple routine to ask the yes/no question specified until the user
658 * answers yes or no, then return 1 if they said 'yes' and 0 if they
662 (void)printf("%s", prompt
);
663 (void)fflush(stdout
);
664 if (!fgets(buf
, sizeof(buf
), stdin
))
666 if (*buf
== 'N' || *buf
== 'n')
668 if (*buf
== 'Y' || *buf
== 'y')
671 "I don't understand your answer; please enter 'y' or 'n'!\n");
681 /* check for bats in the immediate vicinity */
682 for (i
= 0; i
< link_num
; ++i
)
683 if (cave
[cave
[player_loc
].tunnel
[i
]].has_a_bat
)
693 /* check for pits in the immediate vicinity */
694 for (i
= 0; i
< link_num
; ++i
)
695 if (cave
[cave
[player_loc
].tunnel
[i
]].has_a_pit
)
705 /* check for a wumpus within TWO caves of where we are */
706 for (i
= 0; i
< link_num
; ++i
) {
707 if (cave
[player_loc
].tunnel
[i
] == wumpus_loc
)
709 for (j
= 0; j
< link_num
; ++j
)
710 if (cave
[cave
[player_loc
].tunnel
[i
]].tunnel
[j
] ==
720 wumpus_loc
= cave
[wumpus_loc
].tunnel
[random() % link_num
];
727 return(*(const int *)a
< *(const int *)b
? -1 : 1);
739 * read the instructions file, if needed, and show the user how to
742 if (!getans("Instructions? (y-n) "))
745 if (access(_PATH_WUMPINFO
, R_OK
)) {
747 "Sorry, but the instruction file seems to have disappeared in a\n\
748 puff of greasy black smoke! (poof)\n");
752 if (!isatty(STDOUT_FILENO
))
755 if (!(pager
= getenv("PAGER")) || (*pager
== 0))
758 switch (pid
= fork()) {
760 if ((fd
= open(_PATH_WUMPINFO
, O_RDONLY
)) == -1)
761 err(1, "open %s", _PATH_WUMPINFO
);
762 if (dup2(fd
, STDIN_FILENO
) == -1)
764 (void)execl("/bin/sh", "sh", "-c", pager
, NULL
);
765 err(1, "exec sh -c %s", pager
);
769 (void)waitpid(pid
, &status
, 0);
777 (void)fprintf(stderr
,
778 "usage: wump [-h] [-a arrows] [-b bats] [-p pits] [-r rooms] [-t tunnels]\n");
788 "*ROAR* *chomp* *snurfle* *chomp*!\n\
789 Much to the delight of the Wumpus, you walked right into his mouth,\n\
790 making you one of the easiest dinners he's ever had! For you, however,\n\
791 it's a rather unpleasant death. The only good thing is that it's been\n\
792 so long since the evil Wumpus cleaned his teeth that you immediately\n\
793 passed out from the stench!\n");
800 "*thwock!* *groan* *crash*\n\n\
801 A horrible roar fills the cave, and you realize, with a smile, that you\n\
802 have slain the evil Wumpus and won the game! You don't want to tarry for\n\
803 long, however, because not only is the Wumpus famous, but the stench of\n\
804 dead Wumpus is also quite well known, a stench plenty enough to slay the\n\
805 mightiest adventurer at a single whiff!!\n");
812 "\nYou turn and look at your quiver, and realize with a sinking feeling\n\
813 that you've just shot your last arrow (figuratively, too). Sensing this\n\
814 with its psychic powers, the evil Wumpus rampagees through the cave, finds\n\
815 you, and with a mighty *ROAR* eats you alive!\n");
822 "\n*Thwack!* A sudden piercing feeling informs you that the ricochet\n\
823 of your wild arrow has resulted in it wedging in your side, causing\n\
824 extreme agony. The evil Wumpus, with its psychic powers, realizes this\n\
825 and immediately rushes to your side, not to help, alas, but to EAT YOU!\n\
834 "\nWith a jaunty step you enter the magic tunnel. As you do, you\n\
835 notice that the walls are shimmering and glowing. Suddenly you feel\n\
836 a very curious, warm sensation and find yourself in room %d!!\n", where
);
843 "*AAAUUUUGGGGGHHHHHhhhhhhhhhh...*\n\
844 The whistling sound and updraft as you walked into this room of the\n\
845 cave apparently wasn't enough to clue you in to the presence of the\n\
846 bottomless pit. You have a lot of time to reflect on this error as\n\
847 you fall many miles to the core of the earth. Look on the bright side;\n\
848 you can at least find out if Jules Verne was right...\n");
855 "Without conscious thought you grab for the side of the cave and manage\n\
856 to grasp onto a rocky outcrop. Beneath your feet stretches the limitless\n\
857 depths of a bottomless pit! Rock crumbles beneath your feet!\n");