首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Conways邻域中的扩展邻域未在自定义代理集中读取

Conways邻域中的扩展邻域未在自定义代理集中读取
EN

Stack Overflow用户
提问于 2019-09-02 22:42:28
回答 2查看 46关注 0票数 1

我正在尝试编写Conway的生命游戏的一个版本,它不是查看相邻的8个细胞,而是查看相邻的24个细胞。(而不是中心周围的1个正方形,看起来是2)。

我听从了一些建议,设置了一个"neighbors24“代理,它应该查看活细胞中周围的细胞。

代码语言:javascript
复制
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个活细胞,而不是细胞诞生,所有细胞都会死亡。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-03 00:58:21

我对您的go过程做了一些小调整,其中一些输入来自NetLogo模型库中的Moore & Von-Naumann邻居示例。有关调整的更多详细信息,请查看以下代码中的注释。

代码语言:javascript
复制
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%。

票数 2
EN

Stack Overflow用户

发布于 2019-09-03 00:45:26

所有的细胞都在死亡,因为它们不是在计算自己周围的活细胞,而是在原点周围。像这样编辑你的代码:

代码语言:javascript
复制
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中的内容。

看看你是如何计算neighbors24的。您正在使用坐标值。因此,始终是原点周围最多2个面片(位于0,0处)。正如我在上一个问题的评论中所说的,您需要查看模型库中名为"Moore & van Nuemann example“的模型。只要搜索“摩尔”就行了。它的代码将使用补丁自己的坐标来居中居中。

请注意,您还必须在ask patches中获取邻居。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57758870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档