Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- type State =
- | Dead = 0
- | Alive = 1
- let size = 10
- let mutable grid = Array2D.create size size State.Dead
- let spawn (lst : (int * int) list) = for (x, y) in lst do grid.[y, x] <- State.Alive
- let printGrid () =
- let row = (Array2D.length1 grid) - 1
- let col = (Array2D.length2 grid) - 1
- for y in [0..row] do
- for x in [0..col] do
- Console.Write((int) grid.[y, x])
- Console.WriteLine("\n")
- let isValidCell (x, y) = (x > 0 && y > 0 && x < size && y < size)
- let liveNeighbors (x, y) =
- [for i in [-1 .. 1] do
- for j in [-1 .. 1] do yield (x + i, y + j)]
- |> List.filter (fun (i, j) -> (i > 0 && j > 0 && i < size && j < size))
- |> List.filter (fun p -> p <> (x, y))
- |> List.map (fun (x, y) -> grid.[y, x])
- |> List.filter (fun x -> x = State.Alive)
- let countElems l =
- let rec loop l acc =
- match l with
- |h::t -> loop t (acc+1)
- |[] -> acc
- loop l 0
- let tick () =
- let tempGrid = Array2D.create size size State.Dead
- let row = (Array2D.length1 grid) - 1
- let col = (Array2D.length2 grid) - 1
- for y in [0..row] do
- for x in [0..col] do
- let count = countElems(liveNeighbors (y, x))
- match tempGrid.[y, x] with
- | State.Dead -> if count = 3 then tempGrid.[y, x] <- State.Alive
- | State.Alive -> if count <> 2 && count <> 3 then tempGrid.[y, x] <- State.Dead
- | _ -> printfn "LOLDONGS"
- grid <- tempGrid
- let play max =
- let rec loop max acc =
- printGrid()
- tick()
- match acc with
- | max -> printfn "LOLDONE"
- | _ -> loop max (acc + 1)
- loop max 0
- spawn [(2, 1); (2, 2); (2, 3); (1, 3); (0, 2)]
- play(10)
- Console.ReadLine() |> ignore
Add Comment
Please, Sign In to add comment