From acdee334b68caf38161cf536838f18f0b3b6267a Mon Sep 17 00:00:00 2001 From: Seng-Jik <853974536@qq.com> Date: Fri, 12 Jun 2020 12:58:22 +0800 Subject: [PATCH] =?UTF-8?q?add=20F#=E7=BC=96=E7=A8=8B=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=9A=84=E7=94=9F=E5=91=BD=E6=B8=B8=E6=88=8F?= =?UTF-8?q?.fs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...37\345\221\275\346\270\270\346\210\217.fs" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "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" 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 0000000..5f186a1 --- /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 -- Gitee