patches-own [living]
to setup
__clear-all-and-reset-ticks
ask patches[;if rannum < 0.2,a cell born,else nothing happend
ifelse random-float 1 < 0.2[
set pcolor black
]
[
set pcolor white
]
]
end
;B3S23 is the game_charactor
to go
;avoid rules breaking,step 1 to calc living neighbors
ask patches[;set the attr-val is its neighbors with diff color limit
set living count neighbors with [pcolor = black]
]
;step 2 to update every patches with the attr living
ask patches[
ifelse(pcolor = black)[
if living > 3 or living < 2[
set pcolor white
]
]
[
if (living = 3)[
set pcolor black
]
]
]
end
- patches-own [living]:为patches格子新增属性
- ifelse[...][...]:互斥事件判断语句
- set pcolor white:pcolor是patches的固有属性,set ... xxx是赋值语句
- count neighbors with [pcolor = black]:count...with是计数,neighbors是所有邻居patches,with中是筛选条件
- B3S23:表示当有三个活着的邻居时,此patches变为活patches;当有2~3个活邻居时,当前patches继续存活。