Jump to content

Hunt the Wumpus: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 39:
See [[Hunt_The_Wumpus/AutoHotkey]]
 
=={{header|C sharp|C#}}==
See [[Hunt_The_Wumpus/CSharp]].
 
=={{header|Go}}==
I've tried to stick as closely as possible to the task description but, as some points are unclear, I've had to make the following assumptions:
 
1. The bats and the pits are in separate rooms but the Wumpus can be in any room whether there's another hazard in it or not.
 
2. The game starts with the player in room 1 which initially contains no hazards.
 
3. If a bat transports the player to a random empty room, it returns to its original room.
 
4. If the Wumpus wakes up and moves to an 'adjacent' room, it means a room adjacent to the Wumpus not necessarily the player.
<lang go>package main
 
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
 
var cave = map[int][3]int{
1: {2, 3, 4}, 2: {1, 5, 6}, 3: {1, 7, 8}, 4: {1, 9, 10}, 5: {2, 9, 11},
6: {2, 7, 12}, 7: {3, 6, 13}, 8: {3, 10, 14}, 9: {4, 5, 15}, 10: {4, 8, 16},
11: {5, 12, 17}, 12: {6, 11, 18}, 13: {7, 14, 18}, 14: {8, 13, 19},
15: {9, 16, 17}, 16: {10, 15, 19}, 17: {11, 20, 15}, 18: {12, 13, 20},
19: {14, 16, 20}, 20: {17, 18, 19},
}
 
var player, wumpus, bat1, bat2, pit1, pit2 int
 
var arrows = 5
 
func isEmpty(r int) bool {
if r != player && r != wumpus && r != bat1 && r != bat2 && r != pit1 && r != pit2 {
return true
}
return false
}
 
func sense(adj [3]int) {
bat := false
pit := false
for _, ar := range adj {
if ar == wumpus {
fmt.Println("You smell something terrible nearby.")
}
switch ar {
case bat1, bat2:
if !bat {
fmt.Println("You hear a rustling.")
bat = true
}
case pit1, pit2:
if !pit {
fmt.Println("You feel a cold wind blowing from a nearby cavern.")
pit = true
}
}
}
fmt.Println()
}
 
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
 
func plural(n int) string {
if n != 1 {
return "s"
}
return ""
}
 
func main() {
rand.Seed(time.Now().UnixNano())
player = 1
wumpus = rand.Intn(19) + 2 // 2 to 20
bat1 = rand.Intn(19) + 2
for {
bat2 = rand.Intn(19) + 2
if bat2 != bat1 {
break
}
}
for {
pit1 = rand.Intn(19) + 2
if pit1 != bat1 && pit1 != bat2 {
break
}
}
for {
pit2 = rand.Intn(19) + 2
if pit2 != bat1 && pit2 != bat2 && pit2 != pit1 {
break
}
}
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Printf("\nYou are in room %d with %d arrow%s left\n", player, arrows, plural(arrows))
adj := cave[player]
fmt.Printf("The adjacent rooms are %v\n", adj)
sense(adj)
var room int
for {
fmt.Print("Choose an adjacent room : ")
scanner.Scan()
room, _ = strconv.Atoi(scanner.Text())
if room != adj[0] && room != adj[1] && room != adj[2] {
fmt.Println("Invalid response, try again")
} else {
break
}
}
check(scanner.Err())
var action byte
for {
fmt.Print("Walk or shoot w/s : ")
scanner.Scan()
reply := strings.ToLower(scanner.Text())
if len(reply) != 1 || (len(reply) == 1 && reply[0] != 'w' && reply[0] != 's') {
fmt.Println("Invalid response, try again")
} else {
action = reply[0]
break
}
}
check(scanner.Err())
if action == 'w' {
player = room
switch player {
case wumpus:
fmt.Println("You have been eaten by the Wumpus and lost the game!")
return
case pit1, pit2:
fmt.Println("You have fallen down a bottomless pit and lost the game!")
return
case bat1, bat2:
for {
room = rand.Intn(19) + 2
if isEmpty(room) {
fmt.Println("A bat has transported you to a random empty room")
player = room
break
}
}
}
} else {
if room == wumpus {
fmt.Println("You have killed the Wumpus and won the game!!")
return
} else {
chance := rand.Intn(4) // 0 to 3
if chance > 0 { // 75% probability
wumpus = cave[wumpus][rand.Intn(3)]
if player == wumpus {
fmt.Println("You have been eaten by the Wumpus and lost the game!")
return
}
}
}
arrows--
if arrows == 0 {
fmt.Println("You have run out of arrows and lost the game!")
return
}
}
}
}</lang>
 
=={{header|C++}}==
Line 705 ⟶ 531:
}
 
</lang>
 
=={{header|IS-BASIC}}==
<lang IS-BASIC>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 ELSE
610 END SELECT
620 IF LO(L)=4 OR LO(L)=5 THEN PRINT "You fall into a bottomless pit.":EXIT DO
630 LOOP
640 DEF MON(X)=X=LO(RO(L,1)) OR X=LO(RO(L,2)) OR X=LO(RO(L,3))
650 DEF CHK(X)=X=RO(L,1) OR X=RO(L,2) OR X=RO(L,3)
660 DEF INIT
670 TEXT 40:PRINT "Hunt the Wumpus";CHR$(241)
680 FOR I=1 TO 20 ! Create the cave
690 LET LO(I)=0
700 FOR J=1 TO 3
710 READ RO(I,J)
720 NEXT
730 NEXT
740 LET WPOS=RND(19)+2:LET LO(WPOS)=1
750 FOR I=2 TO 5
760 DO
770 LET T=RND(19)+2
780 LOOP UNTIL LO(T)=0
790 LET LO(T)=I
800 NEXT
810 END DEF
820 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
830 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</lang>
 
=={{header|Java}}==
See [[Hunt_The_Wumpus/Java]].
 
=={{header|Javascript}}==
See [[Hunt_The_Wumpus/Javascript]].
 
 
=={{header|Julia}}==
A translation from the original Basic, using the original Basic code and text at https://github.com/kingsawyer/wumpus/blob/master/wumpus.basic as a guide.
<lang julia>const starttxt = """
 
ATTENTION ALL WUMPUS LOVERS!!!
THERE ARE NOW TWO ADDITIONS TO THE WUMPUS FAMILY
OF PROGRAMS.
 
WUMP2: SOME DIFFERENT CAVE ARRANGEMENTS
WUMP3: DIFFERENT HAZARDS
 
"""
 
const helptxt = """
WELCOME TO 'HUNT THE WUMPUS'
THE WUMPUS LIVES IN A CAVE OF 20 ROOMS. EACH ROOM
HAS 3 TUNNELS LEADING TO OTHER ROOMS. (LOOK AT A
DODECAHEDRON TO SEE HOW THIS WORKS-IF YOU DON'T KNOW
WHAT A DODECAHEDRON IS, ASK SOMEONE)
 
HAZARDS:
BOTTOMLESS PITS - TWO ROOMS HAVE BOTTOMLESS PITS IN THEM
IF YOU GO THERE, YOU FALL INTO THE PIT (& LOSE!)
SUPER BATS - TWO OTHER ROOMS HAVE SUPER BATS. IF YOU
GO THERE, A BAT GRABS YOU AND TAKES YOU TO SOME OTHER
ROOM AT RANDOM. (WHICH MIGHT BE TROUBLESOME)
 
WUMPUS:
THE WUMPUS IS NOT BOTHERED BY THE HAZARDS (HE HAS SUCKER
FEET AND IS TOO BIG FOR A BAT TO LIFT). USUALLY
HE IS ASLEEP. TWO THINGS WAKE HIM UP: YOUR ENTERING
HIS ROOM OR YOUR SHOOTING AN ARROW.
IF THE WUMPUS WAKES, HE MOVES (P=.75) ONE ROOM
OR STAYS STILL (P=.25). AFTER THAT, IF HE IS WHERE YOU
ARE, HE EATS YOU UP (& YOU LOSE!)
 
YOU:
EACH TURN YOU MAY MOVE OR SHOOT A CROOKED ARROW
MOVING: YOU CAN GO ONE ROOM (THRU ONE TUNNEL)
ARROWS: YOU HAVE 5 ARROWS. YOU LOSE WHEN YOU RUN OUT.
EACH ARROW CAN GO FROM 1 TO 5 ROOMS. YOU AIM BY TELLING
THE COMPUTER THE ROOM#S YOU WANT THE ARROW TO GO TO.
IF THE ARROW CAN'T GO THAT WAY (IE NO TUNNEL) IT MOVES
AT RANDOM TO THE NEXT ROOM.
IF THE ARROW HITS THE WUMPUS, YOU WIN.
IF THE ARROW HITS YOU, YOU LOSE.
 
WARNINGS:
WHEN YOU ARE ONE ROOM AWAY FROM WUMPUS OR HAZARD,
THE COMPUTER SAYS:
WUMPUS- 'I SMELL A WUMPUS'
BAT - 'BATS NEARBY'
PIT - 'I FEEL A DRAFT'
 
"""
 
function queryprompt(query, choices, choicetxt="")
carr = map(x -> uppercase(strip(string(x))), collect(choices))
while true
print(query, " ", choicetxt == "" ? carr : choicetxt, ": ")
choice = uppercase(strip(readline(stdin)))
if choice in carr
return choice
end
println()
end
end
 
function wumpushunt(cheatmode = false)
println(starttxt)
arrows = 5
rooms = Vector{Vector{Int}}()
push!(rooms, [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], [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])
roomcontents = shuffle(push!(fill("Empty", 15), "Bat", "Bat", "Pit", "Pit", "Wumpus"))
randnextroom(room) = rand(rooms[room])
newplayerroom(cr, range = 40) = (for i in 1:range cr = randnextroom(cr) end; cr)
 
function senseroom(p)
linkedrooms = rooms[p]
if cheatmode
println("linked rooms are $(rooms[p]), which have $(roomcontents[rooms[p][1]]),
$(roomcontents[rooms[p][2]]), $(roomcontents[rooms[p][3]])")
end
if any(x -> roomcontents[x] == "Wumpus", linkedrooms)
println("I SMELL A WUMPUS!")
end
if any(x -> roomcontents[x] == "Pit", linkedrooms)
println("I FEEL A DRAFT")
end
if any(x -> roomcontents[x] == "Bat", linkedrooms)
println("BATS NEARBY!")
end
end
 
function arrowflight(arrowroom)
if roomcontents[arrowroom] == "Wumpus"
println("AHA! YOU GOT THE WUMPUS!")
return("win")
elseif any(x -> roomcontents[x] == "Wumpus", rooms[arrowroom])
numrooms = rand([0, 1, 2, 3])
if numrooms > 0
println("...OOPS! BUMPED A WUMPUS!")
wroom = rooms[arrowroom][findfirst(x -> roomcontents[x] == "Wumpus", rooms[arrowroom])]
for i in 1:3
tmp = wroom
wroom = rand(rooms[wroom])
if wroom == playerroom
println("TSK TSK TSK- WUMPUS GOT YOU!")
return "lose"
else
roomcontents[tmp] = roomcontents[wroom]
roomcontents[wroom] = "Wumpus"
end
end
end
elseif arrowroom == playerroom
println("OUCH! ARROW GOT YOU!")
return "lose"
end
return ""
end
 
println("HUNT THE WUMPUS")
playerroom = 1
while true
playerroom = newplayerroom(playerroom)
if roomcontents[playerroom] == "Empty"
break
end
end
while arrows > 0
senseroom(playerroom)
println("YOU ARE IN ROOM $playerroom. TUNNELS LEAD TO ", join(rooms[playerroom], ";"))
choice = queryprompt("SHOOT OR MOVE (H FOR HELP)", ["S", "M", "H"])
if choice == "M"
choice = queryprompt("WHERE TO", rooms[playerroom])
playerroom = parse(Int, choice)
if roomcontents[playerroom] == "Wumpus"
println("TSK TSK TSK- WUMPUS GOT YOU!")
return "lose"
elseif roomcontents[playerroom] == "Pit"
println("YYYIIIIEEEE . . . FELL IN PIT")
return "lose"
elseif roomcontents[playerroom] == "Bat"
senseroom(playerroom)
println("ZAP--SUPER BAT SNATCH! ELSEWHEREVILLE FOR YOU!")
playerroom = newplayerroom(playerroom, 10)
end
elseif choice == "S"
distance = parse(Int, queryprompt("NO. OF ROOMS(1-5)", 1:5))
choices = zeros(Int, 5)
arrowroom = playerroom
for i in 1:distance
choices[i] = parse(Int, queryprompt("ROOM #", 1:20, "1-20"))
while i > 2 && choices[i] == choices[i-2]
println("ARROWS AREN'T THAT CROOKED - TRY ANOTHER ROOM")
choices[i] = parse(Int, queryprompt("ROOM #", 1:20, "1-20"))
end
arrowroom = choices[i]
end
arrowroom = playerroom
for rm in choices
if rm != 0
if !(rm in rooms[arrowroom])
rm = rand(rooms[arrowroom])
end
arrowroom = rm
if (ret = arrowflight(arrowroom)) != ""
return ret
end
end
end
arrows -= 1
println("MISSED")
elseif choice == "H"
println(helptxt)
end
end
println("OUT OF ARROWS.\nHA HA HA - YOU LOSE!")
return "lose"
end
 
while true
result = wumpushunt()
println("Game over. You $(result)!")
if queryprompt("Play again?", ["Y", "N"]) == "N"
break
end
end
</lang>
 
Line 1,347 ⟶ 896:
)
(StartGame)
</lang>
 
=={{header|Go}}==
I've tried to stick as closely as possible to the task description but, as some points are unclear, I've had to make the following assumptions:
 
1. The bats and the pits are in separate rooms but the Wumpus can be in any room whether there's another hazard in it or not.
 
2. The game starts with the player in room 1 which initially contains no hazards.
 
3. If a bat transports the player to a random empty room, it returns to its original room.
 
4. If the Wumpus wakes up and moves to an 'adjacent' room, it means a room adjacent to the Wumpus not necessarily the player.
<lang go>package main
 
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
 
var cave = map[int][3]int{
1: {2, 3, 4}, 2: {1, 5, 6}, 3: {1, 7, 8}, 4: {1, 9, 10}, 5: {2, 9, 11},
6: {2, 7, 12}, 7: {3, 6, 13}, 8: {3, 10, 14}, 9: {4, 5, 15}, 10: {4, 8, 16},
11: {5, 12, 17}, 12: {6, 11, 18}, 13: {7, 14, 18}, 14: {8, 13, 19},
15: {9, 16, 17}, 16: {10, 15, 19}, 17: {11, 20, 15}, 18: {12, 13, 20},
19: {14, 16, 20}, 20: {17, 18, 19},
}
 
var player, wumpus, bat1, bat2, pit1, pit2 int
 
var arrows = 5
 
func isEmpty(r int) bool {
if r != player && r != wumpus && r != bat1 && r != bat2 && r != pit1 && r != pit2 {
return true
}
return false
}
 
func sense(adj [3]int) {
bat := false
pit := false
for _, ar := range adj {
if ar == wumpus {
fmt.Println("You smell something terrible nearby.")
}
switch ar {
case bat1, bat2:
if !bat {
fmt.Println("You hear a rustling.")
bat = true
}
case pit1, pit2:
if !pit {
fmt.Println("You feel a cold wind blowing from a nearby cavern.")
pit = true
}
}
}
fmt.Println()
}
 
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
 
func plural(n int) string {
if n != 1 {
return "s"
}
return ""
}
 
func main() {
rand.Seed(time.Now().UnixNano())
player = 1
wumpus = rand.Intn(19) + 2 // 2 to 20
bat1 = rand.Intn(19) + 2
for {
bat2 = rand.Intn(19) + 2
if bat2 != bat1 {
break
}
}
for {
pit1 = rand.Intn(19) + 2
if pit1 != bat1 && pit1 != bat2 {
break
}
}
for {
pit2 = rand.Intn(19) + 2
if pit2 != bat1 && pit2 != bat2 && pit2 != pit1 {
break
}
}
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Printf("\nYou are in room %d with %d arrow%s left\n", player, arrows, plural(arrows))
adj := cave[player]
fmt.Printf("The adjacent rooms are %v\n", adj)
sense(adj)
var room int
for {
fmt.Print("Choose an adjacent room : ")
scanner.Scan()
room, _ = strconv.Atoi(scanner.Text())
if room != adj[0] && room != adj[1] && room != adj[2] {
fmt.Println("Invalid response, try again")
} else {
break
}
}
check(scanner.Err())
var action byte
for {
fmt.Print("Walk or shoot w/s : ")
scanner.Scan()
reply := strings.ToLower(scanner.Text())
if len(reply) != 1 || (len(reply) == 1 && reply[0] != 'w' && reply[0] != 's') {
fmt.Println("Invalid response, try again")
} else {
action = reply[0]
break
}
}
check(scanner.Err())
if action == 'w' {
player = room
switch player {
case wumpus:
fmt.Println("You have been eaten by the Wumpus and lost the game!")
return
case pit1, pit2:
fmt.Println("You have fallen down a bottomless pit and lost the game!")
return
case bat1, bat2:
for {
room = rand.Intn(19) + 2
if isEmpty(room) {
fmt.Println("A bat has transported you to a random empty room")
player = room
break
}
}
}
} else {
if room == wumpus {
fmt.Println("You have killed the Wumpus and won the game!!")
return
} else {
chance := rand.Intn(4) // 0 to 3
if chance > 0 { // 75% probability
wumpus = cave[wumpus][rand.Intn(3)]
if player == wumpus {
fmt.Println("You have been eaten by the Wumpus and lost the game!")
return
}
}
}
arrows--
if arrows == 0 {
fmt.Println("You have run out of arrows and lost the game!")
return
}
}
}
}</lang>
 
=={{header|IS-BASIC}}==
<lang IS-BASIC>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 ELSE
610 END SELECT
620 IF LO(L)=4 OR LO(L)=5 THEN PRINT "You fall into a bottomless pit.":EXIT DO
630 LOOP
640 DEF MON(X)=X=LO(RO(L,1)) OR X=LO(RO(L,2)) OR X=LO(RO(L,3))
650 DEF CHK(X)=X=RO(L,1) OR X=RO(L,2) OR X=RO(L,3)
660 DEF INIT
670 TEXT 40:PRINT "Hunt the Wumpus";CHR$(241)
680 FOR I=1 TO 20 ! Create the cave
690 LET LO(I)=0
700 FOR J=1 TO 3
710 READ RO(I,J)
720 NEXT
730 NEXT
740 LET WPOS=RND(19)+2:LET LO(WPOS)=1
750 FOR I=2 TO 5
760 DO
770 LET T=RND(19)+2
780 LOOP UNTIL LO(T)=0
790 LET LO(T)=I
800 NEXT
810 END DEF
820 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
830 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</lang>
 
=={{header|Java}}==
See [[Hunt_The_Wumpus/Java]].
 
=={{header|Javascript}}==
See [[Hunt_The_Wumpus/Javascript]].
 
=={{header|Julia}}==
A translation from the original Basic, using the original Basic code and text at https://github.com/kingsawyer/wumpus/blob/master/wumpus.basic as a guide.
<lang julia>const starttxt = """
 
ATTENTION ALL WUMPUS LOVERS!!!
THERE ARE NOW TWO ADDITIONS TO THE WUMPUS FAMILY
OF PROGRAMS.
 
WUMP2: SOME DIFFERENT CAVE ARRANGEMENTS
WUMP3: DIFFERENT HAZARDS
 
"""
 
const helptxt = """
WELCOME TO 'HUNT THE WUMPUS'
THE WUMPUS LIVES IN A CAVE OF 20 ROOMS. EACH ROOM
HAS 3 TUNNELS LEADING TO OTHER ROOMS. (LOOK AT A
DODECAHEDRON TO SEE HOW THIS WORKS-IF YOU DON'T KNOW
WHAT A DODECAHEDRON IS, ASK SOMEONE)
 
HAZARDS:
BOTTOMLESS PITS - TWO ROOMS HAVE BOTTOMLESS PITS IN THEM
IF YOU GO THERE, YOU FALL INTO THE PIT (& LOSE!)
SUPER BATS - TWO OTHER ROOMS HAVE SUPER BATS. IF YOU
GO THERE, A BAT GRABS YOU AND TAKES YOU TO SOME OTHER
ROOM AT RANDOM. (WHICH MIGHT BE TROUBLESOME)
 
WUMPUS:
THE WUMPUS IS NOT BOTHERED BY THE HAZARDS (HE HAS SUCKER
FEET AND IS TOO BIG FOR A BAT TO LIFT). USUALLY
HE IS ASLEEP. TWO THINGS WAKE HIM UP: YOUR ENTERING
HIS ROOM OR YOUR SHOOTING AN ARROW.
IF THE WUMPUS WAKES, HE MOVES (P=.75) ONE ROOM
OR STAYS STILL (P=.25). AFTER THAT, IF HE IS WHERE YOU
ARE, HE EATS YOU UP (& YOU LOSE!)
 
YOU:
EACH TURN YOU MAY MOVE OR SHOOT A CROOKED ARROW
MOVING: YOU CAN GO ONE ROOM (THRU ONE TUNNEL)
ARROWS: YOU HAVE 5 ARROWS. YOU LOSE WHEN YOU RUN OUT.
EACH ARROW CAN GO FROM 1 TO 5 ROOMS. YOU AIM BY TELLING
THE COMPUTER THE ROOM#S YOU WANT THE ARROW TO GO TO.
IF THE ARROW CAN'T GO THAT WAY (IE NO TUNNEL) IT MOVES
AT RANDOM TO THE NEXT ROOM.
IF THE ARROW HITS THE WUMPUS, YOU WIN.
IF THE ARROW HITS YOU, YOU LOSE.
 
WARNINGS:
WHEN YOU ARE ONE ROOM AWAY FROM WUMPUS OR HAZARD,
THE COMPUTER SAYS:
WUMPUS- 'I SMELL A WUMPUS'
BAT - 'BATS NEARBY'
PIT - 'I FEEL A DRAFT'
 
"""
 
function queryprompt(query, choices, choicetxt="")
carr = map(x -> uppercase(strip(string(x))), collect(choices))
while true
print(query, " ", choicetxt == "" ? carr : choicetxt, ": ")
choice = uppercase(strip(readline(stdin)))
if choice in carr
return choice
end
println()
end
end
 
function wumpushunt(cheatmode = false)
println(starttxt)
arrows = 5
rooms = Vector{Vector{Int}}()
push!(rooms, [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], [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])
roomcontents = shuffle(push!(fill("Empty", 15), "Bat", "Bat", "Pit", "Pit", "Wumpus"))
randnextroom(room) = rand(rooms[room])
newplayerroom(cr, range = 40) = (for i in 1:range cr = randnextroom(cr) end; cr)
 
function senseroom(p)
linkedrooms = rooms[p]
if cheatmode
println("linked rooms are $(rooms[p]), which have $(roomcontents[rooms[p][1]]),
$(roomcontents[rooms[p][2]]), $(roomcontents[rooms[p][3]])")
end
if any(x -> roomcontents[x] == "Wumpus", linkedrooms)
println("I SMELL A WUMPUS!")
end
if any(x -> roomcontents[x] == "Pit", linkedrooms)
println("I FEEL A DRAFT")
end
if any(x -> roomcontents[x] == "Bat", linkedrooms)
println("BATS NEARBY!")
end
end
 
function arrowflight(arrowroom)
if roomcontents[arrowroom] == "Wumpus"
println("AHA! YOU GOT THE WUMPUS!")
return("win")
elseif any(x -> roomcontents[x] == "Wumpus", rooms[arrowroom])
numrooms = rand([0, 1, 2, 3])
if numrooms > 0
println("...OOPS! BUMPED A WUMPUS!")
wroom = rooms[arrowroom][findfirst(x -> roomcontents[x] == "Wumpus", rooms[arrowroom])]
for i in 1:3
tmp = wroom
wroom = rand(rooms[wroom])
if wroom == playerroom
println("TSK TSK TSK- WUMPUS GOT YOU!")
return "lose"
else
roomcontents[tmp] = roomcontents[wroom]
roomcontents[wroom] = "Wumpus"
end
end
end
elseif arrowroom == playerroom
println("OUCH! ARROW GOT YOU!")
return "lose"
end
return ""
end
 
println("HUNT THE WUMPUS")
playerroom = 1
while true
playerroom = newplayerroom(playerroom)
if roomcontents[playerroom] == "Empty"
break
end
end
while arrows > 0
senseroom(playerroom)
println("YOU ARE IN ROOM $playerroom. TUNNELS LEAD TO ", join(rooms[playerroom], ";"))
choice = queryprompt("SHOOT OR MOVE (H FOR HELP)", ["S", "M", "H"])
if choice == "M"
choice = queryprompt("WHERE TO", rooms[playerroom])
playerroom = parse(Int, choice)
if roomcontents[playerroom] == "Wumpus"
println("TSK TSK TSK- WUMPUS GOT YOU!")
return "lose"
elseif roomcontents[playerroom] == "Pit"
println("YYYIIIIEEEE . . . FELL IN PIT")
return "lose"
elseif roomcontents[playerroom] == "Bat"
senseroom(playerroom)
println("ZAP--SUPER BAT SNATCH! ELSEWHEREVILLE FOR YOU!")
playerroom = newplayerroom(playerroom, 10)
end
elseif choice == "S"
distance = parse(Int, queryprompt("NO. OF ROOMS(1-5)", 1:5))
choices = zeros(Int, 5)
arrowroom = playerroom
for i in 1:distance
choices[i] = parse(Int, queryprompt("ROOM #", 1:20, "1-20"))
while i > 2 && choices[i] == choices[i-2]
println("ARROWS AREN'T THAT CROOKED - TRY ANOTHER ROOM")
choices[i] = parse(Int, queryprompt("ROOM #", 1:20, "1-20"))
end
arrowroom = choices[i]
end
arrowroom = playerroom
for rm in choices
if rm != 0
if !(rm in rooms[arrowroom])
rm = rand(rooms[arrowroom])
end
arrowroom = rm
if (ret = arrowflight(arrowroom)) != ""
return ret
end
end
end
arrows -= 1
println("MISSED")
elseif choice == "H"
println(helptxt)
end
end
println("OUT OF ARROWS.\nHA HA HA - YOU LOSE!")
return "lose"
end
 
while true
result = wumpushunt()
println("Game over. You $(result)!")
if queryprompt("Play again?", ["Y", "N"]) == "N"
break
end
end
</lang>
 
Line 1,456 ⟶ 1,455:
 
</lang>
 
=={{header|Perl 6}}==
See [[Hunt_The_Wumpus/Perl_6]]
 
=={{header|Phix}}==
Line 2,157 ⟶ 2,153:
[[1,2], [1,3], [1,4], [2,1], [2,5], [2,6], [3,1], ...]
</lang>
 
 
=={{header|Racket}}==
Line 2,376 ⟶ 2,371:
S 2
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
See [[Hunt_The_Wumpus/Perl_6]]
 
=={{header|Ruby}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.