Bash, 726 721 718 713 645 577 575 573 498 454
Please help me golf this even more! (Can Bash compete with GolfScript?) A lot of credit goes to @Dennis for extreme golfing.
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
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 E(){ $j You killed the wumpus;exit;} q(){ L=({a..s});$j ${L[RANDOM%19]};} C(){ [[ $1 =~ $2 ]];} w=`q` h=`q` Z=npoemfgnshtoksblbtckpdpljqniorelgfhkbqraicadjaghimsmjtqecrdf d(){ $j ${Z:(30#$1*3-30):3};} for((;;));{ b=$p s=`d $p` u=${s#*$r}$s l=${u:0:1} f=${u:1:1} C $w $p&&$j The wumpus ate you&&exit C $h $p&&$j You fell into the pit&&exit C $u $w&&$j You smell the wumpus C $u $h&&$j You feel a breeze from a pit read i C $i 35&&p=$r&&r=$b C $i 25&&p=$l&&r=$b C $i 15&&p=$f&&r=$b C $i 4&&{ C $f $w&&C $i 1&&E;C $l $w&&C $i 2&&E;C $r $w&&E;};} Version History
- Initial release, 698 chars
- 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.
- Remembered that the wumpus moves if you shoot and miss. 726 chars.
- Made
grep -oEa variable. Saved 5 chars. - Made
[a-z]{3}a variable. Saved 3 chars. - Made
echoa variable. Saved 5 chars. - Acted on most of @Dennis 's suggestions. Saved 72 chars.
- Added all remaining suggestions. Saved 68 chars.
- Saved 2 chars from @DigitalTrauma 's suggestion.
- Fixed a major bug where you can only shoot the wumpus if it is on the right. Same character count.
- Used parameter expansion to shave off 2 chars using
$m. - Shaved off a lot of chars by ditching
grepand being slightly more sensible. - Defined
Cas a regexp search function to use in if statements, andEas a function printing "You killed the wumpus" and exiting.
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