如果给定变量的阈值满足5个滴答声,我希望乌龟走到最靠近大多数乌龟的地方。
我的代码是:
to move
let count-tick 5
if var >= 9.5 [
set count-tick count-tick - 1
if count-tick = 0 [
ask turtle [
let nearest-group min-one-of (patches with [sum turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
ask turtle [ ;; once there do the following
set shape "star"
set color red
]
]
]
]
end
我遇到的问题是:a)我不确定如何说the patch with >= 3 turtles closest to you at the given range of 3
(上面尝试过的代码)和b)我怎么说once there, change your shape
。
答案 0 :(得分:2)
我想这就是你想要的:
to move
let count-tick 5
if var >= 9.5 [
set count-tick count-tick - 1
if count-tick = 0 [
let nearest-group min-one-of (patches with [count turtles >= 3] in-radius 3 ) [ distance myself ]
move-to nearest-group ;; go to the biggest crowd near you
set shape "star"
set color red
]
]
end
首先,您已经在调用此移动过程的过程的ask turtles
代码块内。因此,您不需要额外的ask turtles
。在NetLogo词典中查找ask
,对海龟进行迭代,依次运行每只海龟的所有代码。
第二,您需要count turtles
而不是sum turtles
,因为sum
是要累加值。
请注意,这没有错误检查,如果半径3范围内没有补丁,且补丁中至少有3只乌龟,则可能会出现问题。