我正在尝试编写一个Conway的生活游戏版本,而不是查看相邻的8个单元,而是查看相邻的24个单元。 (而不是围绕中心的1个正方形,而是2个。)
我遵循了一些建议,并建立了一个“ neighbors24”代理,该代理应查看活细胞中的周围细胞。
patches-own [
living? ;; indicates if the cell is living
live-neighbors ;; counts how many neighboring cells are alive
]
to setup-blank
clear-all
ask patches [ cell-death ]
reset-ticks
end
to setup-random
clear-all
ask patches
[ ifelse random-float 100.0 < initial-density
[ cell-birth ]
[ cell-death ] ]
reset-ticks
end
to cell-birth
set living? true
set pcolor fgcolor
end
to cell-death
set living? false
set pcolor bgcolor
end
to go
let neighbors24 patches with [abs pxcor <= 2 and abs pycor <= 2]
ask patches
[ set live-neighbors count neighbors24 with [living?] ]
ask patches
[ ifelse live-neighbors = 3
[ cell-birth ]
[ if live-neighbors != 2
[ cell-death ] ] ]
tick
end
to draw-cells
let erasing? [living?] of patch mouse-xcor mouse-ycor
while [mouse-down?]
[ ask patch mouse-xcor mouse-ycor
[ ifelse erasing?
[ cell-death ]
[ cell-birth ] ]
display ]
end
尽管代码可以正确编译,但其行为与我期望的完全不同。例如,如果我在24个邻域半径内放置3个活细胞,而不是细胞诞生,那么所有细胞都会死亡。
答案 0 :(得分:2)
我对您的go
过程进行了一些小调整,其中一些输入来自NetLogo模型库的Moore&Von-Naumann邻域示例。有关调整的更多详细信息,请检查以下代码中的注释。
to go
;; creates a list with patches of the surrounding 24 patches
;; with the caller included.
let neighbors24 [list pxcor pycor] of patches with [abs pxcor <= 2 and abs pycor <= 2]
;; uncomment the line below, if you don´t want to consider the caller
;; patch to adjust the neighbors24 set
;set neighbors24 remove [0 0] neighbors24
;; for illustration, shows the number of coordinates considered as neighbors
;show length neighbors24
;; for illustration, shows the patch coordinates of the neighbors24 set
;show neighbors24
ask patches [
;; each patch checks the the "living" neighbors at the given coordinates (relative to this agent).
;; Check documentation of "at-points"
set live-neighbors count patches at-points neighbors24 with [living? = true]
]
ask patches
[ ifelse live-neighbors = 3
[ cell-birth ]
[ if live-neighbors != 2
[ cell-death ] ] ]
tick
end
我没有广泛测试该代码,但是它的生命补丁的起始密度低,随机(20-30%)似乎还不错。请检查第一轮的示例屏幕截图,其密度为27%。
答案 1 :(得分:1)
所有细胞都在死亡,因为它们不是在计算自身周围的活细胞,而是在起源周围的活细胞。像这样编辑代码:
to go
let neighbors24 patches with [abs pxcor <= 2 and abs pycor <= 2]
type "Live patches near centre: " print count neighbors24 with [living?]
ask patches
...
我添加了一行,以打印出变量live-neighbors中的内容。
看看您如何计算邻居24。您正在使用坐标值。因此,始终是原点周围最多2个面片(位于0,0)的面片。正如我在上一个问题的评论中所述,您需要查看模型库中名为“ Moore&van Nuemann示例”的模型。只需搜索“摩尔”。上面的代码将使用补丁自身的坐标来使社区居中。
请注意,您还必须在ask patches
内找到邻居。