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[edit]

See Hunt_The_Wumpus/AutoHotkey

C#[edit]

See Hunt_The_Wumpus/CSharp.

IS-BASIC[edit]

100 PROGRAM "Wumpus.bas"
110 RANDOMIZE
120 NUMERIC RO(1 TO 20,1 TO 3),LO(1 TO 20),WPOS
130 LET ARROWS=5:LET L=1
140 CALL INIT
150 DO
160 PRINT :PRINT "You are in room";L
170 PRINT "Tunnels lead to ";RO(L,1);RO(L,2);RO(L,3)
180 IF MON(1) THEN PRINT "You smell something terrible nearby."
190 IF MON(2) OR MON(3) THEN PRINT "You hear a rustling."
200 IF MON(4) OR MON(5) THEN PRINT "You feel a cold wind blowing from a nearby cavern."
210 PRINT :PRINT "Shoot or move? (S-M)"
220 DO
230 LET K$=UCASE$(INKEY$)
240 LOOP UNTIL K$="S" OR K$="M"
250 IF K$="M" THEN ! Move
260 INPUT PROMPT "No. of rooms: ":I$
270 LET I=VAL(I$)
280 IF CHK(I) THEN
290 LET L=I
300 ELSE
310 PRINT "Not possibile."
320 END IF
330 ELSE  ! Shoot
340 INPUT PROMPT "Where? ":I$
350 LET I=VAL(I$)
360 IF CHK(I) THEN
370 IF LO(I)=1 THEN
380 PRINT "You kill the Monster Wumpus.":PRINT "You win.":EXIT DO
390 ELSE
400 PRINT "Arrows aren't that crooked - Try another room."
410 IF RND(4)<3 THEN
420 PRINT "You woke the Wumpus and he moved."
430 LET LO(WPOS)=0:LET WPOS=RO(WPOS,RND(2)+1):LET LO(WPOS)=1
440 END IF
450 LET ARROWS=ARROWS-1
460 IF ARROWS=0 THEN PRINT "You ran out of arrows.":EXIT DO
470 END IF
480 ELSE
490 PRINT "Not possibile."
500 END IF
510 END IF
520 SELECT CASE LO(L)
530 CASE 1
540 PRINT "You eaten by Wumpus.":EXIT DO
550 CASE 2,3
560 PRINT "A giant bat takes you in another room.":LET I=L
570 DO
580 LET L=RND(19)+1
590 LOOP UNTIL I<>L
600 CASE 4,5
610 PRINT "You fall into a bottomless pit.":EXIT DO
620 CASE ELSE
630 END SELECT
640 LOOP
650 DEF MON(X)=X=LO(RO(L,1)) OR X=LO(RO(L,2)) OR X=LO(RO(L,3))
660 DEF CHK(X)=X=RO(L,1) OR X=RO(L,2) OR X=RO(L,3)
670 DEF INIT
680 TEXT 40
690 PRINT "Hunt the Wumpus";CHR$(241)
700 FOR I=1 TO 20 ! Create the cave
710 LET LO(I)=0
720 FOR J=1 TO 3
730 READ RO(I,J)
740 NEXT
750 NEXT
760 LET WPOS=RND(19)+2:LET LO(WPOS)=1
770 FOR I=2 TO 5
780 DO
790 LET T=RND(19)+2
800 LOOP UNTIL LO(T)=0
810 LET LO(T)=I
820 NEXT
830 END DEF
840 DATA 2,6,5,3,8,1,4,10,2,5,2,3,1,14,4,15,1,7,17,6,8,7,2,9,18,8,10,9,3,11
850 DATA 19,10,12,11,4,13,20,12,14,5,11,13,6,16,14,20,15,17,16,7,18,17,9,19,18,11,20,19,13,16

Java[edit]

See Hunt_The_Wumpus/Java.

JavaScript[edit]

See Hunt_The_Wumpus/Javascript.

M2000 Interpreter[edit]

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;

 
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
 
 

Perl 6[edit]

See Hunt_The_Wumpus/Perl_6

Phix[edit]

See Hunt_The_Wumpus/Phix.

Ruby[edit]

See Hunt_The_Wumpus/Ruby.