-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gol.elm
331 lines (242 loc) · 6.05 KB
/
Gol.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
module Gol exposing (..)
import Html exposing (Html, button, div, text)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
import Svg
import Svg.Attributes exposing (width, height, viewBox)
import Svg.Events
import Random.Pcg exposing (Seed, initialSeed, step, int)
import Debug
main =
Html.beginnerProgram { model = model, view = view, update = update }
-- MODEL
type alias Model =
{ height : Int, width : Int, grid : FlatGrid }
type alias Grid =
List Row
type alias FlatGrid =
Row
type alias Neighbors =
Row
type alias Row =
List Cell
type alias Cell =
{ status : Status, position : Coordinate }
type alias RenderedCell =
Svg.Svg Msg
type alias RenderedRow =
List RenderedCell
type alias Coordinate =
( Int, Int )
type Community
= Reproduction
| Overpopulation
| Starvation
| LiveOn
| StayDead
seed : Seed
seed =
initialSeed 126781231212
model : Model
model =
let
h =
10
w =
10
grid =
svgGridBuilder h w seed []
|> flattenedSvgGrid
in
{ height = h, width = w, grid = grid }
stepGrid : Model -> Model
stepGrid model =
let
newGrid =
List.map (resetCellStatus model.grid) model.grid
in
{ model | grid = newGrid }
resetCellStatus : FlatGrid -> Cell -> Cell
resetCellStatus grid cell =
let
community =
nextGenerationStatus grid cell
newStatus =
case community of
Starvation ->
Dead
LiveOn ->
Alive
Overpopulation ->
Dead
Reproduction ->
Alive
StayDead ->
Dead
in
{ cell | status = newStatus }
nextGenerationStatus : FlatGrid -> Cell -> Community
nextGenerationStatus grid cell =
let
sizeOfHood =
neighbors cell grid
|> List.length
in
if cell.status == Alive then
case sizeOfHood of
0 ->
Starvation
1 ->
Starvation
2 ->
LiveOn
3 ->
LiveOn
_ ->
Overpopulation
else
case sizeOfHood of
3 ->
Reproduction
_ ->
StayDead
neighbors : Cell -> FlatGrid -> Neighbors
neighbors cell grid =
let
isANeighbor : Cell -> Bool
isANeighbor c =
positionOfNeighbors cell.position
|> List.member c.position
in
List.filter isANeighbor grid
|> List.filter (\c -> c.status == Alive)
positionOfNeighbors : Coordinate -> List Coordinate
positionOfNeighbors coord =
let
( x, y ) =
coord
in
[ ( x - 1, y - 1 )
, ( x, y - 1 )
, ( x + 1, y - 1 )
, ( x - 1, y )
, ( x + 1, y )
, ( x + 1, y + 1 )
, ( x, y + 1 )
, ( x - 1, y + 1 )
]
-- UPDATE
type Msg
= Click Coordinate
| Step
type Status
= Alive
| Dead
update : Msg -> Model -> Model
update msg model =
case msg of
Click position ->
{ model | grid = (toggleClickedCell position model.grid) }
Step ->
stepGrid model
toggleStatus : Status -> Status
toggleStatus status =
if status == Alive then
Dead
else
Alive
toggleClickedCell : Coordinate -> Row -> Row
toggleClickedCell position cells =
List.map
(\c ->
if c.position == position then
{ c | status = (toggleStatus c.status) }
else
c
)
cells
-- VIEW
renderCell : Cell -> Svg.Svg Msg
renderCell cell =
let
( x, y ) =
cell.position
in
cellColor cell
|> svgSquare x y
cellColor : Cell -> String
cellColor cell =
if cell.status == Alive then
"blue"
else
"white"
renderGrid : FlatGrid -> RenderedRow
renderGrid row =
List.map renderCell row
svgSquare : Int -> Int -> String -> RenderedCell
svgSquare x y color =
let
newX =
x
* 10
|> toString
newY =
y
* 10
|> toString
in
Svg.rect
[ width "10"
, height "10"
, Svg.Attributes.fill color
, Svg.Attributes.stroke "black"
, Svg.Attributes.strokeWidth "0.5"
, Svg.Attributes.x newX
, Svg.Attributes.y newY
, Svg.Events.onClick (Click ( x, y ))
]
[]
svgRowBuilder : Int -> Int -> Seed -> Row -> Row
svgRowBuilder length rowNumber seed row =
if length == 0 then
row
else
let
( randomNumber, seed1 ) =
step (int 0 1) seed
status =
if randomNumber == 0 then
Dead
else
Alive
newRow =
({ status = status, position = ( length, rowNumber ) }) :: row
in
svgRowBuilder (length - 1) rowNumber seed1 newRow
svgGridBuilder : Int -> Int -> Seed -> Grid -> Grid
svgGridBuilder height width seed grid =
if height == 0 then
grid
else
let
( _, seed1 ) =
step (int 0 1) seed
newGrid =
(svgRowBuilder width height seed1 []) :: grid
in
svgGridBuilder (height - 1) width seed1 newGrid
flattenedSvgGrid : Grid -> FlatGrid
flattenedSvgGrid grid =
List.foldr (++) [] grid
view : Model -> Html Msg
view model =
let
grid =
model.grid
|> renderGrid
in
div []
[ div []
[ Svg.svg [ width "500", height "500", viewBox "10 10 100 100" ] (grid) ]
, button [ onClick Step ] [ text "Step" ]
]