-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.elm
43 lines (28 loc) · 905 Bytes
/
grid.elm
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
41
42
43
module Grid exposing (Grid, Point, empty, get2d, neighbors, randomGrid, set2d)
import Array exposing (Array)
import Random exposing (Generator)
type alias Grid a =
Array (Array a)
type alias Point =
( Int, Int )
get2d : Point -> Grid a -> Maybe a
get2d ( x, y ) grid =
Array.get y grid |> Maybe.andThen (Array.get x)
set2d : Point -> a -> Grid a -> Grid a
set2d ( x, y ) value grid =
Array.get y grid
|> Maybe.map (\a -> (Array.set y << Array.set x value) a grid)
|> Maybe.withDefault grid
neighbors : Point -> List Point
neighbors ( x, y ) =
[ ( x, y - 1 ), ( x - 1, y ), ( x + 1, y ), ( x, y + 1 ) ]
randomGrid : Int -> Int -> Generator a -> Generator (Grid a)
randomGrid x y gen =
let
randArray n g =
Random.map Array.fromList <| Random.list n g
in
randArray x <| randArray y gen
empty : Grid a
empty =
Array.empty