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*...
12 * SPDX-License-Identifier: BSD-2-Clause
19 #include <sys/socket.h>
24 static int j, k, scratchloc, pies;
25 static char inp[BUFSIZ]; /* common input buffer */
27 #define NUMBERS "0123456789"
50 static int cave[20][3] = {
51 {1, 4, 7}, {0, 2, 9}, {1, 3, 11}, {2, 4, 13}, {0, 3, 5},
52 {4, 6, 14}, {5, 7, 16}, {0, 6, 8}, {7, 9, 17}, {1, 8, 10},
53 {9, 11, 18}, {2, 10, 12}, {11, 13, 19}, {3, 12, 14}, {5, 13, 15},
54 {14, 16, 19}, {6, 15, 17}, {8, 16, 18}, {10, 17, 19}, {12, 15, 18},
57 #define FNA() (rand() % 20)
59 #define FNB() (rand() % 3)
61 #define FNC() (rand() % 4)
66 (void)printf("%s? ", prompt);
67 if (fgets(inp, sizeof(inp), stdin)) {
68 return (tolower(inp[0]));
75 #define PM(x) puts(x);
77 void print_instructions() {
78 PM("Welcome to `Hunt the Superhack'\n")
80 PM(" The superhack lives on the 9th floor of 45 Technology Square "
82 PM("Cambridge, Massachusetts. Your mission is to throw a pie in his "
85 PM(" First, you'll have to find him. A botched experiment by an "
87 PM("physics group has regularized the floor's topology, so that each");
88 PM("room has exits to three other rooms. (Look at a dodecahedron to");
89 PM("see how this works --- if you don't know what a dodecahedron is,");
90 PM("ask someone.)\n");
93 PM(" Each turn you may move to an adjacent room or throw a pie. If");
94 PM("you run out of pies, you lose. Each pie can pass through up to");
95 PM("five rooms (connected by a continuous path from where you are). "
97 PM("aim by telling the computer which rooms you want to throw "
99 PM("If the path is incorrect (presumes a nonexistent connection) the ");
100 PM("pie moves at random.");
102 PM(" If a pie hits the superhack, you win. If it hits you, you "
105 (void)fputs("<Press return to continue>", stdout);
106 IGNORE(fgets(inp, sizeof(inp), stdin));
110 PM(" Starlets --- two rooms contain lonely, beautiful women. If "
112 PM("enter these, you will become fascinated and forget your mission "
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 "
120 PM("either, they will eat one of your pies.");
121 PM(" Superhack --- the superhack is not bothered by hazards (the");
122 PM("lusers are in awe of him, he's programmed the droids to ignore "
124 PM("and he has no sex life). Usually he is hacking. Two things can");
125 PM("interrupt him; you throwing a pie or you entering his room.\n");
126 PM(" On an interrupt, the superhack moves (3/4 chance) or stays "
128 PM("he is (1/4 chance). After that, if he is where you are, he "
130 PM("you and you lose!\n");
132 (void)fputs("<Press return to continue>", stdout);
133 (void)fgets(inp, sizeof(inp), stdin);
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. "
146 PM("will get a warning when this happens.\n");
149 PM(" Available commands are:\n");
150 PM(" ? --- print long instructions.");
151 PM(" m <number> --- move to room with given number.");
152 PM(" t <numbers> --- throw through given rooms.");
154 PM(" d --- dump hazard locations.");
156 PM("\nThe list of room numbers after t must be space-separated. "
158 PM("other than one of these commands displays a short help message.");
161 void move_hazard(where) int where;
167 for (j = 0; j < LOCS; j++) {
168 if (loc[j] == newloc) {
176 void check_hazards() {
177 /* basic status report */
178 (void)printf("You are in room %d. Exits lead to %d, %d, %d. You have "
180 loc[YOU] + 1, cave[loc[YOU]][0] + 1, cave[loc[YOU]][1] + 1,
181 cave[loc[YOU]][2] + 1, pies);
183 /* maybe it's migration time */
188 PM("Swish, swish, swish --- starlets are moving!");
189 move_hazard(STARLET1);
190 move_hazard(STARLET2);
195 PM("Clank, clank, clank --- droids are moving!");
202 PM("Grumble, grumble, grumble --- lusers are moving!");
208 /* display hazard warnings */
209 for (k = 0; k < 3; k++) {
210 int room = cave[loc[YOU]][k];
212 if (room == loc[RMS]) {
213 (void)puts("I smell a superhack!");
214 } else if (room == loc[STARLET1] || room == loc[STARLET2]) {
215 (void)puts("I smell perfume!");
216 } else if (room == loc[DROID1] || room == loc[DROID2]) {
217 (void)puts("Droids nearby!");
218 } else if (room == loc[LUSER1] || room == loc[LUSER2]) {
219 (void)puts("Lusers nearby!");
225 extern void check_shot(), move_superhack();
228 j9 = sscanf(inp + strcspn(inp, NUMBERS), "%d %d %d %d %d", &path[0],
229 &path[1], &path[2], &path[3], &path[4]);
232 PM("Sorry, I didn't see any room numbers after your throw "
237 for (k = 0; k < j9; k++) {
238 if (k >= 2 && path[k] == path[k - 2]) {
240 "Pies can't fly that crookedly --- try again.");
245 scratchloc = loc[YOU];
247 for (k = 0; k < j9; k++) {
250 for (k1 = 0; k1 < 3; k1++) {
251 if (cave[scratchloc][k1] == path[k]) {
252 scratchloc = path[k];
254 if (finished != NOT) {
260 scratchloc = cave[scratchloc][FNB()];
265 if (finished == NOT) {
266 (void)puts("You missed.");
268 scratchloc = loc[YOU];
279 if (scratchloc == loc[RMS]) {
280 (void)puts("Splat! You got the superhack! You win.");
284 else if (scratchloc == loc[YOU]) {
285 (void)puts("Ugh! The pie hit you! You lose.");
290 void move_superhack() {
294 loc[RMS] = cave[loc[RMS]][k];
297 if (loc[RMS] != loc[YOU]) {
301 (void)puts("The superhack flames you to a crisp. You lose!");
307 if (sscanf(inp + strcspn(inp, NUMBERS), "%d", &scratchloc) < 1) {
308 PM("Sorry, I didn't see a room number after your `m' command.");
314 for (k = 0; k < 3; k++) {
315 if (cave[loc[YOU]][k] == scratchloc) {
320 PM("You can't get there from here!");
324 loc[YOU] = scratchloc;
326 if (scratchloc == loc[RMS]) {
327 PM("Yow! You interrupted the superhack.");
329 } else if (scratchloc == loc[STARLET1] || scratchloc == loc[STARLET2]) {
330 PM("You begin to babble at an unimpressed starlet. You lose!");
332 } else if (scratchloc == loc[DROID1] || scratchloc == loc[DROID2]) {
333 PM("Zap --- security droid snatch. Elsewheresville for you!");
334 scratchloc = loc[YOU] = FNA();
336 } else if (scratchloc == loc[LUSER1] || scratchloc == loc[LUSER2]) {
337 PM("Munch --- lusers ate one of your pies!");
342 int main(int argc, char *argv[]) {
343 if (argc >= 2 && strcmp(argv[1], "-s") == 0) {
344 srand(atoi(argv[2]));
346 srand((int)time((long *)0));
351 for (j = 0; j < LOCS; j++) {
355 for (j = 0; j < LOCS; j++) {
356 for (k = 0; k < LOCS; k++) {
359 } else if (loc[j] == loc[k]) {
365 (void)puts("Hunt the Superhack");
368 scratchloc = loc[YOU];
371 while (finished == NOT) {
376 c = getlet("Throw, move or help [t,m,?]");
380 } else if (c == 'm') {
382 } else if (c == '?') {
383 print_instructions();
387 (void)printf("RMS is at %d, starlets at %d/%d, "
388 "droids %d/%d, lusers %d/%d\n",
389 loc[RMS] + 1, loc[STARLET1] + 1,
390 loc[STARLET2] + 1, loc[DROID1] + 1,
391 loc[DROID2] + 1, loc[LUSER1] + 1,
396 PM("Available commands are:\n");
397 PM(" ? --- print long "
399 PM(" m <number> --- move to room with given "
401 PM(" t <numbers> --- throw through given "
404 PM(" d --- dump hazard locations.");
406 PM("The list of room numbers after t must be "
413 if (getlet("Play again") != 'y') {
414 PM("Happy hacking!");
421 /* superhack.c ends here */