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 #include <sys/socket.h>
21 static int j, k, scratchloc, pies;
22 static char inp[BUFSIZ]; /* common input buffer */
40 static int cave[20][3] =
64 #define FNA() (rand() % 20)
66 #define FNB() (rand() % 3)
68 #define FNC() (rand() % 4)
73 (void) printf("%s? ", prompt);
74 if(fgets(inp, sizeof(inp), stdin))
75 return(tolower(inp[0]));
82 #define PM(x) puts(x);
84 void print_instructions()
86 PM("Welcome to `Hunt the Superhack'\n")
88 PM(" The superhack lives on the 9th floor of 45 Technology Square in");
89 PM("Cambridge, Massachusetts. Your mission is to throw a pie in his face.\n");
91 PM(" First, you'll have to find him. A botched experiment by an MIT");
92 PM("physics group has regularized the floor's topology, so that each");
93 PM("room has exits to three other rooms. (Look at a dodecahedron to");
94 PM("see how this works --- if you don't know what a dodecahedron is,");
95 PM("ask someone.)\n");
98 PM(" Each turn you may move to an adjacent room or throw a pie. If");
99 PM("you run out of pies, you lose. Each pie can pass through up to");
100 PM("five rooms (connected by a continuous path from where you are). You");
101 PM("aim by telling the computer which rooms you want to throw through.");
102 PM("If the path is incorrect (presumes a nonexistent connection) the ");
103 PM("pie moves at random.");
105 PM(" If a pie hits the superhack, you win. If it hits you, you lose!\n");
107 (void) fputs("<Press return to continue>", stdout);
108 (void) fgets(inp, sizeof(inp), stdin);
109 (void) putchar('\n');
112 PM(" Starlets --- two rooms contain lonely, beautiful women. If you");
113 PM("enter these, you will become fascinated and forget your mission as");
114 PM("you engage in futile efforts to pick one up. You weenie.");
115 PM(" Droids --- two rooms are guarded by experimental AI security ");
116 PM("droids. If you enter either, the droid will grab you and hustle");
117 PM("you off to somewhere else, at random.");
118 PM(" Lusers --- two rooms contain hungry lusers. If you blunder into");
119 PM("either, they will eat one of your pies.");
120 PM(" Superhack --- the superhack is not bothered by hazards (the");
121 PM("lusers are in awe of him, he's programmed the droids to ignore him,");
122 PM("and he has no sex life). Usually he is hacking. Two things can");
123 PM("interrupt him; you throwing a pie or you entering his room.\n");
124 PM(" On an interrupt, the superhack moves (3/4 chance) or stays where");
125 PM("he is (1/4 chance). After that, if he is where you are, he flames");
126 PM("you and you lose!\n");
128 (void) fputs("<Press return to continue>", stdout);
129 (void) fgets(inp, sizeof(inp), stdin);
130 (void) putchar('\n');
133 PM(" When you are one room away from the superhack or a hazard,");
134 PM("the computer says:");
135 PM(" superhack: \"I smell a superhack!\"");
136 PM(" security droid: \"Droids nearby!\"");
137 PM(" starlet: \"I smell perfume!\"");
138 PM(" luser: \"Lusers nearby!\"");
140 PM("If you take too long finding the superhack, hazards may move. You");
141 PM("will get a warning when this happens.\n");
144 PM(" Available commands are:\n");
145 PM(" ? --- print long instructions.");
146 PM(" m <number> --- move to room with given number.");
147 PM(" t <numbers> --- throw through given rooms.");
149 PM(" d --- dump hazard locations.");
151 PM("\nThe list of room numbers after t must be space-separated. Anything");
152 PM("other than one of these commands displays a short help message.");
155 void move_hazard(where)
162 for (j = 0; j < LOCS; j++)
163 if (loc[j] == newloc)
171 /* basic status report */
172 (void) printf("You are in room %d. Exits lead to %d, %d, %d. You have %d pies left.\n",
179 /* maybe it's migration time */
185 PM("Swish, swish, swish --- starlets are moving!");
186 move_hazard(STARLET1);
187 move_hazard(STARLET2);
192 PM("Clank, clank, clank --- droids are moving!");
199 PM("Grumble, grumble, grumble --- lusers are moving!");
205 /* display hazard warnings */
206 for (k = 0; k < 3; k++)
208 int room = cave[loc[YOU]][k];
210 if (room == loc[RMS])
211 (void) puts("I smell a superhack!");
212 else if (room == loc[STARLET1] || room == loc[STARLET2])
213 (void) puts("I smell perfume!");
214 else if (room == loc[DROID1] || room == loc[DROID2])
215 (void) puts("Droids nearby!");
216 else if (room == loc[LUSER1] || room == loc[LUSER2])
217 (void) puts("Lusers nearby!");
223 extern void check_shot(), move_superhack();
226 j9 = sscanf(inp + isalpha(inp[0]), "%d %d %d %d %d",
227 &path[0], &path[1], &path[2], &path[3], &path[4]);
230 PM("Sorry, I didn't see any room numbers after your throw command.");
234 for (k = 0; k < j9; k++)
235 if (k >= 2 && path[k] == path[k - 2])
237 (void) puts("Pies can't fly that crookedly --- try again.");
241 scratchloc = loc[YOU];
243 for (k = 0; k < j9; k++)
247 for (k1 = 0; k1 < 3; k1++)
249 if (cave[scratchloc][k1] == path[k])
251 scratchloc = path[k];
259 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!");
353 int main(int argc, char *argv[])
355 if (argc >= 2 && strcmp(argv[1], "-s") == 0)
356 srand(atoi(argv[2]));
358 srand((int)time((long *) 0));
363 for (j = 0; j < LOCS; j++)
366 for (j = 0; j < LOCS; j++)
367 for (k = 0; k < LOCS; k++)
370 else if (loc[j] == loc[k])
373 (void) puts("Hunt the Superhack");
376 scratchloc = loc[YOU];
379 while (finished == NOT)
385 c = getlet("Throw, move or help [t,m,?]");
392 print_instructions();
396 (void) printf("RMS is at %d, starlets at %d/%d, droids %d/%d, lusers %d/%d\n",
398 loc[STARLET1] + 1, loc[STARLET2] + 1,
399 loc[DROID1] + 1, loc[DROID2] + 1,
400 loc[LUSER1] + 1, loc[LUSER2] + 1);
405 PM("Available commands are:\n");
406 PM(" ? --- print long instructions.");
407 PM(" m <number> --- move to room with given number.");
408 PM(" t <numbers> --- throw through given rooms.");
410 PM(" d --- dump hazard locations.");
412 PM("The list of room numbers after t must be space-separated");
415 (void) putchar('\n');
418 if (getlet("Play again") != 'y')
420 PM("Happy hacking!");
427 /* superhack.c ends here */