Skip to content

Instantly share code, notes, and snippets.

@mathiasverraes
Last active August 4, 2016 17:40
Show Gist options
  • Save mathiasverraes/c95e71b9c0219f9530c9b7f71a79d4c1 to your computer and use it in GitHub Desktop.
Save mathiasverraes/c95e71b9c0219f9530c9b7f71a79d4c1 to your computer and use it in GitHub Desktop.
initial PoC for a maze game in Haskell
module Lib where
import Data.List.Split
import System.Random
a4by4Maze :: Maze
a4by4Maze = Maze [
[(╝),(╦),(╝),(╣)],
[(╩),(╣),(╗),(╝)],
[(═),(╩),(║),(╝)],
[(╗),(║),(╦),(╔)]
]
data Maze = Maze [[Cell]]
instance Show Maze where
show (Maze []) = ""
show (Maze (row:rows)) = (show row) ++ ",\n" ++ (show $ Maze rows)
data Exit = Open | Closed
o = Open
x = Closed
data Exits = Exits
{ north :: Exit
, east :: Exit
, south :: Exit
, west :: Exit
}
data Cell = CellWE
| CellNS
| CellNE
| CellES
| CellSW
| CellNW
| CellESW
| CellNSW
| CellNEW
| CellNES
deriving(Eq, Enum, Bounded)
instance Show Cell where
show CellWE = "(═)"
show CellNS = "(║)"
show CellNE = "(╚)"
show CellES = "(╔)"
show CellSW = "(╗)"
show CellNW = "(╝)"
show CellESW = "(╦)"
show CellNSW = "(╣)"
show CellNEW = "(╩)"
show CellNES = "(╠)"
exits :: Cell -> Exits
exits CellWE = Exits x o x o
exits CellNS = Exits o x o x
exits CellNE = Exits o o x x
exits CellES = Exits x o o x
exits CellSW = Exits x x o o
exits CellNW = Exits o x x o
exits CellESW = Exits x o o o
exits CellNSW = Exits o x o o
exits CellNEW = Exits o o x o
exits CellNES = Exits o o o x
(═) = CellWE
(║) = CellNS
(╚) = CellNE
(╔) = CellES
(╗) = CellSW
(╝) = CellNW
(╦) = CellESW
(╣) = CellNSW
(╩) = CellNEW
(╠) = CellNES
instance Random Cell where
random g = case randomR (minBound::Cell, maxBound :: Cell) g of
(r, g') -> ( r, g')
randomR (a,b) g = case randomR (fromEnum a, fromEnum b) g of
(r, g') -> (toEnum r, g')
randomMaze :: RandomGen g => Int -> Int -> g -> Maze
randomMaze x y = (Maze . chunksOf x . take (x*y)) <$> randoms
@mathiasverraes
Copy link
Author

screen shot 2016-08-04 at 19 26 21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment