Hunt the Wumpus: Difference between revisions

Content added Content deleted
(fix force unwrapped room variable)
m (syntax highlighting fixup automation)
Line 40: Line 40:


'''Wumpus package specification'''
'''Wumpus package specification'''
<lang Ada>package Wumpus is
<syntaxhighlight lang="ada">package Wumpus is
procedure Start_Game;
procedure Start_Game;
end Wumpus;
end Wumpus;
</syntaxhighlight>
</lang>
'''Wumpus package body'''
'''Wumpus package body'''
<lang Ada>with Ada.Numerics.Discrete_Random;
<syntaxhighlight lang="ada">with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 472: Line 472:


end Wumpus;
end Wumpus;
</syntaxhighlight>
</lang>
'''Main procedure'''
'''Main procedure'''
<syntaxhighlight lang="ada">
<lang Ada>
with Wumpus; use Wumpus;
with Wumpus; use Wumpus;


Line 481: Line 481:
Start_Game;
Start_Game;
end Main;
end Main;
</syntaxhighlight>
</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang BASIC>10 DEFINT A-Z: DIM R(19,3),P(1),B(1)
<syntaxhighlight lang="basic">10 DEFINT A-Z: DIM R(19,3),P(1),B(1)
20 FOR I=0 TO 19: READ R(I,0),R(I,1),R(I,2): NEXT
20 FOR I=0 TO 19: READ R(I,0),R(I,1),R(I,2): NEXT
30 INPUT "Enter a number";X: X=RND(-ABS(X))
30 INPUT "Enter a number";X: X=RND(-ABS(X))
Line 535: Line 535:
520 DATA 4,6,14, 5,7,16, 0,6,8, 7,9,17, 1,8,10
520 DATA 4,6,14, 5,7,16, 0,6,8, 7,9,17, 1,8,10
530 DATA 9,11,18, 2,10,12, 11,13,19, 3,12,14, 5,13,15
530 DATA 9,11,18, 2,10,12, 11,13,19, 3,12,14, 5,13,15
540 DATA 14,16,19, 6,15,17, 8,16,18, 10,17,19, 12,15,18</lang>
540 DATA 14,16,19, 6,15,17, 8,16,18, 10,17,19, 12,15,18</syntaxhighlight>


=={{header|APL}}==
=={{header|APL}}==
Line 547: Line 547:


=={{header|C++}}==
=={{header|C++}}==
<syntaxhighlight lang="cpp">
<lang CPP>
// constant value 2d array to represent the dodecahedron
// constant value 2d array to represent the dodecahedron
// room structure
// room structure
Line 1,035: Line 1,035:
}
}


</syntaxhighlight>
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


<syntaxhighlight lang="common lisp">
<lang Common Lisp>
;;; File: HuntTheWumpus.lisp
;;; File: HuntTheWumpus.lisp


Line 1,400: Line 1,400:
)
)
(StartGame)
(StartGame)
</syntaxhighlight>
</lang>


=={{header|FORTRAN}}==
=={{header|FORTRAN}}==
This code names rooms by alphabetic character, thereby making use of index function and simplifying the input to single characters. Compiled with gfortran, source compatible with FORTRAN 2008. You're welcome to move this content to a separate webpage.
This code names rooms by alphabetic character, thereby making use of index function and simplifying the input to single characters. Compiled with gfortran, source compatible with FORTRAN 2008. You're welcome to move this content to a separate webpage.
Makefile
Makefile
<lang gnu make>
<syntaxhighlight lang="gnu make">
# gnu make
# gnu make


Line 1,422: Line 1,422:
%.o: %.f08
%.o: %.f08
gfortran -std=f2008 -c $(FFLAGS) $< -o $@
gfortran -std=f2008 -c $(FFLAGS) $< -o $@
</syntaxhighlight>
</lang>
The paint subroutine is a place-holder where is envisioned to place additional symbols representing possible hazards and determined hazards. With ANSI graphics or actual pictures. Source:
The paint subroutine is a place-holder where is envisioned to place additional symbols representing possible hazards and determined hazards. With ANSI graphics or actual pictures. Source:
<lang f08>
<syntaxhighlight lang="f08">
! compilation, linux. Filename htw.f90
! compilation, linux. Filename htw.f90
! a=./htw && make $a && $a
! a=./htw && make $a && $a
Line 1,740: Line 1,740:


end program htw
end program htw
</syntaxhighlight>
</lang>
short sample run
short sample run
<pre>
<pre>
Line 1,800: Line 1,800:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>data 7,13,19,12,18,20,16,17,19,11,14,18,13,15,18,9,14,16,1,15,17,10,16,20,6,11,19,8,12,17
<syntaxhighlight lang="freebasic">data 7,13,19,12,18,20,16,17,19,11,14,18,13,15,18,9,14,16,1,15,17,10,16,20,6,11,19,8,12,17
data 4,9,13,2,10,15,1,5,11,4,6,20,5,7,12,3,6,8,3,7,10,2,4,5,1,3,9,2,8,14
data 4,9,13,2,10,15,1,5,11,4,6,20,5,7,12,3,6,8,3,7,10,2,4,5,1,3,9,2,8,14
data 1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1
data 1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1
Line 1,915: Line 1,915:
print "You have slain the Wumpus!"
print "You have slain the Wumpus!"
print "You have won!"
print "You have won!"
end</lang>
end</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
Line 1,927: Line 1,927:


4. If the Wumpus wakes up and moves to an 'adjacent' room, it means a room adjacent to the Wumpus not necessarily the player.
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
<syntaxhighlight lang="go">package main


import (
import (
Line 2,089: Line 2,089:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==


<lang Haskell>import System.Random
<syntaxhighlight lang="haskell">import System.Random
import System.IO
import System.IO
import Data.List
import Data.List
Line 2,257: Line 2,257:
putStrLn "*** HUNT THE WUMPUS ***"
putStrLn "*** HUNT THE WUMPUS ***"
putStrLn ""
putStrLn ""
playGame</lang>
playGame</syntaxhighlight>


=={{header|IS-BASIC}}==
=={{header|IS-BASIC}}==
<lang IS-BASIC>100 PROGRAM "Wumpus.bas"
<syntaxhighlight lang="is-basic">100 PROGRAM "Wumpus.bas"
110 RANDOMIZE
110 RANDOMIZE
120 NUMERIC RO(1 TO 20,1 TO 3),LO(1 TO 20),WPOS
120 NUMERIC RO(1 TO 20,1 TO 3),LO(1 TO 20),WPOS
Line 2,333: Line 2,333:
810 END DEF
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
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>
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</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
Line 2,343: Line 2,343:
=={{header|Julia}}==
=={{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.
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 = """
<syntaxhighlight lang="julia">const starttxt = """


ATTENTION ALL WUMPUS LOVERS!!!
ATTENTION ALL WUMPUS LOVERS!!!
Line 2,533: Line 2,533:
end
end
end
end
</syntaxhighlight>
</lang>


=={{header|M2000 Interpreter}}==
=={{header|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;
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;


<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module WumpusGame {
Module WumpusGame {
Print "Game: Hunt The Wumpus"
Print "Game: Hunt The Wumpus"
Line 2,641: Line 2,641:
WumpusGame
WumpusGame


</syntaxhighlight>
</lang>


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>exits = [[1,4,7], [0,2,9], [1,3,11], [2,4,13], [0,3,5],
<syntaxhighlight lang="miniscript">exits = [[1,4,7], [0,2,9], [1,3,11], [2,4,13], [0,3,5],
[4,6,14], [5,7,16], [0,6,8], [7,9,17], [1,8,10],
[4,6,14], [5,7,16], [0,6,8], [7,9,17], [1,8,10],
[9,11,18], [2,10,12], [11,13,19], [3,12,14], [5,13,15],
[9,11,18], [2,10,12], [11,13,19], [3,12,14], [5,13,15],
Line 2,724: Line 2,724:
break
break
end while
end while
end while</lang>
end while</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>import random, strformat, strutils
<syntaxhighlight lang="nim">import random, strformat, strutils


type Adjacent = array[3, int]
type Adjacent = array[3, int]
Line 2,860: Line 2,860:
randomize()
randomize()
var game = initGame()
var game = initGame()
game.play()</lang>
game.play()</syntaxhighlight>


{{out}}
{{out}}
Line 2,892: Line 2,892:
=={{header|Perl}}==
=={{header|Perl}}==
Besides running in a terminal, it also can run under xinetd.
Besides running in a terminal, it also can run under xinetd.
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # see https://rosettacode.org/wiki/Hunt_the_Wumpus
use strict; # see https://rosettacode.org/wiki/Hunt_the_Wumpus
Line 2,934: Line 2,934:
}
}
else { print "I don't understand :(\n"; }
else { print "I don't understand :(\n"; }
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,945: Line 2,945:
https://github.com/jburse/jekejeke-samples/raw/master/pack/games/wumpus-1.0.0.zip
https://github.com/jburse/jekejeke-samples/raw/master/pack/games/wumpus-1.0.0.zip


<lang prolog>
<syntaxhighlight lang="prolog">
/**
/**
* Simple text based dungeon game in Prolog.
* Simple text based dungeon game in Prolog.
Line 3,320: Line 3,320:
write(' BAT - ''BATS NEARBY'''), nl,
write(' BAT - ''BATS NEARBY'''), nl,
write(' PIT - ''I FEEL A DRAFT'''), nl, nl.
write(' PIT - ''I FEEL A DRAFT'''), nl, nl.
</syntaxhighlight>
</lang>


=={{header|Python}}==
=={{header|Python}}==
Line 3,326: Line 3,326:
This implementation of "Hunt the Wumpus" follows is based on the rules of the original game (see discussion page for more information). It uses python 3 syntax and the standard library random.
This implementation of "Hunt the Wumpus" follows is based on the rules of the original game (see discussion page for more information). It uses python 3 syntax and the standard library random.


<lang python>
<syntaxhighlight lang="python">
import random
import random


Line 3,603: Line 3,603:
WG.gameloop()
WG.gameloop()


</syntaxhighlight>
</lang>


Example run (on a windows system):
Example run (on a windows system):


<lang cmd>
<syntaxhighlight lang="cmd">
D:\Workspace\rosettacode>python wumpus.py
D:\Workspace\rosettacode>python wumpus.py
HUNT THE WUMPUS
HUNT THE WUMPUS
Line 3,622: Line 3,622:


Game over!
Game over!
</syntaxhighlight>
</lang>


To create an example for a valid edge list, navigate to the folder where you saved wumpus.py, open up a python interactive shell and do:
To create an example for a valid edge list, navigate to the folder where you saved wumpus.py, open up a python interactive shell and do:


<lang cmd>
<syntaxhighlight lang="cmd">
>>> import wumpus
>>> import wumpus
>>> WG = wumpus.WumpusGame()
>>> WG = wumpus.WumpusGame()
Line 3,632: Line 3,632:
>>> print edges
>>> print edges
[[1,2], [1,3], [1,4], [2,1], [2,5], [2,6], [3,1], ...]
[[1,2], [1,3], [1,4], [2,1], [2,5], [2,6], [3,1], ...]
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 3,836: Line 3,836:
(let ([current-game-state (new-game-state)])
(let ([current-game-state (new-game-state)])
(game-loop current-game-state)))
(game-loop current-game-state)))
</syntaxhighlight>
</lang>


<lang cmd>
<syntaxhighlight lang="cmd">
Start the game with
Start the game with


Line 3,850: Line 3,850:


S 2
S 2
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 3,857: Line 3,857:


=={{header|Red}}==
=={{header|Red}}==
<lang red>
<syntaxhighlight lang="red">
Red [
Red [
Title: "Hunt the Wumpus"
Title: "Hunt the Wumpus"
Line 4,181: Line 4,181:


start
start
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 4,190: Line 4,190:


=={{header|SenseTalk}}==
=={{header|SenseTalk}}==
<lang sensetalk>
<syntaxhighlight lang="sensetalk">
-- HUNT THE WUMPUS
-- HUNT THE WUMPUS
-- BY GREGORY YOB
-- BY GREGORY YOB
Line 4,455: Line 4,455:
end if
end if
end move
end move
</syntaxhighlight>
</lang>


=={{Header|Shale}}==
=={{Header|Shale}}==
<lang shale>
<syntaxhighlight lang="shale">
#!/usr/local/bin/shale
#!/usr/local/bin/shale


Line 4,914: Line 4,914:
init()
init()
toplevel()
toplevel()
</syntaxhighlight>
</lang>


=={{header|Swift}}==
=={{header|Swift}}==
{{trans|Go}}
{{trans|Go}}
<lang swift>
<syntaxhighlight lang="swift">
import Foundation
import Foundation


Line 5,133: Line 5,133:
game()
game()


</syntaxhighlight>
</lang>


=={{header|Wren}}==
=={{header|Wren}}==
Line 5,140: Line 5,140:
{{libheader|Wren-ioutil}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
{{libheader|Wren-str}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "/fmt" for Fmt
import "/fmt" for Fmt
import "/ioutil" for Input
import "/ioutil" for Input
Line 5,251: Line 5,251:
}
}
}
}
}</lang>
}</syntaxhighlight>