-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorld.elm
264 lines (223 loc) · 6.34 KB
/
World.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
module World exposing (..)
import Html exposing (Html, div, table, tr, td, text, button, h2)
import Html.Attributes exposing (style)
import Html.Events exposing (onClick)
import Html.App as App
import Window
import Task
import Random
import Debug
import Cell exposing (..)
-- MAIN
main =
App.program
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ ecosystem : Ecosystem
, windowSize : Window.Size
, generations : Int
, leftToRandomize : Int
}
type alias CellModel = Cell.Model
type alias Ecosystem = List (List CellModel)
rows =
10
columns =
10
init : ( Model, Cmd Msg )
init =
let
listOfCells = List.repeat columns ( List.repeat rows True )
newEcosystem = makeNewEcosystem listOfCells
size =
{ width = 800
, height = 800
}
model =
{ ecosystem = newEcosystem
, windowSize = size
, generations = 0
, leftToRandomize = 0
}
windowSizeCmd = getWindowSize
randomizeNewBoardCmd = randomizeNewBoard RandomizeEcosystem
cmds = Cmd.batch [ windowSizeCmd, randomizeNewBoardCmd ]
in
( model, cmds )
makeNewEcosystem : List ( List Bool ) -> Ecosystem
makeNewEcosystem ecosystem =
List.indexedMap (\ i row ->
row |> List.indexedMap (\j num ->
( Cell.init Cell.Alive ( i, j ) )
)
) ecosystem
getWindowSize : Cmd Msg
getWindowSize = Task.perform SizeUpdateFailure NewWindowSize Window.size
randomizeNewBoard : msg -> Cmd msg
randomizeNewBoard msg = Task.perform identity identity (Task.succeed msg)
neighbors : Coords -> List Coords
-- Neighbors are ordered counter clockwise from top left where i is row, j is col
neighbors ( i, j ) = [ ( i + 1, j - 1 )
, ( i + 1, j )
, ( i + 1, j + 1 )
, ( i, j + 1 )
, ( i - 1, j + 1 )
, ( i - 1, j )
, ( i - 1, j - 1 )
, ( i, j - 1 )
]
-- UPDATE
type Msg
= CellMessage Cell.Msg
| NewWindowSize Window.Size
| SizeUpdateFailure String
| NextGeneration
| NewEcosystem Ecosystem
| RandomizeEcosystem
| ChangeNextCellTo Bool
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
CellMessage cellMsg -> ( model, Cmd.none )
NewWindowSize newWindowSize -> ( { model | windowSize = newWindowSize }, Cmd.none )
SizeUpdateFailure _ -> ( model, Cmd.none )
NextGeneration -> ( makeNextGen model, Cmd.none )
NewEcosystem newEcosystem ->
( { model | ecosystem = newEcosystem, generations = 0 }
, Cmd.none
)
RandomizeEcosystem ->
( { model | leftToRandomize = rows * columns - 1 }
, Random.generate ChangeNextCellTo Random.bool
)
ChangeNextCellTo on ->
let
row =
model.leftToRandomize // rows
col =
model.leftToRandomize % columns
in
( { model
| ecosystem = ecoWithSpot row col on model.ecosystem
, leftToRandomize = ( model.leftToRandomize - 1 )
}
, if model.leftToRandomize > 0 then
Random.generate ChangeNextCellTo Random.bool
else
Cmd.none
)
makeNextGen : Model -> Model
makeNextGen ( { ecosystem, windowSize, generations } as model ) =
let
newGen =
List.map (\ row ->
List.map (\ cellModel ->
liveOrDie cellModel ecosystem
) row
) ecosystem
in
{ model
| ecosystem = newGen
}
boolToLifeStatus : Bool -> LifeStatus
boolToLifeStatus on =
if on then
Alive
else
Dead
ecoWithSpot : Int -> Int -> Bool -> Ecosystem -> Ecosystem
ecoWithSpot row col on ecosystem =
let
lifeStatus = boolToLifeStatus on
in
List.indexedMap
(\i r ->
if i == row then
(List.indexedMap
(\j cell ->
if j == col then
{ cell | lifeStatus = lifeStatus }
else cell) r
)
else
r
) ecosystem
liveOrDie : CellModel -> Ecosystem -> CellModel
liveOrDie ( { lifeStatus, coords } as model ) ecosystem =
let
cellNeighbors = neighbors coords
isNeighborAndAlive = (\ cellModel ->
if ( ( List.member ( .coords cellModel ) cellNeighbors )
&& ( cellModel.lifeStatus == Alive ) ) then
True
else
False
)
liveNeighbors =
List.length ( List.concatMap (\ row ->
List.filter isNeighborAndAlive row ) ecosystem
)
in
case lifeStatus of
Alive -> case liveNeighbors of
2 -> model
3 -> model
_ -> Cell.update GoToOtherSide model
Dead -> case liveNeighbors of
3 -> Cell.update GoToOtherSide model
_ -> model
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model = Window.resizes NewWindowSize
-- VIEW
view : Model -> Html Msg
view ( { ecosystem, windowSize, generations } as model ) =
let
-- define sizes
minSize = ( Basics.min windowSize.width windowSize.height )
|> toFloat
|> (\ x -> x * 0.9 )
mainWidth = toString minSize
tdSize = toString ( ( minSize / columns ) - 2 )
-- define styles
tdStyle =
style
[ ( "width", tdSize ++ "px" )
, ( "height", tdSize ++ "px" )
]
mainDivStyle = style
[ ( "width", mainWidth ++ "px" )
, ( "text-align", "center" )
, ( "margin", "20px auto" )
, ( "font-family", "Verdana, Geneva, sans-serif" )
]
buttonStyle = style
[ ( "font-family", "Verdana, Geneva, sans-serif" )
, ( "padding", "10px" )
, ( "margin", "0 20px" )
]
-- define html elements
rows = List.map (\ row ->
tr [] ( row
|> List.map (\ cellModel ->
td [ tdStyle ] [ ( renderCell cellModel ) ] )
)
) model.ecosystem
cellTable = table [ style [ ( "margin-bottom", "20px" ) ] ] rows
in
div [ mainDivStyle ]
[ h2 [] [ text "Conway's Game Of Life in Elm" ]
, cellTable
, button [ buttonStyle, onClick NextGeneration ] [ text "Next generation" ]
, button [ buttonStyle, onClick RandomizeEcosystem ] [ text "New random board" ]
]
renderCell : CellModel -> Html Msg
renderCell cellModel =
cellModel
|> Cell.view
|> App.map CellMessage