Maze generation: Difference between revisions
→{{header|Haskell}}: Specified imports, applied hlint hindent, simplified a Boolean chain with 'bool'
(Added Fōrmulæ) |
(→{{header|Haskell}}: Specified imports, applied hlint hindent, simplified a Boolean chain with 'bool') |
||
Line 3,504:
{-# LANGUAGE TypeFamilies #-}
import Control.Monad▼
import Control.Monad.ST▼
import Data.Array▼
import Data.Array.ST
(STArray, freeze, newArray, readArray, writeArray)
import Data.STRef (STRef, newSTRef, readSTRef, writeSTRef)
import System.Random (Random(..), getStdGen, StdGen)
▲import Control.Monad (forM_, unless)
▲import Control.Monad.ST (ST, stToIO)
import Data.Array (Array, (!), bounds)
rand
rand :: Random a => (a, a) -> STRef s StdGen -> ST s a▼
:: Random a
rand range gen = do
data Maze = Maze
{ rightWalls, belowWalls :: Array (Int, Int) Bool
}
maze :: Int -> Int -> StdGen -> ST s Maze
maze width height gen = do
Maze <$>
where
i <-
forM_ (ns !! i : take i ns ++ drop
\there
seen <- readArray
unless seen $
visit gen visited
where
(min x1 x2, min y1 y2)▼
removeWall (x1, y1) (x2, y2)
bool [(x, y - 1)] [] (
maxX = width - 1
maxY = height - 1
newArray ((0, 0), (maxX, maxY))
printMaze :: Maze -> IO ()
printMaze (Maze rWalls bWalls) = do
\y ->
putStr "|"
forM_ [0 .. maxX] $ \x -> do▼
forM_ [0 .. maxX]
\x -> do
putStrLn "+"
where
maxX = fst (snd $ bounds rWalls)
maxY = snd (snd $ bounds rWalls)
main :: IO ()
main = getStdGen >>= stToIO . maze 11 8 >>= printMaze</lang>
{{out|Sample output}}
|