|
@@ -14,11 +14,13 @@ type Cell
|
|
| Dead
|
|
| Dead
|
|
|
|
|
|
type alias Board = Array.Array (Array.Array Cell)
|
|
type alias Board = Array.Array (Array.Array Cell)
|
|
|
|
+type alias Rule = Int -> Cell -> Cell
|
|
|
|
|
|
type alias Model =
|
|
type alias Model =
|
|
{ width : Int
|
|
{ width : Int
|
|
, height : Int
|
|
, height : Int
|
|
, board : Board
|
|
, board : Board
|
|
|
|
+ , rule : Rule
|
|
}
|
|
}
|
|
|
|
|
|
initModel : Int -> Int -> Model
|
|
initModel : Int -> Int -> Model
|
|
@@ -26,7 +28,20 @@ initModel w h =
|
|
{ width = w
|
|
{ width = w
|
|
, height = h
|
|
, height = h
|
|
--, board = Array.repeat h <| Array.repeat w Dead
|
|
--, board = Array.repeat h <| Array.repeat w Dead
|
|
- , board = modify 3 2 Alive <| Array.repeat h <| Array.repeat w Dead
|
|
|
|
|
|
+ --, board = modify 3 2 Alive <| Array.repeat h <| Array.repeat w Dead
|
|
|
|
+ , board = modify 4 4 Alive <| modify 4 3 Alive <| modify 3 4 Alive <| modify 3 2 Alive <| Array.repeat h <| Array.repeat w Dead
|
|
|
|
+ , rule = (\livingNeighbours cell ->
|
|
|
|
+ case cell of
|
|
|
|
+ Alive ->
|
|
|
|
+ if livingNeighbours < 2 || livingNeighbours > 3 then
|
|
|
|
+ Dead
|
|
|
|
+ else
|
|
|
|
+ Alive
|
|
|
|
+ _ ->
|
|
|
|
+ if livingNeighbours == 3 then
|
|
|
|
+ Alive
|
|
|
|
+ else
|
|
|
|
+ Dead)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -88,46 +103,31 @@ modify x y newState board =
|
|
Nothing ->
|
|
Nothing ->
|
|
board
|
|
board
|
|
|
|
|
|
-surroundings : Array.Array Cell -> Array.Array Cell -> Int -> Array.Array Cell -> Int
|
|
|
|
-surroundings prevRow nextRow ind row =
|
|
|
|
|
|
+valueAt : Int -> Int -> Board -> Cell
|
|
|
|
+valueAt r c board =
|
|
|
|
+ Maybe.withDefault Dead <| Array.get c <| Maybe.withDefault Array.empty (Array.get r board)
|
|
|
|
+
|
|
|
|
+surroundings : Int -> Int -> Board -> Int
|
|
|
|
+surroundings r c board =
|
|
let
|
|
let
|
|
- ul =
|
|
|
|
- case Array.get (ind - 1) prevRow of
|
|
|
|
- Just Alive -> 1
|
|
|
|
- _ -> 0
|
|
|
|
- uc =
|
|
|
|
- case Array.get ind prevRow of
|
|
|
|
- Just Alive -> 1
|
|
|
|
- _ -> 0
|
|
|
|
- ur =
|
|
|
|
- case Array.get (ind + 1) prevRow of
|
|
|
|
- Just Alive -> 1
|
|
|
|
- _ -> 0
|
|
|
|
- ml =
|
|
|
|
- case Array.get (ind - 1) row of
|
|
|
|
- Just Alive -> 1
|
|
|
|
- _ -> 0
|
|
|
|
- mr =
|
|
|
|
- case Array.get (ind + 1) row of
|
|
|
|
- Just Alive -> 1
|
|
|
|
- _ -> 0
|
|
|
|
- ll =
|
|
|
|
- case Array.get (ind - 1) nextRow of
|
|
|
|
- Just Alive -> 1
|
|
|
|
- _ -> 0
|
|
|
|
- lc =
|
|
|
|
- case Array.get ind nextRow of
|
|
|
|
- Just Alive -> 1
|
|
|
|
- _ -> 0
|
|
|
|
- lr =
|
|
|
|
- case Array.get (ind + 1) nextRow of
|
|
|
|
- Just Alive -> 1
|
|
|
|
|
|
+ living cell =
|
|
|
|
+ case cell of
|
|
|
|
+ Alive -> 1
|
|
_ -> 0
|
|
_ -> 0
|
|
|
|
+ ul = living <| valueAt (r - 1) (c - 1) board
|
|
|
|
+ uc = living <| valueAt (r - 1) c board
|
|
|
|
+ ur = living <| valueAt (r - 1) (c + 1) board
|
|
|
|
+ ml = living <| valueAt r (c - 1) board
|
|
|
|
+ mr = living <| valueAt r (c + 1) board
|
|
|
|
+ ll = living <| valueAt (r + 1) (c - 1) board
|
|
|
|
+ lc = living <| valueAt (r + 1) c board
|
|
|
|
+ lr = living <| valueAt (r + 1) (c + 1) board
|
|
in
|
|
in
|
|
ul + uc + ur + ml + mr + ll + lc + lr
|
|
ul + uc + ur + ml + mr + ll + lc + lr
|
|
|
|
|
|
-replaceRow : Board -> Int -> Array.Array Cell -> Array.Array Cell
|
|
|
|
-replaceRow board rn row =
|
|
|
|
|
|
+
|
|
|
|
+replaceRow : Rule -> Board -> Int -> Array.Array Cell -> Array.Array Cell
|
|
|
|
+replaceRow rule board rn row =
|
|
let
|
|
let
|
|
prevRow =
|
|
prevRow =
|
|
Maybe.withDefault Array.empty <| Array.get (rn - 1) board
|
|
Maybe.withDefault Array.empty <| Array.get (rn - 1) board
|
|
@@ -137,25 +137,18 @@ replaceRow board rn row =
|
|
Array.indexedMap
|
|
Array.indexedMap
|
|
(\cn cell ->
|
|
(\cn cell ->
|
|
--TODO: insert rules
|
|
--TODO: insert rules
|
|
- case cell of
|
|
|
|
- Dead ->
|
|
|
|
- if surroundings prevRow nextRow cn row > 0 then
|
|
|
|
- Alive
|
|
|
|
- else
|
|
|
|
- Dead
|
|
|
|
- Alive ->
|
|
|
|
- Dead
|
|
|
|
|
|
+ rule (surroundings rn cn board) cell
|
|
) row
|
|
) row
|
|
|
|
|
|
-step : Board -> Board
|
|
|
|
-step board =
|
|
|
|
- Array.indexedMap (replaceRow board) board
|
|
|
|
|
|
+step : Rule -> Board -> Board
|
|
|
|
+step rule board =
|
|
|
|
+ Array.indexedMap (replaceRow rule board) board
|
|
|
|
|
|
update : Msg -> Model -> (Model, Cmd Msg)
|
|
update : Msg -> Model -> (Model, Cmd Msg)
|
|
update msg model =
|
|
update msg model =
|
|
case msg of
|
|
case msg of
|
|
Step ->
|
|
Step ->
|
|
- ( { model | board = step model.board }, Cmd.none )
|
|
|
|
|
|
+ ( { model | board = step model.rule model.board }, Cmd.none )
|
|
_ ->
|
|
_ ->
|
|
( model, Cmd.none )
|
|
( model, Cmd.none )
|
|
|
|
|