Hunt the Wumpus

From Rosetta Code
Task
Hunt the Wumpus
You are encouraged to solve this task according to the task description, using any language you may know.
This task has been flagged for clarification. Code on this page in its current state may be flagged incorrect once this task has been clarified. See this page's Talk page for discussion.

Create a simple implementation of the classic textual game Hunt The Wumpus.

The rules are:

The game is set in a cave that consists of a 20 room labyrinth. Each room is connected to 3 other rooms (the cave is modeled after the vertices of a dodecahedron). The objective of the player is to find and kill the horrendous beast Wumpus that lurks in the cave.

The player has 5 arrows. If they run out of arrows before killing the Wumpus, the player loses the game.

In the cave there are:

  • One Wumpus
  • Two giant bats
  • Two bottomless pits

If the player enters a room with the Wumpus, he is eaten by it and the game is lost.

If the player enters a room with a bottomless pit, he falls into it and the game is lost.

If the player enters a room with a giant bat, the bat takes him and transports him into a random empty room.

Each turn the player can either walk into an adjacent room or shoot into an adjacent room.

Whenever the player enters a room, he "senses" what happens in adjacent rooms. The messages are:

  • Nearby Wumpus: "You smell something terrible nearby."
  • Nearby bat: "You hear a rustling."
  • Nearby pit: "You feel a cold wind blowing from a nearby cavern."

When the player shoots, he wins the game if he is shooting in the room with the Wumpus. If he shoots into another room, the Wumpus has a 75% of chance of waking up and moving into an adjacent room: if this is the room with the player, he eats him up and the game is lost.

AutoHotkey

See Hunt_The_Wumpus/AutoHotkey

C#

See Hunt_The_Wumpus/CSharp.

Java

See Hunt_The_Wumpus/Java.

JavaScript

See Hunt_The_Wumpus/Javascript.

M2000 Interpreter

For Windows only (in Linux you hear nothing, using Wine 3.6): In Sense() you can change Print with Speech so you Hear your sense;

<lang M2000 Interpreter> Module WumpusGame {

     Print "Game: Hunt The Wumpus"
     Arrows=5
     Dim Room(1 to 20)
     Room(1)=(2,6,5),(3,8,1),(4,10,2),(5,2,3),(1,14,4)
     Room(6)=(15,1,7),(17,6,8),(7,2,9),(18,8,10),(9,3,11)
     Room(11)=(19,10,12),(11,4,13),(20,12,14),(5,11,13), (6,16,14)
     Room(16)=(20,15,17),(16,7,18),(17,9,19),(18,11,20),(19,13,16)
     Enum Things {EmptyRoom, Bat1, Bat2, Pit1, Pit2, Wumpus}
     Dim Content(1 to 20)=EmptyRoom
     i=each(Things,2)  ' from 2 to End
     While i {
           r=random(1,20)
           if Content(r)<>EmptyRoom then restart
           Content(r)=Eval(i)
     }
     WumpusPos=r
     PlayerPos=-1
     TranspotPlayer()
     Done=False
     \\ Help is statement but here used as variable
     Help=False
     While Arrows>0 And Not Done {
           Sense()
           Print "W- Walk, T - Throw Arrow, G - Give up or H for Help"
           a$=Ucase$(Key$)
           If a$="W" Then {
                 Print "Choose Tunnel to Walk: 1, 2 or 3"
                 r=Val("0"+Key$)-1
                 if r>=0 and r<=2 then {
                       PlayerPos=Array(room(PlayerPos), r)
                       Select Case Content(PlayerPos)
                       Case Wumpus
                       Eaten()
                       Case Pit1, Pit2
                       {
                             Arrows=0
                             Print "You fall to a bottomless pit;"
                       }
                       Case Bat1, Bat2
                       {
                             Print "A giant bat takes you in another room;"
                             TranspotPlayer()
                       }
                       End Select
                 }
           } Else.if a$="T" Then {
                 Arrows--
                 Print "Choose Tunnel to Throw Arrow: 1, 2  or 3"      
                 r=Val("0"+Key$)-1
                 if r>=0 and r<=2 then {
                       i=room(PlayerPos)
                       If Content(Array(i, r))=Wumpus then {
                             Done=True
                     } Else.if random(1,4)<4 then WakeWumpus()
                 }            
           } Else.if a$="G" Then {
                  Arrows=0
           } Else.if a$="H" Then Help~
     }
     If Done then Print "You kill the Monster Wumpus; You Win.": Exit
     Print "You loose."
     
     Sub TranspotPlayer()
           local r=random(1,20)
           While Content(r)<>EmptyRoom {r=random(1,20)}
           PlayerPos=r
     End Sub
     Sub WakeWumpus()
           local j=array(room(WumpusPos),random(0,2))
           If content(j)=EmptyRoom Then {
                 swap content(j), content(WumpusPos)
                 WumpusPos=j
                 If WumpusPos=PlayerPos then Eaten()
           }
     End Sub
     Sub Eaten()
           Arrows=0
           Print "You eaten by Wumpus;"
     End Sub
     Sub Sense()
           local k=Room(PlayerPos)
           local j=each(k), Wumpus_near, bat_near, pit_near
           If Help then Print "Player Room:";PlayerPos, "Wumpus Room:";WumpusPos
           While j {
                 If Help Then Print "Tunnel:";j^+1, "Room:";Array(j), "Content:";eval$(content(array(j)))
                 Select Case content(array(j))
                 Case Bat1, Bat2
                 bat_near=True
                 Case Pit1, Pit2
                 pit_near=True
                 Case Wumpus
                 Wumpus_near=True
                 End Select
           }
           If Wumpus_near Then Print "You smell something terrible nearby."
           If bat_near Then Print "You hear a rustling."
           if pit_near Then Print "You feel a cold wind blowing from a nearby cavern."
     End Sub    

} WumpusGame

</lang>

Perl 6

See Hunt_The_Wumpus/Perl_6

Phix

See Hunt_The_Wumpus/Phix.

Ruby

See Hunt_The_Wumpus/Ruby.