Parcourir la source

added and applied rules

Brandon Wong il y a 7 ans
Parent
commit
dd74113984
1 fichiers modifiés avec 41 ajouts et 48 suppressions
  1. 41 48
      Main.elm

+ 41 - 48
Main.elm

@@ -14,11 +14,13 @@ type Cell
     | Dead
 
 type alias Board = Array.Array (Array.Array Cell)
+type alias Rule = Int -> Cell -> Cell
 
 type alias Model =
     { width : Int
     , height : Int
     , board : Board
+    , rule : Rule
     }
 
 initModel : Int -> Int -> Model
@@ -26,7 +28,20 @@ initModel w h =
     { width = w
     , height = h
     --, 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 ->
             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
-        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
+        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
         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
         prevRow =
             Maybe.withDefault Array.empty <| Array.get (rn - 1) board
@@ -137,25 +137,18 @@ replaceRow board rn row =
         Array.indexedMap
         (\cn cell ->
             --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
 
-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 =
     case msg of
         Step ->
-            ( { model | board = step model.board }, Cmd.none )
+            ( { model | board = step model.rule model.board }, Cmd.none )
         _ ->
             ( model, Cmd.none )