|
@@ -6,6 +6,7 @@ import Html.Events exposing (..)
|
|
import Html.Attributes exposing (id, class, classList, style)
|
|
import Html.Attributes exposing (id, class, classList, style)
|
|
|
|
|
|
import Array
|
|
import Array
|
|
|
|
+import Random
|
|
import Time exposing (Time, second)
|
|
import Time exposing (Time, second)
|
|
|
|
|
|
|
|
|
|
@@ -17,19 +18,14 @@ type alias Board = Array.Array (Array.Array Cell)
|
|
type alias Rule = Int -> Cell -> Cell
|
|
type alias Rule = Int -> Cell -> Cell
|
|
|
|
|
|
type alias Model =
|
|
type alias Model =
|
|
- { width : Int
|
|
+ { board : Board
|
|
- , height : Int
|
|
|
|
- , board : Board
|
|
|
|
, rule : Rule
|
|
, rule : Rule
|
|
}
|
|
}
|
|
|
|
|
|
-initModel : Int -> Int -> Model
|
|
+initModel : Model
|
|
-initModel w h =
|
|
+initModel =
|
|
- { width = w
|
|
+ { board = Array.empty
|
|
- , height = h
|
|
+ --, board = modify 4 4 Alive <| modify 5 5 Alive <| modify 6 5 Alive <| modify 6 4 Alive <| modify 6 3 Alive <| 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 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 ->
|
|
, rule = (\livingNeighbours cell ->
|
|
case cell of
|
|
case cell of
|
|
Alive ->
|
|
Alive ->
|
|
@@ -48,6 +44,7 @@ initModel w h =
|
|
type Msg
|
|
type Msg
|
|
= NoOp
|
|
= NoOp
|
|
| Step
|
|
| Step
|
|
|
|
+ | SetBoard Board
|
|
|
|
|
|
|
|
|
|
viewRow row =
|
|
viewRow row =
|
|
@@ -58,19 +55,15 @@ viewRow row =
|
|
<|
|
|
<|
|
|
Array.map (\c ->
|
|
Array.map (\c ->
|
|
let
|
|
let
|
|
- colour =
|
|
+ cellClass =
|
|
case c of
|
|
case c of
|
|
- Alive ->
|
|
+ Alive -> "alive"
|
|
- "yellow"
|
|
+ Dead -> "dead"
|
|
- Dead ->
|
|
|
|
- "grey"
|
|
|
|
in
|
|
in
|
|
td
|
|
td
|
|
- [ style
|
|
+ [ class ("cell " ++ cellClass)
|
|
- [ ("background-color", colour)
|
|
|
|
- ]
|
|
|
|
]
|
|
]
|
|
- [text (toString c)]
|
|
+ []
|
|
) row
|
|
) row
|
|
|
|
|
|
viewBoard : Board -> Html Msg
|
|
viewBoard : Board -> Html Msg
|
|
@@ -87,8 +80,7 @@ view model =
|
|
div
|
|
div
|
|
[ onClick Step
|
|
[ onClick Step
|
|
]
|
|
]
|
|
- [ text "cellular-elm"
|
|
+ [ viewBoard model.board
|
|
- , viewBoard model.board
|
|
|
|
]
|
|
]
|
|
|
|
|
|
modify : Int -> Int -> Cell -> Board -> Board
|
|
modify : Int -> Int -> Cell -> Board -> Board
|
|
@@ -136,7 +128,6 @@ replaceRow rule board rn row =
|
|
in
|
|
in
|
|
Array.indexedMap
|
|
Array.indexedMap
|
|
(\cn cell ->
|
|
(\cn cell ->
|
|
- --TODO: insert rules
|
|
|
|
rule (surroundings rn cn board) cell
|
|
rule (surroundings rn cn board) cell
|
|
) row
|
|
) row
|
|
|
|
|
|
@@ -149,18 +140,24 @@ update msg model =
|
|
case msg of
|
|
case msg of
|
|
Step ->
|
|
Step ->
|
|
( { model | board = step model.rule model.board }, Cmd.none )
|
|
( { model | board = step model.rule model.board }, Cmd.none )
|
|
|
|
+ SetBoard board ->
|
|
|
|
+ ( { model | board = board }, Cmd.none )
|
|
_ ->
|
|
_ ->
|
|
( model, Cmd.none )
|
|
( model, Cmd.none )
|
|
|
|
|
|
|
|
+generateRandom : Int -> Int -> Random.Generator Board
|
|
|
|
+generateRandom nr nc =
|
|
|
|
+ Random.map Array.fromList <| Random.list nr <| Random.map Array.fromList <| Random.list nc <| Random.map (\n -> if n == 1 then Alive else Dead) (Random.int 1 4)
|
|
|
|
+
|
|
subscriptions : Model -> Sub Msg
|
|
subscriptions : Model -> Sub Msg
|
|
subscriptions model =
|
|
subscriptions model =
|
|
Sub.batch
|
|
Sub.batch
|
|
- [ Time.every second (\_ -> Step)
|
|
+ [ Time.every (second*3) (\_ -> Step)
|
|
]
|
|
]
|
|
|
|
|
|
main =
|
|
main =
|
|
Html.program
|
|
Html.program
|
|
- { init = ((initModel 20 10), Cmd.none)
|
|
+ { init = (initModel, Random.generate SetBoard (generateRandom 50 100))
|
|
, view = view
|
|
, view = view
|
|
, update = update
|
|
, update = update
|
|
, subscriptions = subscriptions
|
|
, subscriptions = subscriptions
|