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*...
19 #include <sys/socket.h>
22 static int j, k, scratchloc, pies;
23 static char inp[BUFSIZ]; /* common input buffer */
25 #define NUMBERS "0123456789"
42 #define IGNORE(r) do{if(r);}while(0)
44 static int cave[20][3] =
68 #define FNA() (rand() % 20)
70 #define FNB() (rand() % 3)
72 #define FNC() (rand() % 4)
77 (void) printf("%s? ", prompt);
78 if (fgets(inp, sizeof(inp), stdin))
79 return(tolower(inp[0]));
86 #define PM(x) puts(x);
88 void print_instructions()
90 PM("Welcome to `Hunt the Superhack'\n")
92 PM(" The superhack lives on the 9th floor of 45 Technology Square in");
93 PM("Cambridge, Massachusetts. Your mission is to throw a pie in his face.\n");
95 PM(" First, you'll have to find him. A botched experiment by an MIT");
96 PM("physics group has regularized the floor's topology, so that each");
97 PM("room has exits to three other rooms. (Look at a dodecahedron to");
98 PM("see how this works --- if you don't know what a dodecahedron is,");
99 PM("ask someone.)\n");
102 PM(" Each turn you may move to an adjacent room or throw a pie. If");
103 PM("you run out of pies, you lose. Each pie can pass through up to");
104 PM("five rooms (connected by a continuous path from where you are). You");
105 PM("aim by telling the computer which rooms you want to throw through.");
106 PM("If the path is incorrect (presumes a nonexistent connection) the ");
107 PM("pie moves at random.");
109 PM(" If a pie hits the superhack, you win. If it hits you, you lose!\n");
111 (void) fputs("<Press return to continue>", stdout);
112 IGNORE(fgets(inp, sizeof(inp), stdin));
113 (void) putchar('\n');
116 PM(" Starlets --- two rooms contain lonely, beautiful women. If you");
117 PM("enter these, you will become fascinated and forget your mission as");
118 PM("you engage in futile efforts to pick one up. You weenie.");
119 PM(" Droids --- two rooms are guarded by experimental AI security ");
120 PM("droids. If you enter either, the droid will grab you and hustle");
121 PM("you off to somewhere else, at random.");
122 PM(" Lusers --- two rooms contain hungry lusers. If you blunder into");
123 PM("either, they will eat one of your pies.");
124 PM(" Superhack --- the superhack is not bothered by hazards (the");
125 PM("lusers are in awe of him, he's programmed the droids to ignore him,");
126 PM("and he has no sex life). Usually he is hacking. Two things can");
127 PM("interrupt him; you throwing a pie or you entering his room.\n");
128 PM(" On an interrupt, the superhack moves (3/4 chance) or stays where");
129 PM("he is (1/4 chance). After that, if he is where you are, he flames");
130 PM("you and you lose!\n");
132 (void) fputs("<Press return to continue>", stdout);
133 (void) fgets(inp, sizeof(inp), stdin);
134 (void) putchar('\n');
137 PM(" When you are one room away from the superhack or a hazard,");
138 PM("the computer says:");
139 PM(" superhack: \"I smell a superhack!\"");
140 PM(" security droid: \"Droids nearby!\"");
141 PM(" starlet: \"I smell perfume!\"");
142 PM(" luser: \"Lusers nearby!\"");
144 PM("If you take too long finding the superhack, hazards may move. You");
145 PM("will get a warning when this happens.\n");
148 PM(" Available commands are:\n");
149 PM(" ? --- print long instructions.");
150 PM(" m <number> --- move to room with given number.");
151 PM(" t <numbers> --- throw through given rooms.");
153 PM(" d --- dump hazard locations.");
155 PM("\nThe list of room numbers after t must be space-separated. Anything");
156 PM("other than one of these commands displays a short help message.");
159 void move_hazard(where)
166 for (j = 0; j < LOCS; j++)
167 if (loc[j] == newloc)
175 /* basic status report */
176 (void) printf("You are in room %d. Exits lead to %d, %d, %d. You have %d pies left.\n",
183 /* maybe it's migration time */
189 PM("Swish, swish, swish --- starlets are moving!");
190 move_hazard(STARLET1);
191 move_hazard(STARLET2);
196 PM("Clank, clank, clank --- droids are moving!");
203 PM("Grumble, grumble, grumble --- lusers are moving!");
209 /* display hazard warnings */
210 for (k = 0; k < 3; k++)
212 int room = cave[loc[YOU]][k];
214 if (room == loc[RMS])
215 (void) puts("I smell a superhack!");
216 else if (room == loc[STARLET1] || room == loc[STARLET2])
217 (void) puts("I smell perfume!");
218 else if (room == loc[DROID1] || room == loc[DROID2])
219 (void) puts("Droids nearby!");
220 else if (room == loc[LUSER1] || room == loc[LUSER2])
221 (void) puts("Lusers nearby!");
227 extern void check_shot(), move_superhack();
230 j9 = sscanf(inp + strcspn(inp, NUMBERS), "%d %d %d %d %d",
231 &path[0], &path[1], &path[2], &path[3], &path[4]);
235 PM("Sorry, I didn't see any room numbers after your throw command.");
239 for (k = 0; k < j9; k++)
240 if (k >= 2 && path[k] == path[k - 2])
242 (void) puts("Pies can't fly that crookedly --- try again.");
246 scratchloc = loc[YOU];
248 for (k = 0; k < j9; k++)
252 for (k1 = 0; k1 < 3; k1++)
254 if (cave[scratchloc][k1] == path[k])
256 scratchloc = path[k];
264 scratchloc = cave[scratchloc][FNB()];
272 (void) puts("You missed.");
274 scratchloc = loc[YOU];
286 if (scratchloc == loc[RMS])
288 (void) puts("Splat! You got the superhack! You win.");
292 else if (scratchloc == loc[YOU])
294 (void) puts("Ugh! The pie hit you! You lose.");
299 void move_superhack()
304 loc[RMS] = cave[loc[RMS]][k];
306 if (loc[RMS] != loc[YOU])
309 (void) puts("The superhack flames you to a crisp. You lose!");
317 if (sscanf(inp + strcspn(inp, NUMBERS), "%d", &scratchloc) < 1)
319 PM("Sorry, I didn't see a room number after your `m' command.");
325 for (k = 0; k < 3; k++)
326 if (cave[loc[YOU]][k] == scratchloc)
329 PM("You can't get there from here!");
333 loc[YOU] = scratchloc;
335 if (scratchloc == loc[RMS])
337 PM("Yow! You interrupted the superhack.");
340 else if (scratchloc == loc[STARLET1] || scratchloc == loc[STARLET2])
342 PM("You begin to babble at an unimpressed starlet. You lose!");
345 else if (scratchloc == loc[DROID1] || scratchloc == loc[DROID2])
347 PM("Zap --- security droid snatch. Elsewheresville for you!");
348 scratchloc = loc[YOU] = FNA();
351 else if (scratchloc == loc[LUSER1] || scratchloc == loc[LUSER2])
353 PM("Munch --- lusers ate one of your pies!");
358 int main(int argc, char *argv[])
360 if (argc >= 2 && strcmp(argv[1], "-s") == 0)
361 srand(atoi(argv[2]));
363 srand((int)time((long *) 0));
368 for (j = 0; j < LOCS; j++)
371 for (j = 0; j < LOCS; j++)
372 for (k = 0; k < LOCS; k++)
375 else if (loc[j] == loc[k])
378 (void) puts("Hunt the Superhack");
381 scratchloc = loc[YOU];
384 while (finished == NOT)
390 c = getlet("Throw, move or help [t,m,?]");
397 print_instructions();
401 (void) printf("RMS is at %d, starlets at %d/%d, droids %d/%d, lusers %d/%d\n",
403 loc[STARLET1] + 1, loc[STARLET2] + 1,
404 loc[DROID1] + 1, loc[DROID2] + 1,
405 loc[LUSER1] + 1, loc[LUSER2] + 1);
410 PM("Available commands are:\n");
411 PM(" ? --- print long instructions.");
412 PM(" m <number> --- move to room with given number.");
413 PM(" t <numbers> --- throw through given rooms.");
415 PM(" d --- dump hazard locations.");
417 PM("The list of room numbers after t must be space-separated");
420 (void) putchar('\n');
423 if (getlet("Play again") != 'y')
425 PM("Happy hacking!");
432 /* superhack.c ends here */