-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.fsx
34 lines (29 loc) · 941 Bytes
/
GameOfLife.fsx
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
// https://en.wikipedia.org/wiki/Conway's_Game_of_Life
type Cell =
| Live
| Dead
let tryGetCell (x:int) (y:int) (state:Cell array array) : Cell option =
state
|> Seq.tryItem x
|> Option.map (Seq.tryItem y)
|> Option.flatten
let getLiveNeighboursCount (x:int) (y:int) (state:Cell array array) : int =
[ (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) ]
|> Seq.map (fun (x,y) -> tryGetCell x y state)
|> Seq.choose id
|> Seq.filter ((=) Live)
|> Seq.length
let cellTick (cell:Cell) (x:int) (y:int) (state:Cell array array) : Cell =
match cell, getLiveNeighboursCount x y state with
| Live, 2
| Live, 3
| Dead, 3 -> Live
| _ -> Dead
let tick (state:Cell array array) : Cell array array =
state
|> Array.mapi (fun x cells ->
cells
|> Array.mapi (fun y cell ->
cellTick cell x y state))