X-Git-Url: https://jxself.org/git/?p=wumpus.git;a=blobdiff_plain;f=superhack.c;h=2a245bdaac5814a19953f16a1906403021737b11;hp=f9879fd8ee10b7afc6af792d4ae53a573b3a3bb1;hb=696447095f4d7fd1752336c4b7dc59be694d74bb;hpb=9be728e4b06b5fd96f63f9e1a95404889bb900f9 diff --git a/superhack.c b/superhack.c index f9879fd..2a245bd 100644 --- a/superhack.c +++ b/superhack.c @@ -3,21 +3,28 @@ * * Author: Eric S. Raymond * - * My update of a classic adventure game. The C is crude because it's - * a hack on a line-by-line translation of a BASIC `Hunt The Wumpus'. - * This code is no relation to the elaborate dungeon game called `Hack'. + * My update of a classic adventure game. This code is no relation to + * the elaborate dungeon game called `Hack'. * * Any resemblance to persons living or dead is strictly coincidental. And * if you believe *that*... + * + * SPDX-License-Identifier: BSD-2-Clause */ #include #include +#include +#include +#include +#include +#include static int path[5]; static int j, k, scratchloc, pies; static char inp[BUFSIZ]; /* common input buffer */ +#define NUMBERS "0123456789" #define YOU 0 #define RMS 1 #define STARLET1 2 @@ -34,6 +41,8 @@ static int loc[LOCS]; #define LOSE -1 static int finished; +#define IGNORE(r) do{if(r);}while(0) + static int cave[20][3] = { {1,4,7}, @@ -68,7 +77,7 @@ int getlet(prompt) char *prompt; { (void) printf("%s? ", prompt); - if(fgets(inp, sizeof(inp), stdin)) + if (fgets(inp, sizeof(inp), stdin)) return(tolower(inp[0])); else { fputs("\n",stdout); @@ -80,8 +89,6 @@ char *prompt; void print_instructions() { - char ebuf[BUFSIZ]; - PM("Welcome to `Hunt the Superhack'\n") PM(" The superhack lives on the 9th floor of 45 Technology Square in"); @@ -104,7 +111,7 @@ PM("Cambridge, Massachusetts. Your mission is to throw a pie in his face.\n"); PM(" If a pie hits the superhack, you win. If it hits you, you lose!\n"); (void) fputs("", stdout); - (void) fgets(inp, sizeof(inp), stdin); + IGNORE(fgets(inp, sizeof(inp), stdin)); (void) putchar('\n'); PM("Hazards:"); @@ -222,8 +229,9 @@ void throw() extern void check_shot(), move_superhack(); int j9; - j9 = sscanf(inp + isalpha(inp[0]), "%d %d %d %d %d", + j9 = sscanf(inp + strcspn(inp, NUMBERS), "%d %d %d %d %d", &path[0], &path[1], &path[2], &path[3], &path[4]); + if (j9 < 1) { PM("Sorry, I didn't see any room numbers after your throw command."); @@ -231,7 +239,7 @@ void throw() } for (k = 0; k < j9; k++) - if (path[k] == path[k - 2]) + if (k >= 2 && path[k] == path[k - 2]) { (void) puts("Pies can't fly that crookedly --- try again."); return; @@ -261,7 +269,6 @@ void throw() } - ammo: if (finished == NOT) { (void) puts("You missed."); @@ -309,7 +316,7 @@ void move_superhack() void move() { - if (sscanf(inp + isalpha(inp[0]), "%d", &scratchloc) < 1) + if (sscanf(inp + strcspn(inp, NUMBERS), "%d", &scratchloc) < 1) { PM("Sorry, I didn't see a room number after your `m' command."); return; @@ -332,7 +339,7 @@ goodmove: PM("Yow! You interrupted the superhack."); move_superhack(); } - else if (scratchloc == loc[STARLET1] || scratchloc == loc[STARLET1]) + else if (scratchloc == loc[STARLET1] || scratchloc == loc[STARLET2]) { PM("You begin to babble at an unimpressed starlet. You lose!"); finished = LOSE; @@ -350,9 +357,7 @@ goodmove: } } -main(argc, argv) -int argc; -char *argv[]; +int main(int argc, char *argv[]) { if (argc >= 2 && strcmp(argv[1], "-s") == 0) srand(atoi(argv[2])); @@ -423,6 +428,7 @@ char *argv[]; break; } } + return 0; } /* superhack.c ends here */