代码拉取完成,页面将自动刷新
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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。