如何在相邻色块之间传播颜色?
How do I spread a color between neighbouring patches?
我们正在用 Netlogo 设计一个教室,如下所示:
Classroom
人代表老师在教室里走来走去,灰色块代表空座位,绿色块代表学生集中的座位,红色块代表学生不专心的座位。我们想 'spread' 将注意力不集中作为 'disease' 到邻近的 GREEN 补丁。我们发现一些代码行几乎可以达到我们想要的效果:
ask patches with [pcolor = green] [set pcolor [pcolor] of one-of neighbors4]
但这最终会使所有补丁变灰,所以我们尝试将其更改为:
ask patches with [pcolor = green] [set pcolor [pcolor] of one-of neighbors4 with [pcolor = red]]
这一行出现以下错误:'OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.'
你们知道如何解决这个问题吗?
出现您遇到的错误是因为您告诉绿色补丁将它们的颜色更改为红色邻居之一,但并非所有绿色补丁都必须有红色邻居。在这种情况下,当您告诉绿色补丁 "change your color to one of your neighbors that is red" 绿色补丁消失时,"well, among my neighbors there is nobody
with that color." 然后返回特殊代理集 nobody
,并且 nobody
没有原始绿色的颜色补丁访问!
我想你可能会通过另一种方式更容易地做到这一点——也就是说,让红色斑块成为扩散的斑块。例如,使用此示例设置:
to setup
ca
resize-world 0 25 0 25
ask patches [
set pcolor green
]
ask n-of 5 patches [ set pcolor red ]
reset-ticks
end
你的世界里有一群专注的学生和5个捣蛋鬼。现在,如果你让你的麻烦制造者检查他们是否有任何可能被破坏的相邻补丁,你可能会以更快的速度向外扩散注意力不集中:
to colorswap
ask patches with [ pcolor = red ] [
; See if there is any possible neighbor patch
; to whom I can spread my lack of concentration
let target one-of neighbors4 with [ pcolor = green ]
; if the target exists, have them change their color
if target != nobody [
ask target [
set pcolor red
]
]
]
end
我们正在用 Netlogo 设计一个教室,如下所示:
Classroom
人代表老师在教室里走来走去,灰色块代表空座位,绿色块代表学生集中的座位,红色块代表学生不专心的座位。我们想 'spread' 将注意力不集中作为 'disease' 到邻近的 GREEN 补丁。我们发现一些代码行几乎可以达到我们想要的效果:
ask patches with [pcolor = green] [set pcolor [pcolor] of one-of neighbors4]
但这最终会使所有补丁变灰,所以我们尝试将其更改为:
ask patches with [pcolor = green] [set pcolor [pcolor] of one-of neighbors4 with [pcolor = red]]
这一行出现以下错误:'OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.'
你们知道如何解决这个问题吗?
出现您遇到的错误是因为您告诉绿色补丁将它们的颜色更改为红色邻居之一,但并非所有绿色补丁都必须有红色邻居。在这种情况下,当您告诉绿色补丁 "change your color to one of your neighbors that is red" 绿色补丁消失时,"well, among my neighbors there is nobody
with that color." 然后返回特殊代理集 nobody
,并且 nobody
没有原始绿色的颜色补丁访问!
我想你可能会通过另一种方式更容易地做到这一点——也就是说,让红色斑块成为扩散的斑块。例如,使用此示例设置:
to setup
ca
resize-world 0 25 0 25
ask patches [
set pcolor green
]
ask n-of 5 patches [ set pcolor red ]
reset-ticks
end
你的世界里有一群专注的学生和5个捣蛋鬼。现在,如果你让你的麻烦制造者检查他们是否有任何可能被破坏的相邻补丁,你可能会以更快的速度向外扩散注意力不集中:
to colorswap
ask patches with [ pcolor = red ] [
; See if there is any possible neighbor patch
; to whom I can spread my lack of concentration
let target one-of neighbors4 with [ pcolor = green ]
; if the target exists, have them change their color
if target != nobody [
ask target [
set pcolor red
]
]
]
end