40
\$\begingroup\$

When I was a lad, kids would wander into computer stores and play Hunt the Wumpus until the staff kicked us out. It was a simple game, programmable on the home computers of the mid-1970s, machines so rudimentary that instead of chicklet-sized microprocessors, I think some of them probably had real chicklets in there.

Let's evoke that bygone era by reproducing the game on modern hardware.

  1. The player starts in a random room on an icosahedral map (thus there are 20 rooms in total, connected to each other like the faces of an icosahedron, and every room has exactly three exits).

  2. The wumpus starts in a randomly selected different room. The wumpus stinks, and its odor can be detected in any of the three rooms adjacent to its location, though the direction of the odor is impossible for the player to determine. The game reports only "you smell a wumpus."

  3. The player carries a bow and an infinite number of arrows, which he may shoot at any time into the room in front of him. If the wumpus is in that room, it dies and the player wins. If the wumpus was not in that room, it is startled and moves randomly into any of the three rooms connected to its current location.

  4. One, randomly selected room (guaranteed not to be the room in which the player starts) contains a bottomless pit. If the player is in any room adjacent to the pit, he feels a breeze, but gets no clue as to which door the breeze came from. If he walks into the room with the pit, he dies and wumpus wins. The wumpus is unaffected by the pit.

  5. If the player walks into the wumpus's room, or if the wumpus walks into the player's room, the wumpus wins.

  6. The player specifies the direction he is facing with a number (1 = right, 2 = left, 3 = back), and then an action (4 = shoot an arrow, 5 = walk in the specified direction).

  7. For the sake of scoring, each game string ("You feel a breeze," "You smell a wumpus," "Your arrow didn't hit anything", etc.) can be considered one byte. No abusing this to hide game code in text; this is just for interacting with the player.

  8. Deduct 10% of your byte count for implementing megabats, which start in a random room different from the player (though they can share a room with the wumpus and/or the pit). If the player walks into the room with the bats, the bats will carry the player to another randomly selected room (guaranteed not to be the room with the pit or the wumpus in it), before flying off to their own, new random location. In the three rooms adjacent to the bats, they can be heard squeaking, but the player is given no information about which room the sound comes from.

  9. Deduct 35% of your byte count for implementing a graphical interface that shows the icosahedral map and some kind of indication of the information the player has so far about the location of the pit, the wumpus, and the bats (if applicable), relative to the player. Obviously, if the wumpus moves or the player gets moved by the bats, the map needs to reset accordingly.

  10. Lowest byte count, as adjusted, wins.

BASIC source code for a version of the game (not necessarily conforming to the rules above and, in any case, utterly ungolfed) can be found at this website and probably others.

\$\endgroup\$
18
  • \$\begingroup\$ Some clarifications: 3. if the wumpus was not in that room it is startled and moves to one of the THREE rooms.. so if you fire an arrow and miss, the wumpus may come and kill you, right? And the wumpus will only move if startled, otherwise it just stays put? 6. I understand the player's heading is determined by the room he came from. So if he came from the south his options would be 1.northeast 2.northwest 3.south and if he came from the north it would be the opposite. Also your rules seem simpler/golfier than the reference program (which I haven't investigated in detail yet.) Am I correct? \$\endgroup\$ Apr 21, 2014 at 22:38
  • \$\begingroup\$ Argh! I can't find any pictures of the dual graph of an icosahedron anywhere on the net. \$\endgroup\$
    – Jack M
    Apr 21, 2014 at 23:35
  • 1
    \$\begingroup\$ @steveverrill Yes, if you spook it, it may come and kill you. If you don't spook it, it doesn't move. There are many variations on the game; many versions allow arrows to circle around and kill you, for example. I've pared that out. \$\endgroup\$ Apr 21, 2014 at 23:47
  • 3
    \$\begingroup\$ @JackM the map of the faces of an icosahedron is identical to the map of vertices of a dodecahedron, and that graph is easily found. Try for example wolframalpha.com/input/?i=DodecahedralGraph+edgerules or the equivalent Mathematica command GraphData["DodecahedralGraph", "EdgeRules"]. Either way you get {1 -> 14, 1 -> 15, 1 -> 16, 2 -> 5, 2 -> 6, 2 -> 13, 3 -> 7, 3 -> 14, 3 -> 19, 4 -> 8, 4 -> 15, 4 -> 20, 5 -> 11, 5 -> 19, 6 -> 12, 6 -> 20, 7 -> 11, 7 -> 16, 8 -> 12, 8 -> 16, 9 -> 10, 9 -> 14, 9 -> 17, 10 -> 15, 10 -> 18, 11 -> 12, 13 -> 17, 13 -> 18, 17 -> 19, 18 -> 20} \$\endgroup\$ Apr 22, 2014 at 0:35
  • 2
    \$\begingroup\$ @JackM No, "back" implies turning around and walking back the way you came. If you hit "back" twice, you end up where you started. No need to store earlier game states. \$\endgroup\$ Apr 22, 2014 at 2:16

8 Answers 8

22
+250
\$\begingroup\$

GolfScript, 163

:n;:`"You shot the wumpus.
""The wumpus ate you.
""The pit swallowed you.
"{19:|rand}2*0|{[:,~,4%"ftvh"=.,+,@-]{20%}%}:^{;.^.+.3$?>"You feel a breeze.
"1$6"You smell a wumpus.
"4$8{$?-1>*p}2*'"#{'|):|';`head -1`}"'++~{3%}/={=3$=|{"Your shot missed.
"p@^3rand=@@}if}{=@;}if.[|4$6$]?.)!}do])=

The score is obtained by taking the byte count (290), adding the number of strings used for interaction with the user (6) and subtracting the combined length of those strings (133). The linefeeds are part of the strings and contribute to the byte count.

Milestones

  1. Ported professorfish's answer from Bash to GolfScript. Score: 269

  2. Acted on Peter Taylor's suggestions in the comments. Score: 250

  3. Peter Taylor refactored my entire code and helped me to compress the lookup table. Score: 202

  4. Replaced the lookup table of adjacent rooms with a mathematical approach. Score: 182

  5. Refactored input, output and the function supporting the mathematical approach. Score: 163

A big “Thank you!” goes to Peter Taylor for all his help.

How it works

The 20 rooms are represented as the vertexes of a dodecahedron, which have been assigned numbers from 0 to 19 in the following fashion:

Dodecahedral graph

To find the rooms which are adjacent to room N and order them in clockwise fashion, we have to consider four cases:

  • If N ≡ 0 mod 4 (blue vertexes), the adjacent room are 19 - N, N + 2 mod 20 and N - 2 mod 20.

  • If N ≡ 1 mod 4 (green vertexes), the adjacent room are 19 - N, N - 4 mod 20 and N + 4 mod 20.

  • If N ≡ 2 mod 4 (yellow vertexes), the adjacent room are 19 - N, N - 2 mod 20 and N + 2 mod 20.

  • If N ≡ 3 mod 4 (red vertexes), the adjacent room are, 19 - N, N + 4 mod 20 and N - 4 mod 20.

# The function “p” is implemented as “{`print n print}”. By storing an empty string in 
# “n” and nullifying “`”, “p” becomes an alias for “print”.

:n;:`

# Push the messages corresponding to the three possible outcomes of the game.

"You shot the wumpus.\n""The wumpus ate you.\n""The pit swallowed you.\n"

# Place the wumpus and the pit in randomly selected rooms different from room 19; place 
# the player in room 19, with his back to room 0.

{19:|rand}2*0|

# Function “^” takes a single number as its argument and returns an array of all the
# adjacent rooms to the room that number corresponds to.

{

  [

    :,~       # Store the room number in “,” and negate it ( ~N ≡ 19 - N mod 20 )

    ,4%       # Push the room number modulus 4.

    "ftvh"=   # If it is equal to 0|1|2|3, push 102|116|118|104 ≡ 2|-4|-2|4 mod 20.

    .,+,@-    # Determine the room number plus and minus the integer from above.

  ]{20%}%     # Take all three room numbers modulus 20.

 }:^

{             # STACK: Strings Pit Wumpus Previous Current Function|Index

  ;           # STACK: Strings Pit Wumpus Previous Current

  # Find the adjacent rooms to the current room, duplicate them and remove the rooms 
  # before the first occurrence of the previous room. Since the rooms are ordered in
  # clockwise fashion, the array of adjacent rooms will begin with the rooms 
  # corresponding to the following directions: “Back Left Right”

  .^.+.3$?>   # STACK: Strings Pit Wumpus Previous Current Adjacent

  # Push two more messages and their respective triggers.

  "You feel a breeze.\n"1$6"You smell a wumpus.\n"4$8

  # STACK: ... Pit Wumpus Previous Current Adjacent String Adjacent 6 String Adjacent 8

  # Do the following twice: Duplicate the nth stack element and check if it's present in 
  # the array of adjacent rooms. If so, print the string below it.

  {$?-1>*p}2*

  # Read one line (direction, action, LF) from STDIN. The counter “|” is needed so the 
  # result won't get cached.

  '"#{'|):|';`head -1`}"'++~

  {3%}/       # Replace 1|2|3|4|5|LF with their character codes modulus 3 (1|2|0|1|2|1).

  ={          # If the player shoots an arrow:

    =3$=      # Determine the specified room and check if it corresponds to the wumpus.

      |       # If it does, push and invalid room number ( | > 19 ).

      # If it does not, say so and move the wumpus to a randomly selected adjacent room.

      {"Your shot missed."p@^3rand=@@}

    if

  }{           # If the player moves:

    =@;        # Place him into the selected room.

  }if

  # STACK: Pit Wumpus Previous Current Invalid?

  # Determine if the player's current room number is either invalid, the wumpus's room
  # number or the pit's room number (first match).

  .[|4$6$]?

  # If there is no match, the index is -1 and incrementing and negating it yields “true”.

  # STACK: Strings Pit Wumpus Precious Current Invalid? Index Boolean

# Repeat loop is the boolean is falsy. If repeated, the first instruction of the loop 
# will pop the index.

}do      

# Consolidate the entire stack into an array. And pop its last element: the index.
# Replace the array with the element corresponding to that index.

])=

# GolfScript will execute “print n print”.
\$\endgroup\$
14
  • 1
    \$\begingroup\$ You can save 1 in Q with 19rand 97+; 2 in @ with 97%3*&>..., a further 1 by inlining Q as {19rand 97+}2*:,\:H, a few by replacing | with *, which is often the best way to do an if. B serves no purpose, and I think a few more variables could be eliminated by using the stack. \$\endgroup\$ Apr 27, 2014 at 7:20
  • 1
    \$\begingroup\$ Forgot to mention another frequent trick: base conversion for lookup tables. You can replace the 62 chars for the adjacency list with a 33-char string followed by 256base 20base (and probably also eliminate a few +/- 97). The only downside is that it will require non-printable characters. \$\endgroup\$ Apr 27, 2014 at 16:20
  • 1
    \$\begingroup\$ I've saved a further 13 by refactoring to be more idiomatic GS (mainly using the stack rather than variables); and there's an additional 10 at the cost of making the output less pretty. That's apart from the lookup table compression mentioned in my previous comment. \$\endgroup\$ Apr 27, 2014 at 23:15
  • 1
    \$\begingroup\$ Not at all, I've enjoyed it. I'm just disappointed that the lookup table approach is so much better than the more mathematical one I was intending to use. BTW I think that your current version has a small bug, because if you fire an arrow, miss, and startle the wumpus into your room then it only outputs You were killed by the wumpus with no mention of the arrow missing. That's why I was appending in the non-pretty version. \$\endgroup\$ Apr 28, 2014 at 9:06
  • 1
    \$\begingroup\$ 2*2+ => )2* \$\endgroup\$ May 1, 2014 at 5:26
15
\$\begingroup\$

REV0 C++ (Visual Studio on Windows) 405

#include"stdafx.h"
#include<stdlib.h>
#include<time.h>
int main(){srand(time(NULL));char i,h=rand()%19,w=rand()%19,p=19,d=0,q,e,m[]="e@LwQMQOSOLT";while(p-h&&p-w){for(i=3;i--;){q=(p+m[p%4*3+i])%20;if(q==w)puts("you smell a wumpus");if(q==h)puts("you feel a breeze");}scanf_s("%d",&i);e=(d+i/10)*m[p%4]%3;q=(p+m[p%4*3+e])%20;if(i%5){if(q==w){puts("YOU KILLED THE WUMPUS!");h=p;}else{puts("arrow missed");w=(w+m[w%4*3+rand()%3])%20;}}else{p=q;d=e;if(p==h)puts("YOU FELL IN A HOLE!");}if(p==w)puts("THE WUMPUS GOT YOU!");}}

Below is a playthrough, demonstrating that (provided you don't start right next to a hazard) with correct play you can always win. The player feels a breeze, turns back and does a complete counterclockwise loop. As it takes him exactly 5 moves to feel a breeze again, he knows the hole to his right, and gets as far away as possible. Similarly, when he smells the wumpus, not knowing whether it is right or left, he turns back and does a clockwise loop. It takes him 5 moves to smell the wumpus again, so he knows it is to the left and shoots with certainty.

If he had looped the other way he would have found the wumpus sooner and known it was in the same direction he was turning.

enter image description here

REV1 C (GCC on Cygwin), 431-35% bonus = 280.15

#define u(t,s,c) if(t){puts(s);c;}
i,d,e,a,b;main(){srand(time(0));char q,p=19,h=rand()%p,w=rand()%p,*m="e@LwQMQOSOLT-\\/\n \v ";  
while(p-h&&p-w){
  for(i=3;i--;){q=(p+m[p%4*3+i])%20;u(q==w,"you smell a wumpus",a|=2<<p)u(q==h,"you feel a breeze",b|=1<<p)}
  for(i=20;i--;)printf("%c%c",i==p?m[d+12]:48+(a>>i&2)+(b>>i&1),m[i%4+15]);
  scanf("%d",&i);e=(d+i/10)*m[p%4]%3;q=(p+m[p%4*3+e])%20;
  if(i%5){u(q-w,"arrow missed",w=(w+m[w%4*3+rand()%3])%20;a=0)else u(1,"YOU KILLED THE WUMPUS!",h=p)}
  else{p=q;d=e;u(p==h,"YOU FELL IN A HOLE!",)}
  u(p==w,"THE WUMPUS GOT YOU!",)}}

Newlines added for clarity. The changes from Rev 0 are as follows:

A big thankyou to @Dennis for recommending GCC compiler on Cygwin Linux emulator for Windows. This compiler does not require the includes in the rev 0 program, and it allows default int type for variables and main. This is a life-changing golfing tip!

Additionally running in Linux means that \f does cause the cursor to move down without doing a carriage return (unlike in Windows where it just produces a printable symbol.) This has allowed considerable shortening of the printf statement that prints the board

Several additional tips from Dennis in the comments, and one of my own: change of condition when checking if arrow hit the wumpus: if(q==w)>if(q-w) (..else.. is reversed)

Addition of graphic display showing the information the player knows about where a wumpus is smelt / a breeze is felt to claim the 35% bonus. (I deleted the old debug version of this which showed the exact position of the wumpus and hole. It can be seen in the edit history.)

REV2 C (GCC on Cygwin), 389-35% bonus = 252.85

#define Q(N) (N+"QTLOQMQOSOLT"[N%4*3+e])%20
#define P printf(
i,d,e,a,b;main(){int p=19,q=srand(&p),h=rand()%p,w=rand()%p;
while(p-h&&p-w){
  for(e=3;e--;){q=Q(p);q-w||P"You smell a wumpus\n",a|=2<<p);q-h||P"You feel a breeze\n",b|=1<<p);}
  for(i=20;i--;)P"%c%c",i-p?48+(a>>i&2)+(b>>i&1):"-\\/"[d],"\n \v "[i%4]);
  scanf("%d",&i);e=(d+i/9)*"edde"[p%4]%3;q=Q(p);
  if(i%5){e=rand()%3;w=q-w?P"Your arrow didn't hit anything\n",a=0)&Q(w):(p=20);}
  else p=q,d=e;
}
P p-20?p-w?"YOU FELL IN A HOLE!\n":"THE WUMPUS GOT YOU!\n":"YOU KILLED THE WUMPUS!\n");}

Thanks again to Dennis for refactoring my code:

Char constant m[] replaced with literals (I didn't know you could index a literal.)

Seeding of random numbers with stack variable (system dependent, some systems randomise memory allocation as a security measure.)

Macro with puts replaced with a macro with printf and additional code which must be executed when the message displayed placed inside printf arguments (advantage taken of the face that printf does not print the last few arguments if there aren't enough format specfiers in the format string.) if replaced by ||

Calculation of new position of player/wumpus placed inside new macro.

Win/lose messages placed outside while loop. if replaced by conditional operator.

Use of conditional operator in line for shooting arrow. If the player misses, this requires both printing a message and adjusting wumpus position. Dennis offered a couple of ways of combining printf and the calculation of wumpus position into a single expression, but I have gone with one of my own. printf returns the number of characters printed, which for Your arrow didn't hit anything\n is 31 (11111 binary.) So, 31&Q(w)==Q(w).

My other contribution to this edit has been elimination of some unnecessary brackets.

Output

Here the player has already found where the Wumpus is, but chooses to do a thorough explore to find out exactly where the pit is, too. Unlike my old debug version which showed where the wumpus and pit were throughout the game, this shows only the rooms where the player has visited and felt a breeze (1) smelt the wumpus (2) or both (3). (If the player shoots an arrow and misses, the variable a containing the wumpus position info is reset.)

enter image description here

ICOSAHEDRON REPRESENTATION

Note: this section is based on rev 1

My star feature! There is no graph in my code. To explain how it works, see the world map below. Any point on the icosahedron can be represented by a latitude 0-3 and a longitude 0-4 (or a single number, long*4+lat.) The longitude line marked on the map passes only through those faces with longitude zero, and the latitude line passes through the centre of the faces with latitude zero.

The player can be oriented on 3 possible axes, represented by the symbols as follows: north-south- northeast-southwest\ northwest-southeast /. In any given room he has exactly one exit on each of these axes available to him. In the display shown the player makes a complete clockwise loop. It is generally easy to identify from the player marking where he came from, and therefore where he is allowed to go to.

The one case that is a little difficult for the uninitiated eye is the fourth one. When you see a slant in one of these polar rows, the player has come from the polar cell nearest the outside end of the slant and is facing generally towards the equator. Thus the player is facing southeast and his options are: 15(SOUTH, the cell to the right) 25(northEAST, the cell above) or 35(northWEST, the cell below.)

So, basically I map the icosahedron to a 5x4 grid, with cells numbered 19 to 0 in the order they are printed. The move is made by adding or subtracting from the current position, depending on the player's latitude and direction, per the table below.

If the player goes off the bottom (west) of the board, he comes back on the top (east) side and vice versa, so his position is taken modulo 20. Generally the moves are coded into m[] by adding ascii 80 (P) to the raw value giving the characters shown below, but principle any multiple of 20 can be added without affecting the operation.

Table of addition values for moves

Direction Symbol Latitude 0  1  2  3     Latitude 0 1 2 3

0, N-S      -             1 -1  1 -1              Q O Q O  
1, NE-SW    \            -4  1 -1  4              L Q O T
2, NW-SE    /             4 -3  3 -4              T M S L

The player's input (divided by 10 to remove the second digit) is added to his current direction and taken modulo 3 to get his new direction. This works fine in the majority of cases. However there is an issue when he is in a polar room and moves toward the pole. It will be clear when folding the map below that if he leaves the room facing "northeast" he will enter the new square facing "southeast" so a correction must be made. This is done in the line e=(d+i/10)*m[p%4]%3; by the multiplication by m[p%4]. The first four values of m[] are selected such that, in addition to their function above, they also have the characteristic m[1]%3==m[2]%3==1 and m[0]%3==m[3]%3==2. This leaves the direction alone for the equatorial rooms and applies the necessary correction for polar rooms.

The logical time to do the correction would be after the move. However to save characters it is done before the move. Therefore certain values in m[] must be transposed. So the last 2 characters are LT instead of TL per the table above for example.

enter image description here

UNGOLFED CODE

this is rev 1 code, which is less obfuscated than rev 2.

This will run on GCC / Linux. I have included in the comments the extra code needed to make it run on Visual studio / Windows. It's a big difference!

//Runs on gcc/linux. For visual studio / windows, change printf(...) 
//to printf(" %c%c%c",9*(i%4==1),i==p?m[d+12]:48+(a>>i&2)+(b>>i&1),10*!(i%2)) and uncomment the following lines
//#include"stdafx.h"
//#include<stdlib.h>
//#include<time.h>
//#pragma warning(once:996;once:430) //allow the use of scanf instead of scanf_s, allow default type=int. 
//Though rather than using the pragma, it is shorter to follow compiler recommendation and use scanf_s and int.

#define u(t,s,c) if(t){puts(s);c;}  //if(test){puts(string);additional code;}

i,     //player input, loop counter
d,e,   //current and proposed direction
a,b;   //bit flags for where wumpus smelt / breeze felt
 
main(){
    srand(time(0));
    char q,p=19,h=rand()%p,w=rand()%p,  //Initialise player, hole and wumpus. q stores proposed player position.
    *m="e@LwQMQOSOLT-\\/\n \f ";        //Chars 0-11: movetable. Chars 12-14:symbol for player. Chars 15-18: graphics format.   

    while(p-h&&p-w){

        // Print warnings
        for(i=3;i--;){q=(p+m[p%4*3+i])%20;u(q==w,"you smell a wumpus",a|=2<<p)u(q==h,"you feel a breeze",b|=1<<p)}
            
        // graphic display 
        for(i=20;i--;)printf("%c%c",i==p?m[d+12]:48+(a>>i&2)+(b>>i&1),m[i%4+15]);
        
        // Get player input and work out direction and room 
        scanf("%d",&i);
        e=(d+i/10)*m[p%4]%3;
        q=(p+m[p%4*3+e])%20;

        // i%5 is false if player inputs 5 (move player) otherwise true (shoot arrow) 
        if(i%5)
        {u(q-w,"arrow missed",w=(w+m[w%4*3+rand()%3])%20;a=0)else u(1,"YOU KILLED THE WUMPUS!",h=p)}
        else{p=q;d=e;u(p==h,"YOU FELL IN A HOLE!",)}
        u(p==w,"THE WUMPUS GOT YOU!",)
    }

}

ISSUES AND CURIOISITIES

I have taken advantage of the point mentioned by @professorfish, if the wumpus and pit start in random places, there is no need for the player to start in a random place. The player always starts in room 19 facing north.

I understand that as the wumpus is "unaffected by the pit" the wumpus can start in, or enter the room where the pit is. In general this simplifies things except for one point. I have no specific variable to indicate the game is over; it is over when the player coincides with the wumpus or pit. So when the player wins I display the winning message but move the pit to the player to kludge out of the loop! I can't put the player in the pit as the wumpus might be there and I would get a message about the wumpus that I don't want.

The rev0program worked perfectly in visual studio, but the IDE said "stack corrupted around variable i" on exit. This is because scanf is trying to put an int into a char. Dennis reported incorrect behaviour on his Linux machine because of this. Anyway it is fixed by use of the correct type in rev 1.

The line for displaying the board in rev 0 is clumsy and appears slightly different on other platforms. In printf(" %c%c%c") the middle %c is the printable character displayed. The last %c is either ASCII 0 or ASCII 10 (\n, newline with carriage return in Windows.) There appears to be no character in Windows that works in the console, that will go down a line without giving a carriage return. If there was I wouldn't need the first c% (ASCII 0, or ASCII 9 tab before latitude 1 character. Tabs are notoriously undefined in their behaviour.) The leading space improves formatting (puts latitude 3 & 2 characters nearer latitude 1 character.) Rev 1 has a revison of this line which uses a \f formfeed character and therefore needs no format character at the start of the printf. This makes it shorter, but the \f does not work in Windows.

\$\endgroup\$
16
  • 1
    \$\begingroup\$ I love the writeup. \$\endgroup\$ Apr 27, 2014 at 0:33
  • \$\begingroup\$ I'm not sure if it's because of the modifications I had to make to compile it with GCC on Linux (remove the first include, replace scanf_s with scanf and include stdio.h if I compile as C++ rater than C), but it doesn't quite work for me. For example, if I go left, then back right at the beginning (15 35), I'm in a different room than the one I started in. \$\endgroup\$
    – Dennis
    Apr 29, 2014 at 2:40
  • \$\begingroup\$ @Dennis I've tracked down the source of the error on exit. it's the scanf_s (supposedly safe!) which "corrupts the stack around variable i" when it tries to put what I assume is a 32 bit integer into a char. So the first thing I would suggest is check the type that scanf uses for a "%d" and change the variable i to that type. I get the right answer w/ exit error for char, the right answer w/o exit error for int, and the wrong answer with the Microsoft type __int64 (equivalent long long, unless I put "%lld".) Also have you run the ungolfed version and did you have any problems with the display? \$\endgroup\$ Apr 29, 2014 at 12:17
  • \$\begingroup\$ @steveverrill: Yes, I had tried both versions. The type of i is indeed the problem. The man page says: "d Matches an optionally signed decimal integer; the next pointer must be a pointer to int." Changing the type makes it work just fine. \$\endgroup\$
    – Dennis
    Apr 29, 2014 at 13:35
  • \$\begingroup\$ @steveverrill: I don't know how VS handles things, but if you compile with GCC (as C, not C++), you can save a lot of chars. None of the includes is needed if you replace NULL with 0 and scanf_s with scanf, you don't need int before main and you can move i and d outside of main (they default to int and are initialized to 0). Also, you can define p=19,h=rand()%p,w=rand()%p, replace m[] with *m and it should be possible to define a macro for all instances of if(...==...)puts(...);. \$\endgroup\$
    – Dennis
    Apr 29, 2014 at 19:46
9
\$\begingroup\$

GolfScript, 269 characters

{puts}:|;20,{;9{rand}:r~}$3<(:>"B:%d`w85>2n+Fup`y/>@D-=J7ldnx/W5XsLAb8~"{32-}%"`\24"{base}/3/{[.[~@].[~@]]}%:A=3r=0=:F;~:W;:P;{>A={0=F=}?:^P&!!{"You feel a breeze"|}*^W&!!{"You smell a wumpus"|}*'"#{'9.?r';STDIN.gets()}"'++~);(3%^=\4`={W={"Your arrow hit the wumpus"|0}{"Your arrow didn't hit anything"|W A=0=3r=:W>=.!\{"The wumpus catches you"|}*}if}{>:F;:>W=.!\{"You ran into the wumpus"|}*>P=.!\{"You fell into the pit"|}*&}if}do

Note that 163 was subtracted from the character count for the hard-coded strings. If you want debug output indicating the room numbers add the following line right after the first occurence of ^:

'  YOU 'F'->'>+++puts'  DIRECTIONS [BRL] '^`+puts'  PIT 'P+puts'  WUMPUS 'W+puts 

An example session (with additional debug output):

  YOU 6->11
  DIRECTIONS [BRL] [6 7 16]
  PIT 7
  WUMPUS 5
You feel a breeze
25
  YOU 11->16
  DIRECTIONS [BRL] [11 17 15]
  PIT 7
  WUMPUS 5
35
  YOU 16->11
  DIRECTIONS [BRL] [16 6 7]
  PIT 7
  WUMPUS 5
You feel a breeze
15
  YOU 11->6
  DIRECTIONS [BRL] [11 10 1]
  PIT 7
  WUMPUS 5
15
  YOU 6->10
  DIRECTIONS [BRL] [6 15 5]
  PIT 7
  WUMPUS 5
You smell a wumpus
14
Your arrow didn't hit anything
  YOU 6->10
  DIRECTIONS [BRL] [6 15 5]
  PIT 7
  WUMPUS 0
25
  YOU 10->5
  DIRECTIONS [BRL] [10 14 0]
  PIT 7
  WUMPUS 0
You smell a wumpus
24
Your arrow hit the wumpus
\$\endgroup\$
4
  • \$\begingroup\$ Here is the first working code. Coming back later for some more golfing. \$\endgroup\$
    – Howard
    Apr 22, 2014 at 17:05
  • \$\begingroup\$ My code is currently 1 character longer. I am looking for any possible way to golf further! \$\endgroup\$
    – Timtech
    Apr 23, 2014 at 15:17
  • \$\begingroup\$ Not that you need my help, but you can save 14 chars by defining {puts}:|;, 5 chars by replacing R and W with - and > (allows to eliminate surrounding spaces) and 9 chars by dropping '> 'print (doesn't seem to be required by the question). \$\endgroup\$
    – Dennis
    Apr 26, 2014 at 4:24
  • \$\begingroup\$ @Dennis Thank you. I'll surely implement some of your suggestions. \$\endgroup\$
    – Howard
    Apr 26, 2014 at 7:36
9
\$\begingroup\$

JavaScript (ECMAScript 6) - 2197 1759 -45% = 967.45 Characters

Nearly finished golfing this...

Includes a GUI with an Icosahedral Map and Mega-Bats for the full bonuses.

Wumpus GUI

  • Each room has 4 buttons: X (the Pit); B (the Mega-Bat); W (the Wumpus); and P (You).
  • Your current location is coloured blue.
  • The buttons are coloured red if the object it represents could be in that location and green if it is definitely not in that location.
  • The W and P buttons can be clicked only in the rooms adjacent to your current location.
  • If you win the background turns green and if you die the background turns red.

Code:

P=x=>parseInt(x,36);Q=(y,a=4)=>[P(x)<<a for(x of y)];e=Q("def45c6di7ej1ai1bj2af3bf9dg8eh46b57a1gh0280390678ci9cj24g35h",0);X=Q("o6fl6afnik27bloscfaf");Y=Q("icp8i8t4jej4encjjan6");A='appendChild';C='createElement';W='width';H='height';G='background-color';L='disabled';I='innerHTML';N='className';D=document;R=Math.random;B=D.body;E=[];F=1<0;T=!F;Z="XBWP";s=D[C]('style');s.type='text/css';t='.A{position:absolute;left:25px;top:25px}.D{'+W+':50px;'+H+':50px}.D button{'+W+':25px;'+H+':25px;float:left}.R{'+G+':red}.G{'+G+':green}.B{'+G+':blue}';for(i in X)t+='#D'+i+'{left:'+X[i]+'px;top:'+Y[i]+'px}';s[A](D.createTextNode(t));D.head[A](s);c=D[C]('canvas');c[N]='A';c[W]=c[H]=500;B[A](c);x=c.getContext('2d');x.beginPath();d=(i,b,v)=>{for(j=0;j<3;j++){E[e[3*i+j]][b][L]=v}};a=(i,l,v)=>{t=F;for(j=0;j<3;j++)t=e[3*i+j]==l?T:t;if(t)M[v]++;b=E[i][v];b.c=-1;for(j=0;j<3;j++)E[e[3*i+j]][v].c+=t?1:-1;for(j of E)j[v][N]=j[v].c==M[v]?'R':'G';};M=[0,0,0];S=v=>{M[v]=0;for(i of E){i[v][N]='';i[v].c=0}};for(i in X){for(j=3*i;j<3*i+3;j++)x.moveTo(X[i],Y[i])|x.lineTo(X[e[j]],Y[e[j]]);B[A](v=D[C]('div'));v[N]='A D';v.id='D'+i;E[i]=[];for(j in Z){b=E[i][j]=v[A](D[C]('button'));b[L]=T;b.i=i;b.c=0;b[I]=Z[j];}E[i][4][O='onclick']=function(){d(P,2,T);d(P,3,T);if(this.i==W)c[N]+=' G';else{S(2);W=e[3*W+R()*3|0];if(W==P)c[N]+=' R';else{a(P,W,2);d(P,2,F);d(P,3,F)}}};E[i][3][O]=function(){d(P,2,T);d(P,3,T);E[P][3][N]='';P=this.i;if(W==P||Q==P){c[N]+=' R';return}else if(Z==P){j=P;do{P=R()*20|0}while(P==W||P==Q||P==j);do{Z=R()*20|0}while(Z==j||Z==P);S(1)}d(P,2,F);d(P,3,F);E[P][3][N]='B';a(P,Q,0);a(P,Z,1);a(P,W,2)}}x.stroke();P=R()*20|0;do{W=R()*20|0}while(W==P);do{Q=R()*20|0}while(Q==P);do{Z=R()*20|0}while(Z==P);E[P][3][N]='B';a(P,Q,0);a(P,Z,1);a(P,W,2);d(P,2,F);d(P,3,F)
\$\endgroup\$
2
  • \$\begingroup\$ You get 1066 without ECMA 6 using the closure compiler. \$\endgroup\$
    – A.M.K
    Apr 28, 2014 at 0:41
  • \$\begingroup\$ I was wondering how much easier it would be when you have a graphical representation to help deduct where things is. 1+ but it's slightly too easy :) \$\endgroup\$
    – Sylwester
    May 1, 2014 at 12:29
9
\$\begingroup\$

Bash, 365 (first working version 726!)

CATCHING UP WITH GOLFSCRIPT?

@Dennis has basically done all the golfing for me. Thanks!

The program assumes valid input. Valid input is the direction you choose (1 for right, 2 for left, 3 for back) followed by your action (4 to shoot, 5 to walk).

Some Explanation

I normally do big verbose explanations, but this is probably a bit too complicated for me to be bothered.

Each vertex on the dodecahedron graph is encoded as a letter (a=1, b=2, ... t=20).

The player's starting position is always 20 (and they are standing with their back to 18), since that doesn't matter in itself, only the relative positions of the player, pit and wumpus matter.

The variable $p stores the player's location. $r stores the player's previous location. $w is the wumpus and $h (H for hole) is the pit.

Code

p=t
r=r
j=echo
Z=npoemfsgnohtksblbtpckdpljqnriogelfhkbqrcaiadjhagimsmjtqecrdf
q(){ $j ${Z:RANDOM%19*3:1};}
C(){ [[ ${!1} =~ ${!2} ]];}
d(){ s=${Z:30#$1*3-30:3};}
w=`q`
h=`q`
for((;;));{
b=$p
d $p
u=u${s#*$r}$s
C w p&&$j The wumpus ate you&&exit
C h p&&$j You fell in the pit&&exit
C u w&&$j You smell the wumpus
C u h&&$j You feel a breeze from a pit
read i
F=5
y=${u:i/10:1};C i F&&p=$y&&r=$b||{ d $w;C y w&&$j You killed the wumpus&&exit;$j You missed;w=${s:RANDOM%3:1};};}

Version History

  1. Initial release, 698 chars
  2. Fixed bug where "You feel a breeze" and "You smell the wumpus" can't display at the same time; saved 39 chars by making the random number generation a function.
  3. Remembered that the wumpus moves if you shoot and miss. 726 chars.
  4. Made grep -oE a variable. Saved 5 chars.
  5. Made [a-z]{3} a variable. Saved 3 chars.
  6. Made echo a variable. Saved 5 chars.
  7. Acted on most of @Dennis 's suggestions. Saved 72 chars.
  8. Added all remaining suggestions. Saved 68 chars.
  9. Saved 2 chars from @DigitalTrauma 's suggestion.
  10. Fixed a major bug where you can only shoot the wumpus if it is on the right. Same character count.
  11. Used parameter expansion to shave off 2 chars using $m.
  12. Shaved off a lot of chars by ditching grep and being slightly more sensible.
  13. Defined C as a regexp search function to use in if statements, and E as a function printing "You killed the wumpus" and exiting.
  14. Saved 1 char by "if statement" rearrangement.
  15. Saved a lot of chars by getting rid of d, and removed unnecessary brackets.
  16. Fixed bugs. Added lots of chars :(
  17. MOARR SAVINGS (http://xkcd.com/1296/)
  18. Another of @Dennis 's ideas (saving a few chars), and my crafty (ab)use of indirection (saving 1 char).
  19. Style fix for q().
  20. re-added proper output

Sample run

"In:" is input, "Out: is output".

The player wanders around for a bit, smells the wumpus and shoots. They miss, and the wumpus comes into their room and eats them.

In: 15

In: 15

In: 25

In: 25

In: 15

Out: You smell the wumpus

In: 14

Out: You missed

Out: The wumpus ate you

\$\endgroup\$
17
  • 1
    \$\begingroup\$ I think you can make your code at least 100 bytes shorter. 1. exit is only one byte longer than g=1 and it eliminates the need to test for non-zero g and some elif statements. 2. You can use ((i==35)) instead of [ $i = 35 ] and ...&&... instead of if ... then ... fi. 3. q(){ L=({a..s});$j ${L[RANDOM%19]};} and n=`$k $w$m<<<$d`;w=${n:RANDOM%2+1:1} both save a few bytes. \$\endgroup\$
    – Dennis
    Apr 23, 2014 at 18:34
  • 1
    \$\begingroup\$ Replace while :;do...done with for((;;);{...} for a 3 char saving \$\endgroup\$ Apr 25, 2014 at 17:38
  • 1
    \$\begingroup\$ @professorfish: I think a function would work better than the current string-grep-cut approach. For example, d(){ x=npoemfgnshtoksblbtckpdpljqniorelgfhkbqraicadjaghimsmjtqecrdf;s=${x:3*30#$1-30:3};} will allow you to replace the definitions of s and n with d $p and d $w. If you furthermore define u=${s#*$r}$s (and adjust the definitions of l and f accordingly), you won't need $k and $m anymore. Saves 83 bytes, I think. Also, the space in q () is not required. \$\endgroup\$
    – Dennis
    Apr 26, 2014 at 3:46
  • 1
    \$\begingroup\$ @professorfish: And you can save 3 additional bytes by defining c(){ [[ $1 =~ $2 ]];} and replacing, e.g., the second to last line with c $r $b||{ $j You missed;d $w;w=${s:RANDOM%2+1:1};}. \$\endgroup\$
    – Dennis
    Apr 26, 2014 at 3:59
  • 1
    \$\begingroup\$ @professorfish: Using the function I suggested should be 3 bytes shorter. You can save 106 additional bytes by replacing the four lines after b=$p with d $p;u=u${s#*$r}$s, the lines after read i with y=${u:i/10:1};C $i 5&&{ p=$y;r=$b;}||{ d $w;C $y $w&&$j You killed the wumpus&&exit;$j You missed;w=${s:RANDOM%2:1};} and getting rid of E(). \$\endgroup\$
    – Dennis
    Apr 26, 2014 at 17:46
6
\$\begingroup\$

GolfScript (206 198)

[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*({[.<><.<><]}:F~-{99rand}$~5,{.<{>.'You smell a wumpus.\n'4{$F@?~!!*}:Q~{print}:,~}3*{>.'You feel a breeze.\n'5Q,}3*'"#{'C):C';STDIN.gets()}"'++~~:&9/{>}*&5%{'You killed the wumpus.'3Q{\<{>}3rand*\"Your arrow didn't hit anything.\n",0}or}{\;.'You fell into the pit.'4Q}if\.'You were killed by the wumpus.'4Q@or:n!}do];

Finally caught up with Dennis' lookup table version, from which it borrows quite a bit. The interesting thing about this version is that it doesn't have a lookup table for the room layout.

The 60 rotational symmetries of an icosahedron are isomorphic to the alternating group on 5 letters, A_5. After trying all kinds of approaches to representing the group compactly, I've come back to the simplest one: each element is a permutation of even parity. The group can be generated from two generators in more than one way: the approach I'm taking uses the generators 3 and 3 1. These allow us to generate 1 = 3 3 1, 2 = 3 3 1 3 1, and 3 = 3.

Observe that direction 3 corresponds to an element of order 2, because after going through the door behind you, that door is behind you again. Direction 1 corresponds to an element of order 5, walking around a vertex of the icosahedron. (Similarly element 2). And the combination 3 1 is of order 3, as it cycles round the rooms adjacent to the one which starts out behind you.

So we're looking for a permutation of order 2 to represent direction 3 and a permutation of order 5 to represent direction 1 such that 3 1 is of order 3.

There are 15 permutations of order 2 in A_5, and for each one there are 8 candidate permutations for 1 (and hence for 3 1). There's an obvious attraction to [4 3 2 1 0] for 3: reversing an array is just -1%. Of its possible companion permutations 3 1 I've chosen [0 1 3 4 2], which admits a fairly short implementation as [~@].

Ungolfed

# Generate the 60 permutations by repeated application of `3 1` and `3`
[5:C,]{{.{[~@]}:>~.{-1%}:<~}%.&}8*
# Remove [0 1 2 3 4] and its equivalence class (apply 3 (3 1)^n 3 for n in 0,1,2)
({[.<><.<><]}:F~-
# Shuffle the remaining 57 options to select random starting points for wumpus and pit
# Note that this introduces a slight bias against them being in the same room,
# but it's still possible.
{99rand}$~
# Start player at [0 1 2 3 4]
5,
{
    # Stack: Pit Wumpus Player
    .<
    # The adjacent rooms to the player are Player<> , Player<>> , and Player<>>>
    # If the wumpus is in one of those rooms, say so.
    {
        >."You smell a wumpus.\n"4
        {
            # ... X str off
            $F@?~!!*
            # ... str off$F X ?~!! *
            # Which means that we leave either str (if off$ and X are equivalent)
            # or the empty string on the stack
        }:Q~
        {print}:,~
    }3*
    # Ditto for the pit
    {>."You feel a breeze.\n"5Q,}3*
    # Read one line from STDIN.
    '"#{'C):C';STDIN.gets()}"'++~~
    # Stack: Pit Wumpus Player Player< Input
    # Find the room corresponding to the specified direction.
    :&9/{>}*&
    # Stack: Pit Wumpus Player TargetRoom Input
    5%{
        # Shoots:
        "You killed the wumpus."3Q
        {
            \<{>}3rand*\ # Move the wumpus to an adjacent room
            "Your arrow didn't hit anything.\n", # Inform
            0 # Continue
        }
        or
    }{
        # Moves:
        \;
        # If player and pit share a room, say so.
        ."You fell into the pit."4Q
    }if
    # If player and wumpus share a room, say so.
    # NB If the player walks into a room with the pit and the wumpus,
    # the `or` favours the pit death.
    \."You were killed by the wumpus."4Q@or
    # Save the topmost element of the stack for output if we break the loop. Loop if it's falsy.
    :n!
}do
# Ditch the junk.
];
\$\endgroup\$
4
  • \$\begingroup\$ Nice algebraic approach! There's a minor bug though: 10/@3%= tries to access the fourth element of an array of length 3 if the input is 35. \$\endgroup\$
    – Dennis
    Apr 30, 2014 at 5:35
  • \$\begingroup\$ @Dennis, yes, I realised after I went to bed. I can think of various ways of fixing it, all costing 2. \$\endgroup\$ Apr 30, 2014 at 7:32
  • \$\begingroup\$ You can get one char back with 9/3%@3%=. \$\endgroup\$
    – Dennis
    Apr 30, 2014 at 16:44
  • \$\begingroup\$ I'm currently 7 chars up with some more drastic restructuring. But that 1 char of 9/ instead of 10/ still works, so thanks. \$\endgroup\$ Apr 30, 2014 at 16:46
6
\$\begingroup\$

Wumpus, 384 - 129 (strings) = 255 bytes

1SDL2vSD70L?.;;3AL1a!?,9)".supmuw a llems uoY"99+1.
II5x?,&WC2.           L2a!?,9)".ezeerb a leef uoY"93*2.
L1a!,FCFC[&WCL1a!?,"!supm",AW#16#[(=]?.;;l(&o1.
    ?,".uoy eta ",".gnih","uw eht dellik uoY"#22&oN@
     #15#L2a!?. ,"supmu","tyna tih t'ndid worra ruoY"#31&oND";"4L1a!?.;;L1xSUL1xSD=F-#81~4~?.;;;CCWC=F-#97~4~?.;;;2.
 ,"nto the pit."|       "w ehT"l&oN@
 |"i llef uoY"l2-&oN@

Try it online! (Of course, TIO doesn't make a lot of sense, because you can't use the program interactively there, and once the program runs out of instructions on STDIN it will read 0 0, which is equivalent to 3 4, so you'll end up shooting arrows until the Wumpus moves there or kills you.)

When running this locally, make sure that the linefeed after the second number of each input gets flushed (because Wumpus needs it to determine that the number is over). In Powershell, I somehow need to enter one more character after the linefeed to make it work (doesn't matter which character, but I just used double linefeeds for testing).

There's a lot of room for golfing this further, but trying completely new layouts takes a while. The final score also depends a lot on the actual strings I use, because in a 2D language, a string of N bytes tends to cost you more than N bytes of source code, because it puts significant constraints on the code layout, and you often need to split it into multiple sections (incurring additional double quotes). At the extreme end, if I reduced every string to a single letter (and the -129 to -12), I'd probably be saving a ton of bytes.

Explanation

First a disclaimer: despite the language's name, it was not designed to make implementing Hunt the Wumpus particularly easy. Instead, I first designed the language around the theme of triangles, ended up with an icosahedral data structure, and decided to call it Wumpus because of that.

So yeah, while Wumpus is mainly stack-based, it also has 20 registers which are arranged around the faces of an icosahedron. That means, we get a data structure to represent the map for free. The only thing we can't do easily is find specific faces on the icosahedron, so to search for them, we need to "roll the d20" until we end up on the face we're looking for. (It's possible to do this in a deterministic way, but that would take a lot more bytes.) Searching for faces like this terminates almost surely (i.e. with probability 1), so the search running forever is not a concern in practice).

The above code is a golfed version of this first implementation with a saner layout:

1SDL2vSD70L?.;;2.  < Setup; jumps to third line which starts the main loop

3AL1a! ?,".supmuw a llems uoY"#19&oN03.          < This section checks the player's surroundings.
        L2a!?,".ezeerb a leef uoY"#18&oN04.
             AW(=12[?.;;7.

    }&WC#11.                                     < This section reads the input. The top branch moves, the bottom branch shoots
II5x^                                              and kills or moves the wumpus.
     {FCFC[&WCL1a !?,"!supmuw eht dellik uoY"#22&oN@
                    ".gnihtyna tih t'ndid worra ruoY"#31&oND#59 9L1a!?.;;L1xSUL1xSD=F-#82~9~?.;;;CCWC=F-#98~9~?.;;;#11.

L1a!?,".uoy eta supmuw ehT"#19&oN@               < This section checks whether the player dies.
     L2a!?,".tip eht otni llef uoY"#22&oN@         Otherwise, we return back to the third line.
          2.

Since the golfing mostly involved compressing the layout, I'll just explain this version for now (until I add any golfing tricks that go beyond restructuring the code).

Let's start with the setup code:

1SDL2vSD70L?.;;2.

Initially, all faces are set to 0. We'll encode the wumpus by setting the 1-bit of the corresponding face, and the pit by setting the 2-bit. This way, they can both be in the same room. The player's position will not be recorded on the icosahedron at all, instead it will always be active face (only one of the 20 registers is active at a time).

1S     Store a 1 in the initially active face to put the wumpus there.
D      Roll the d20. Applies a uniformly random rotation to the icosahedron.
L2vS   Load the value of that face (in case it's the wumpus's), set the 2-bit
       and store the result back on that face.

Now we need to find a random empty face to put the player in.

D      Roll the D20.
70     Push 7 and 0 which are the coordinates of the D in the program.
L      Load the value of the current face.
?.     If that value is non-zero (i.e. the active face has either the
       wumpus or the pit), jump back to the D to reroll the die.
;;2.   Otherwise, discard the 0 and the 7 and jump to (0, 2), which is
       the beginning of the main loop.

This next section checks the player's surroundings and prints the appropriate warnings:

3AL1a! ?,".supmuw a llems uoY"#19&oN03.
        L2a!?,".ezeerb a leef uoY"#18&oN04.
             AW(=12[?.;;7.

This is a loop which we run through 3 times. Each time, we look at the right neighbour, print the appropriate string(s) if there's a hazard and then rotate the icosahedron by 120°.

3    Push a 3 as a loop counter.
A    Tip the icosahedron onto the NW neighbour of the active face, which
     will be used to represent the right-hand room.
L1a  Extract the 1-bit of the value on that face.
!?,  If that value is zero, strafe to the next line, otherwise continue.

  ".supmuw a llems uoY"#19&oN03.
     Print "You smell a wumpus.", a linefeed and then jump to the next line.

L2a  Extract the 2-bit of the value on that face.
!?,  If that value is zero, strafe to the next line, otherwise continue.

  ".ezeerb a leef uoY"#18&oN04.
     Print "You feel a breeze.", a linefeed and then jump to the next line.
A    Tip back to the original active room (where the player is).
W    Rotate the icosahedron by 120°, so that the next iteration checks
     another neighbour.
(=   Decrement the loop counter and duplicate it.
12   Push 1, 2, the coordinates of the cell after the 3 (the loop counter).
[    Pull up one copy of the loop counter.
?.   If it's non-zero, jump to the beginning of the loop, otherwise continue.
;;7. Discard the 2 and the 1 and jump to (0, 7), which reads the player's
     input for this turn.

The next section reads two numbers from the player and then either moves the player or shoots an arrow. The former is trivial, the latter less so. The main issue for shooting the arrow is the case where it misses. In that case we a) need to go looking for the wumpus to move it, and then b) return to the player's room and the correct orientation of the icosahedron (so that "back" remains "back"). This is the most expensive part of the entire program.

    }&WC#11.
II5x^
     {FCFC[&WCL1a !?,"!supmuw eht dellik uoY"#22&oN@
                    ".gnihtyna tih t'ndid worra ruoY"#31&oND#59 9L1a!?.;;L1xSUL1xSD=F-#82~9~?.;;;CCWC=F-#98~9~?.;;;#11.

The entry point to this section is the I on the left.

II   Read the integers from STDIN.
5x   XOR the second one with 5.
^    Turn either left or right, depending on the previous result. If the
     second input is 4, XORing with 5 gives 1 and the IP turns right.
     Otherwise, we get 0 and the IP turns left.

If the player entered 5, move:

}    Turn right so that the IP moves east again.
&W   If the room indicator is X, rotate the icosahedron by X*120°. This
     puts the target room south of the active face (where the back room
     normally is).
C    Tip the icosahedron onto the southern face. This moves the player there.
     Due to the way tipping works, the formerly active face will now be
     the southern neighbour, i.e. correctly at the back of the player.
#11. Jump to (0, 11), the final section which checks whether the player
     stepped into the pit or onto the wumpus.

If the player entered 4, move:

{    Turn left so that the IP moves east again.
F    Store the active face index (the player's position) on the stack.
CFC  Also store the face index of the southern neighbour (the back room)
     on the stack, so that we can recover the correct orientation if
     we need to.
[    Pull up the player's room choice.
&WC  Tip the icosahedron onto the corresponding face (same as for the move action)
L1a  Extract the 1-bit of the value on that face to check whether the arrow
     hit the wumpus.
!?,  If that value is zero, strafe to the next line, otherwise continue.

  "!supmuw eht dellik uoY"#22&oN@
     Print "You killed the wumpus.", a linefeed, and terminate the program.

".gnihtyna tih t'ndid worra ruoY"#31&oN
     Print "Your arrow didn't hit anything." and a linefeed.

This next bit is a loop which searches for the wumpus:

D    Roll the d20. The easiest way to search for the wumpus is to look at
     random faces.
#59 9
     Push 59 and 9, the coordinates of the beginning of this loop.
L1a  Extract the 1-bit of the value on the current face.
!?.  If that value is zero, jump back to the beginning of this loop to
     try another face, otherwise continue.
;;   Discard the 9 and the 59.
L1xS Unset the 1-bit of the current face to remove the wumpus there.
U    Tip the icosahedron onto a random neighbouring face. This moves us
     to a random adjacent room.
L1xS Set the 1-bit of the current face to put the wumpus there.

This next bit contains two loops which get us back to the player's room
with the correct orientation. We do this by first searching for the room
at the player's back, and then looking through its neighbours to find the
player's room.

D    Roll the d20.
=F-  Duplicate the back room index and subtract the current face index.
#82~9~
     Push 82 and 9 and pull up the difference we just computed.
?.   If the difference is non-zero (we've got the wrong room), jump back
     to the D to try again. Otherwise continue.
;;;  We've found the back room. Discard the 9, the 82 and the back room index.
C    Tip the icosahedron onto the southern face (one of the candidate
     neighbours which might be the player's room).
CWC  This begins the loop that searches for the player's room. Tip onto
     the back room, rotate by 120°, tip back. This cycles through the
     neighbours of the back room, while keeping the active face on those
     neighbours.
=F-  Duplicate the player's room index and subtract the current face index.
#98~9~
     Push 98 and 9 and pull up the difference we just computed.
?.   If the difference is non-zero (we've got the wrong room), jump back
     to the CWC to try again. Otherwise continue.
;;;  We've found the player's room and since we entered from the back room
     via C, we've also got the correct orientation. Discard the 9, the 98
     and the player's room index.
#11. Jump to (0, 11), the final section which checks whether the player
     stepped into the pit or onto the wumpus.

Phew, that was the hard part. Now we just need to check whether the player dies and otherwise start over the main loop:

L1a!?,".uoy eta supmuw ehT"#19&oN@
     L2a!?,".tip eht otni llef uoY"#22&oN@
          2.

The structure of this section is essentially identical to the structure we used when checking the player's surroundings: we check the 1-bit of the current face (the player's room) and if it's set we print The wumpus ate you. and terminate the program. Otherwise, we check the 2-bit and it's set we print You fell into the pit. and terminate the program. Otherwise we reach the 2. which jumps back to the beginning of the main loop (at coordinates (0, 2)).

\$\endgroup\$
1
\$\begingroup\$

awk - big

This didn't turn out as short as I had hoped, but I took a slightly different approach to dealing with the graph, so I'm posting the ungolfed version anyway.

I took advantage of the fact that an icosahedron (20 sided polyhedron) under rotations preserving orientation is isomorphic to the alternating group of degree 5 (5 element permutations having an even number of even length cycles). I then choose two permutations with cycle length 5 as "left" and "right", and I choose one permutation with cycle length 2 as "back". Using these, I build up the graph from one room by walking the Hamiltonian path (2xRRRLLLRLRL, using 3xRB in each room to capture the 3 possible directions).

function meta(z,a,b,c,d) {
    if(z==COMPOSE) {
        split(a,c,"");
        split(b,d,"");
        return c[d[1]]c[d[2]]c[d[3]]c[d[4]]c[d[5]];
    }
    if(z==WALK) {
        split(R" "R" "R" "L" "L" "L" "R" "L" "R" "L,c);
        for(b = 1; b <= 20; b++) {
            map[a] = b;
            a = meta(COMPOSE,meta(COMPOSE,a,R),B);
            map[a] = b;
            a = meta(COMPOSE,meta(COMPOSE,a,R),B);
            map[a] = b;
            a = meta(COMPOSE,meta(COMPOSE,a,R),B);
            a = meta(COMPOSE, a, c[b % 10 + 1]);
        }
    }
    if(z==TEST) {
        a = map[meta(COMPOSE,U,L)];
        b = map[meta(COMPOSE,U,R)];
        c = map[meta(COMPOSE,U,B)];
        if(a==W||b==W||c==W) print "You smell the wumpus";
        if(a==P||b==P||c==P) print "You feel a breeze";
        if(map[U]==W) {
            print "You have been eaten by the wumpus";
            exit;
        }
        if(map[U]==P) {
            print "You have fallen into a bottomless pit";
            exit;
        }
    }
    if(z==ARROWTEST) {
        if(A==W) {
            print "You have slain the wumpus!";
            exit;
        } else {
            for(a in p) if(map[a]==W) break;
            W=map[meta(COMPOSE,a,v[int(rand()*3)+1])];
        }
    }
}

BEGIN {
    COMPOSE = 0;
    WALK = 1;
    TEST = 2;
    ARROWTEST = 3;
    L = 35214;
    R = 35421;
    B = 35142;
    split(R" "L" "B,V);
    meta(WALK,L);
    W = int(rand()*19)+2;
    P = int(rand()*19)+2;
    U = L;
    meta(TEST);
}

{
    d=int($0/10);
    m=$0%10;
    if(m==5) U = meta(COMPOSE,U,V[d]);
    else if(m==4) {
        A = map[meta(COMPOSE,U,V[d])];
        meta(ARROWTEST);
    }
    meta(TEST);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.