|
| 1 | +module LightTable.FSharp.Examples.GameOfLife |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Drawing |
| 5 | +open System.IO |
| 6 | + |
| 7 | +type Cell = |
| 8 | + | Alive |
| 9 | + | Dead |
| 10 | + |
| 11 | +let step cell neighbours = |
| 12 | + let alives = |
| 13 | + neighbours |> List.sumBy (function |
| 14 | + | Alive -> 1 |
| 15 | + | Dead -> 0) |
| 16 | + match (cell, alives) with |
| 17 | + | Alive, n when n < 2 -> Dead // by under-population |
| 18 | + | Alive, n when n > 3 -> Dead // by overcrowding |
| 19 | + | Alive, 2 | Alive, 3 -> Alive // lives on |
| 20 | + | Dead, 3 -> Alive // by reproduction |
| 21 | + | Dead, _ -> Dead |
| 22 | + |
| 23 | +let neighbours getAt (x, y) = |
| 24 | + [ for i in [ x - 1..x + 1 ] do |
| 25 | + for j in [ y - 1..y + 1 ] -> (i, j) ] |
| 26 | + |> List.filter (fun a -> a <> (x, y)) //not itself |
| 27 | + |> List.map (fun coord -> (coord, getAt <| coord)) |
| 28 | + |
| 29 | +/// World is a Map (x,y) -> Cell with all Alive cell, default is Dead cell |
| 30 | +type World = Map<int * int, Cell> |
| 31 | + |
| 32 | +let emptyWorld : World = Map.empty |
| 33 | + |
| 34 | +let getAt coord (world : World) = |
| 35 | + match world |> Map.tryFind coord with |
| 36 | + | Some x -> x |
| 37 | + | None -> Dead |
| 38 | + |
| 39 | +let setAt coord v (world : World) = |
| 40 | + match v with |
| 41 | + | Alive -> world |> Map.add coord v |
| 42 | + | Dead -> world |> Map.remove coord |
| 43 | + |
| 44 | +let tick world = |
| 45 | + let at coord = world |> getAt coord |
| 46 | + |
| 47 | + let stepCell (coord, cell) = |
| 48 | + let next = |
| 49 | + neighbours at coord |
| 50 | + |> List.map snd |
| 51 | + |> step (at coord) |
| 52 | + (coord, next) |
| 53 | + |
| 54 | + let cells = (world |> Map.toSeq) |
| 55 | + |
| 56 | + let potential = |
| 57 | + cells |
| 58 | + |> Seq.map (fun (coord, _) -> neighbours at coord) |
| 59 | + |> Seq.concat |
| 60 | + |> Seq.append cells |
| 61 | + |> Seq.distinct |
| 62 | + potential |
| 63 | + |> Seq.map stepCell |
| 64 | + |> Seq.fold (fun w (coord, cell) -> w |> setAt coord cell) emptyWorld |
| 65 | + |
| 66 | +let evolution initial = |
| 67 | + let some x = if x = emptyWorld then None else Some(x, x) |
| 68 | + initial |> Seq.unfold (tick >> some) |
| 69 | + |
| 70 | +// example |
| 71 | +let glider = |
| 72 | + [ (2, 0), Alive |
| 73 | + (0, 1), Alive |
| 74 | + (2, 1), Alive |
| 75 | + (1, 2), Alive |
| 76 | + (2, 2), Alive ] |
| 77 | + |
| 78 | +let add pattern (x, y) world = |
| 79 | + let offset (i, j) = x + i, y + j |
| 80 | + pattern |
| 81 | + |> List.fold (fun w (coord, cell) -> w |> setAt (offset coord) cell) world |
| 82 | + |
| 83 | +// draw |
| 84 | +let rec drawWorld (area : Rectangle) cellSize world = |
| 85 | + let cells = |
| 86 | + seq { |
| 87 | + for i in [ area.X..area.Right ] do |
| 88 | + for j in [ area.Y..area.Bottom ] -> (i, j) |
| 89 | + } |
| 90 | + |
| 91 | + let aliveShape (x, y) = |
| 92 | + Rectangle(x * cellSize, y * cellSize, cellSize, cellSize) |
| 93 | + cells |
| 94 | + |> Seq.map (fun coord -> (coord, world |> getAt coord)) |
| 95 | + |> Seq.filter (fun (coord, cell) -> cell = Alive) |
| 96 | + |> Seq.fold (fun xs (coord, cell) -> aliveShape coord :: xs) [] |
| 97 | + |> drawImg (area.Right * cellSize) (area.Bottom * cellSize) |
| 98 | + |
| 99 | +and drawImg (width : int) height (rects : Rectangle list) = |
| 100 | + let bmp = new Bitmap(width, height) |
| 101 | + use g = Graphics.FromImage(bmp) |
| 102 | + match rects with |
| 103 | + | [] -> () |
| 104 | + | r -> g.FillRectangles(System.Drawing.Brushes.Red, r |> List.toArray) |
| 105 | + bmp :> Image |
| 106 | + |
| 107 | +/// plaintext format ( http://www.conwaylife.com/wiki/Plaintext ) |
| 108 | +let decodePlaintext text = |
| 109 | + let data = text |> List.filter (fun (l: string) -> not <| l.StartsWith("!")) |
| 110 | + let cell s = if s = "O" then Alive else Dead |
| 111 | + let row y d = d |> List.mapi (fun x c -> ((x, y), cell c)) |
| 112 | + data |
| 113 | + |> List.map (fun line -> line.ToCharArray() |> Seq.map (fun x -> x.ToString()) |> Seq.toList) |
| 114 | + |> List.mapi (fun y t -> t |> row y) |
| 115 | + |> List.concat |
| 116 | + |> List.fold (fun w (coord, c) -> w |> setAt coord c) emptyWorld |
| 117 | + |
| 118 | +let downloadPattern name = |
| 119 | + use client = new System.Net.WebClient() |
| 120 | + let url = sprintf "http://www.conwaylife.com/patterns/%s.cells" name |
| 121 | + client.DownloadString(Uri(url)) |
| 122 | + |> (fun x -> x.Split('\n') |> Seq.toList) |
| 123 | + |> decodePlaintext |
| 124 | + |
| 125 | +let glider2 () = downloadPattern <| "glider" |
| 126 | +let baker () = downloadPattern <| "baker" |
| 127 | +let gosperglidergun () = downloadPattern <| "gosperglidergun" |
| 128 | + |
| 129 | +// let's try |
| 130 | +let run times world = |
| 131 | + world |
| 132 | + |> evolution |
| 133 | + |> Seq.zip [ 0..times ] |
| 134 | + |> Seq.map snd |
| 135 | + |
| 136 | +let sample = emptyWorld |> add glider (0, 0) |
| 137 | + |
| 138 | +open LightTable.FSharp.Inspector.Helpers |
| 139 | + |
| 140 | +let gif initial = |
| 141 | + [ initial ] |
| 142 | + |> Seq.append (initial |> run 60) |
| 143 | + |> Seq.map (drawWorld (Rectangle(0, 0, 40, 40)) 25) |
| 144 | + |> Gif.create (TimeSpan.FromSeconds(0.2)) |
| 145 | + |
| 146 | + |
| 147 | +gosperglidergun () |> gif |
0 commit comments