namespace WebSharperProject open IntelliFactory.WebSharper open IntelliFactory.WebSharper.Html module GameOfLife' = [] let width = 12 [] let height = 12 [] let getValue (board:int[][]) (x,y) = let x = (x + width) % width let y = (y + height) % height board.[y].[x] [] let computeNeighbours board (x,y) = let at = getValue board [ -1,-1;0,-1;1,-1; -1,0; 1,0; -1,1 ;0, 1;1,1; ] |> Seq.map (fun (dx,dy) -> x+dx,y+dy) |> Seq.map at |> Seq.sum [] let doGeneration board = board |> Array.mapi (fun y xs -> xs |> Array.mapi (fun x n -> match n, computeNeighbours board (x,y) with | _, 3 -> 1 | n, 2 -> n | _, _ -> 0 ) ) [] let toTable (board:int[][]) = let at = getValue board [ yield (Style "background-color:gray") for y = 0 to height-1 do yield TR [ for x = 0 to width-1 do let color = if at(x,y) = 0 then "white" else "black" yield TD [ Width "16"; Height "12"; Style ("background-color:"+color) ] :> INode ] :> INode ] |> Table [] let Main () = let board = Array.init height (fun y -> Array.init width (fun x -> 0) ) [7,7;8,7;9,7;9,6;8,5] |> List.iter (fun (x,y) -> board.[y].[x] <- 1) let current = ref board let div = Div [toTable !current] let rec loop () = async { current := doGeneration !current div.Clear () div.Append (toTable !current) do! Async.Sleep 1 do! loop () } loop () |> Async.Start div [] type GameOfLife() = inherit Web.Control() [] override this.Body = GameOfLife'.Main ()