-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomGeneration.hs
More file actions
40 lines (31 loc) · 1.29 KB
/
RandomGeneration.hs
File metadata and controls
40 lines (31 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module RandomGeneration where
import Types
import System.Random (randomRIO)
randomElement :: [a] -> IO a
randomElement [] = fail "randomElement: empty list"
randomElement [x] = return x
randomElement xs = (xs !!) `fmap` randomRIO (0, length xs - 1)
-- implementations for GenerationBias.
-- the basic functionality is shifting the odds between randomly
-- choosing a horizontal or vertical neighbour of a given
-- maze cell; different odds result in the desired visual pattern.
randomBias :: GenerationBias -> (Int, Int) -> MazeIx -> [MazeIx] -> IO MazeIx
randomBias NoBias _ _ xs =
randomElement xs
randomBias CheckerBoard dims@(mazeW, mazeH) ix@(x, y) xs =
let
sqX = 2*x `div` mazeW
sqY = 2*y `div` mazeH
bias = bool Horizontal Vertical $
(odd sqY && even sqX) || (odd sqX && even sqY)
in randomBias bias dims ix xs
randomBias DiagonalSplit dims@(mazeW, mazeH) ix@(x, y) xs =
let bias = bool Vertical Horizontal $ x > (y * mazeW) `div` mazeH
in randomBias bias dims ix xs
randomBias bias _ (x, y) xs =
let biased = filter filterFunc xs
in randomElement $ concat $ xs : replicate 3 biased
where
filterFunc = case bias of
Vertical -> (== x) . fst
Horizontal -> (== y) . snd