2 * superhack.c --- modern version of a classic adventure game
4 * Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 * My update of a classic adventure game. This code is no relation to
7 * the elaborate dungeon game called `Hack'.
9 * Any resemblance to persons living or dead is strictly coincidental. And
10 * if you believe *that*...
18 static int j, k, scratchloc, pies;
19 static char inp[BUFSIZ]; /* common input buffer */
37 static int cave[20][3] =
61 #define FNA() (rand() % 20)
63 #define FNB() (rand() % 3)
65 #define FNC() (rand() % 4)
70 (void) printf("%s? ", prompt);
71 if(fgets(inp, sizeof(inp), stdin))
72 return(tolower(inp[0]));
79 #define PM(x) puts(x);
81 void print_instructions()
85 PM("Welcome to `Hunt the Superhack'\n")
87 PM(" The superhack lives on the 9th floor of 45 Technology Square in");
88 PM("Cambridge, Massachusetts. Your mission is to throw a pie in his face.\n");
90 PM(" First, you'll have to find him. A botched experiment by an MIT");
91 PM("physics group has regularized the floor's topology, so that each");
92 PM("room has exits to three other rooms. (Look at a dodecahedron to");
93 PM("see how this works --- if you don't know what a dodecahedron is,");
94 PM("ask someone.)\n");
97 PM(" Each turn you may move to an adjacent room or throw a pie. If");
98 PM("you run out of pies, you lose. Each pie can pass through up to");
99 PM("five rooms (connected by a continuous path from where you are). You");
100 PM("aim by telling the computer which rooms you want to throw through.");
101 PM("If the path is incorrect (presumes a nonexistent connection) the ");
102 PM("pie moves at random.");
104 PM(" If a pie hits the superhack, you win. If it hits you, you lose!\n");
106 (void) fputs("<Press return to continue>", stdout);
107 (void) fgets(inp, sizeof(inp), stdin);
108 (void) putchar('\n');
111 PM(" Starlets --- two rooms contain lonely, beautiful women. If you");
112 PM("enter these, you will become fascinated and forget your mission as");
113 PM("you engage in futile efforts to pick one up. You weenie.");
114 PM(" Droids --- two rooms are guarded by experimental AI security ");
115 PM("droids. If you enter either, the droid will grab you and hustle");
116 PM("you off to somewhere else, at random.");
117 PM(" Lusers --- two rooms contain hungry lusers. If you blunder into");
118 PM("either, they will eat one of your pies.");
119 PM(" Superhack --- the superhack is not bothered by hazards (the");
120 PM("lusers are in awe of him, he's programmed the droids to ignore him,");
121 PM("and he has no sex life). Usually he is hacking. Two things can");
122 PM("interrupt him; you throwing a pie or you entering his room.\n");
123 PM(" On an interrupt, the superhack moves (3/4 chance) or stays where");
124 PM("he is (1/4 chance). After that, if he is where you are, he flames");
125 PM("you and you lose!\n");
127 (void) fputs("<Press return to continue>", stdout);
128 (void) fgets(inp, sizeof(inp), stdin);
129 (void) putchar('\n');
132 PM(" When you are one room away from the superhack or a hazard,");
133 PM("the computer says:");
134 PM(" superhack: \"I smell a superhack!\"");
135 PM(" security droid: \"Droids nearby!\"");
136 PM(" starlet: \"I smell perfume!\"");
137 PM(" luser: \"Lusers nearby!\"");
139 PM("If you take too long finding the superhack, hazards may move. You");
140 PM("will get a warning when this happens.\n");
143 PM(" Available commands are:\n");
144 PM(" ? --- print long instructions.");
145 PM(" m <number> --- move to room with given number.");
146 PM(" t <numbers> --- throw through given rooms.");
148 PM(" d --- dump hazard locations.");
150 PM("\nThe list of room numbers after t must be space-separated. Anything");
151 PM("other than one of these commands displays a short help message.");
154 void move_hazard(where)
161 for (j = 0; j < LOCS; j++)
162 if (loc[j] == newloc)
170 /* basic status report */
171 (void) printf("You are in room %d. Exits lead to %d, %d, %d. You have %d pies left.\n",
178 /* maybe it's migration time */
184 PM("Swish, swish, swish --- starlets are moving!");
185 move_hazard(STARLET1);
186 move_hazard(STARLET2);
191 PM("Clank, clank, clank --- droids are moving!");
198 PM("Grumble, grumble, grumble --- lusers are moving!");
204 /* display hazard warnings */
205 for (k = 0; k < 3; k++)
207 int room = cave[loc[YOU]][k];
209 if (room == loc[RMS])
210 (void) puts("I smell a superhack!");
211 else if (room == loc[STARLET1] || room == loc[STARLET2])
212 (void) puts("I smell perfume!");
213 else if (room == loc[DROID1] || room == loc[DROID2])
214 (void) puts("Droids nearby!");
215 else if (room == loc[LUSER1] || room == loc[LUSER2])
216 (void) puts("Lusers nearby!");
222 extern void check_shot(), move_superhack();
225 j9 = sscanf(inp + isalpha(inp[0]), "%d %d %d %d %d",
226 &path[0], &path[1], &path[2], &path[3], &path[4]);
229 PM("Sorry, I didn't see any room numbers after your throw command.");
233 for (k = 0; k < j9; k++)
234 if (k >= 2 && path[k] == path[k - 2])
236 (void) puts("Pies can't fly that crookedly --- try again.");
240 scratchloc = loc[YOU];
242 for (k = 0; k < j9; k++)
246 for (k1 = 0; k1 < 3; k1++)
248 if (cave[scratchloc][k1] == path[k])
250 scratchloc = path[k];
258 scratchloc = cave[scratchloc][FNB()];
267 (void) puts("You missed.");
269 scratchloc = loc[YOU];
281 if (scratchloc == loc[RMS])
283 (void) puts("Splat! You got the superhack! You win.");
287 else if (scratchloc == loc[YOU])
289 (void) puts("Ugh! The pie hit you! You lose.");
294 void move_superhack()
299 loc[RMS] = cave[loc[RMS]][k];
301 if (loc[RMS] != loc[YOU])
304 (void) puts("The superhack flames you to a crisp. You lose!");
312 if (sscanf(inp + isalpha(inp[0]), "%d", &scratchloc) < 1)
314 PM("Sorry, I didn't see a room number after your `m' command.");
320 for (k = 0; k < 3; k++)
321 if (cave[loc[YOU]][k] == scratchloc)
324 PM("You can't get there from here!");
328 loc[YOU] = scratchloc;
330 if (scratchloc == loc[RMS])
332 PM("Yow! You interrupted the superhack.");
335 else if (scratchloc == loc[STARLET1] || scratchloc == loc[STARLET2])
337 PM("You begin to babble at an unimpressed starlet. You lose!");
340 else if (scratchloc == loc[DROID1] || scratchloc == loc[DROID2])
342 PM("Zap --- security droid snatch. Elsewheresville for you!");
343 scratchloc = loc[YOU] = FNA();
346 else if (scratchloc == loc[LUSER1] || scratchloc == loc[LUSER2])
348 PM("Munch --- lusers ate one of your pies!");
357 if (argc >= 2 && strcmp(argv[1], "-s") == 0)
358 srand(atoi(argv[2]));
360 srand((int)time((long *) 0));
365 for (j = 0; j < LOCS; j++)
368 for (j = 0; j < LOCS; j++)
369 for (k = 0; k < LOCS; k++)
372 else if (loc[j] == loc[k])
375 (void) puts("Hunt the Superhack");
378 scratchloc = loc[YOU];
381 while (finished == NOT)
387 c = getlet("Throw, move or help [t,m,?]");
394 print_instructions();
398 (void) printf("RMS is at %d, starlets at %d/%d, droids %d/%d, lusers %d/%d\n",
400 loc[STARLET1] + 1, loc[STARLET2] + 1,
401 loc[DROID1] + 1, loc[DROID2] + 1,
402 loc[LUSER1] + 1, loc[LUSER2] + 1);
407 PM("Available commands are:\n");
408 PM(" ? --- print long instructions.");
409 PM(" m <number> --- move to room with given number.");
410 PM(" t <numbers> --- throw through given rooms.");
412 PM(" d --- dump hazard locations.");
414 PM("The list of room numbers after t must be space-separated");
417 (void) putchar('\n');
420 if (getlet("Play again") != 'y')
422 PM("Happy hacking!");
428 /* superhack.c ends here */