5 Star 50 Fork 0

Gitee Community / 码力传递:晒代码赢奖品

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
F#编程语言实现的生命游戏.fs 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
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
1
https://gitee.com/gitee-community/gitee-7th-event-3.git
git@gitee.com:gitee-community/gitee-7th-event-3.git
gitee-community
gitee-7th-event-3
码力传递:晒代码赢奖品
master

搜索帮助