Last active
October 19, 2015 21:11
-
-
Save jwosty/3a45467532872480c3cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Cell = | On | Off | |
let (%) x m = ((x % m) + m) % m | |
/// Tells you how many neighbors are on | |
let nOnNeighbors (cells : _ [,]) (x, y) = | |
[for xOffset in -1..1 do | |
for yOffset in -1..1 do | |
if not ((xOffset = 0) && (yOffset = 0)) then | |
let x = (x + xOffset) % Array2D.length1 cells | |
let y = (y + yOffset) % Array2D.length2 cells | |
match cells.[x,y] with | On -> yield 1 | Off -> yield 0] | |
|> List.sum | |
let next gameState = | |
gameState |> Array2D.mapi (fun x y cellState -> | |
let n = nOnNeighbors gameState (x, y) | |
match cellState with | |
| On -> if n = 2 || n = 3 then On else Off | |
| Off -> if n = 3 then On else Off) | |
let display gameState = | |
for x in 0..(Array2D.length1 gameState - 1) do | |
for y in 0..(Array2D.length2 gameState - 1) do | |
match gameState.[x,y] with | |
| On -> printf "*" | |
| Off -> printf " " | |
printfn "" | |
let rec run gameState = | |
display gameState | |
System.Console.ReadKey () |> ignore | |
System.Console.Clear () | |
run (next gameState) | |
let initial = [[Off; Off; Off; Off; Off; Off; Off; Off; Off; Off; Off] | |
[Off; Off; Off; Off; Off; Off; Off; Off; Off; Off; Off] | |
[Off; Off; Off; Off; Off; Off; Off; Off; Off; Off; Off] | |
[Off; Off; Off; Off; Off; On; Off; Off; Off; Off; Off] | |
[Off; Off; Off; Off; Off; Off; On; Off; Off; Off; Off] | |
[Off; Off; Off; Off; On; On; On; Off; Off; Off; Off] | |
[Off; Off; Off; Off; Off; Off; Off; Off; Off; Off; Off] | |
[Off; Off; Off; Off; Off; Off; Off; Off; Off; Off; Off] | |
[Off; Off; Off; Off; Off; Off; Off; Off; Off; Off; Off] | |
[Off; Off; Off; Off; Off; Off; Off; Off; Off; Off; Off]] | |
|> array2D | |
[<EntryPoint>] | |
let main args = | |
run initial | |
module Graphics | |
open Microsoft.Xna.Framework | |
open Microsoft.Xna.Framework.Graphics | |
type GameWindow(game) as this = | |
inherit Game() | |
let mutable graphics = new GraphicsDeviceManager(this) | |
override this.Initialize () = | |
() | |
override this.Draw gameTime = | |
this.GraphicsDevice.Clear Color.OrangeRed | |
[<EntryPoint>] | |
let main args = | |
using (new GameWindow(Game.initial)) (fun game -> | |
game.Run ()) | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment