Skip to main content
13 of 24
added 104 characters in body
user avatar
user avatar

Bash, 726 721 718 713 645 577 575

Please help me golf this even more!

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.

The variable $d stores the graph in the following format:

<VERTEX NUMBER><3 ADJACENT VERTICES IN CLOCKWISE ORDER><DELIMITER> 

Code

p=t r=r k='grep -oE' m='[a-z]{3}' j=echo q (){ L=({a..s});$j ${L[RANDOM%19]};} w=`q` h=`q` d=anpo_bemf_cgns_dhto_eksb_flbt_gckp_hdpl_ijqn_jior_kelg_lfhk_mbqr_naic_oadj_pagh_qims_rmjt_sqec_trdf for((;;));{ b=$p s=`$k $p$m<<<$d|cut -c2-` u=`$k $r'[a-z]{2}'<<<$s$s` l=${u:1:1} f=${u:2:1} [ $w = $p ]&&$j The wumpus ate you&&exit [ $h = $p ]&&$j You fell in the pit&&exit [[ $u =~ $w ]]&&$j You smell the wumpus [[ $u =~ $h ]]&&$j You feel a breeze from a pit read i ((i==35))&&p=$r&&r=$b ((i==25))&&p=$l&&r=$b ((i==15))&&p=$f&&r=$b [ $i = 14 -a $w = $f -o $i = 24 -a $w = $l -o $w = $r ]&&$j You killed the wumpus&&exit [ $r != $b ]&&$j You missed&&n=`$k $w$m<<<$d`&&w=${n:RANDOM%2+1: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.

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

Possible Golfing to be done

The icosahedron graph data takes up 100 bytes. Is there a way to generate it automatically?

user16402