diff --git "a/F#\347\274\226\347\250\213\350\257\255\350\250\200\345\256\236\347\216\260\347\232\204\347\224\237\345\221\275\346\270\270\346\210\217.fs" "b/F#\347\274\226\347\250\213\350\257\255\350\250\200\345\256\236\347\216\260\347\232\204\347\224\237\345\221\275\346\270\270\346\210\217.fs" new file mode 100644 index 0000000000000000000000000000000000000000..5f186a19d02d9b5f3862ee82253f604fbb6e8052 --- /dev/null +++ "b/F#\347\274\226\347\250\213\350\257\255\350\250\200\345\256\236\347\216\260\347\232\204\347\224\237\345\221\275\346\270\270\346\210\217.fs" @@ -0,0 +1,61 @@ +type Cell = +| Live +| Died + +type Map = Cell[][] + +let myMap = + let width = 150 + let height = 58 + Array.init height (fun y -> + Array.init width (fun x -> + if x = width / 2 || y = height / 2 then Live + else Died)) + +let printMap = + Array.iter (fun line -> + line + |> Array.iter ( + function + | Live -> '*' + | Died -> ' ' + >> printf "%c") + printfn "") + +let clamp v small big = + if v < small then small + else if v > big then big + else v + +let slice first last array = + let s = clamp first 0 (-1 + Array.length array) + let e = clamp last 0 (-1 + Array.length array) + array.[s..e] + +let nextMap map = + map + |> Array.Parallel.mapi (fun y line -> + line + |> Array.mapi (fun x _ -> + let cur = map.[y].[x] + let neibours = + map + |> slice (y-1) (y+1) + |> Array.collect (slice (x-1) (x+1)) + |> Array.filter ((=) Live) + |> Array.length + |> function + | count when cur = Live -> count - 1 + | count -> count + match neibours with + | 3 -> Live + | 2 -> cur + | _ -> Died)) + +let rec playGame map = + System.Console.Clear () + printMap map + System.Threading.Thread.Sleep 20 + playGame (nextMap map) + +playGame myMap \ No newline at end of file