我希望我的代码可以根据滑块中给出的海龟数量进行更多压缩,我希望将其重点放在原点上。如果这是我的代码
to solid
set color blue
set xcor random sqrt number-of-particles - number-of-particles / 2
set ycor random sqrt number-of-particles - number-of-particles / 2
ifelse any? patches with [pcolor = black and count turtles-here = 0]
[move-to one-of patches with [pcolor = black and count turtles-here = 0]]
[die]
end
我将如何包含它以便乌龟移动到最接近原点的空补丁?到目前为止我已经
了if number-of-particles < volume * volume
[move-to one-of patches with [
答案 0 :(得分:1)
假设&#34; origin&#34;,你的意思是patch 0 0
,这是一种方法:
to move-near-origin
let free-patches patches with [pcolor = black and not any? turtles-here]
ifelse any? free-patches [
move-to min-one-of free-patches [ distance patch 0 0 ]
]
[ die ]
end
两个关键部分是min-one-of
和distance
。
另请注意,我已将count turtles-here = 0
替换为not any? turtles-here
,这基本上做同样的事情,但更具可读性。我还在patches with [pcolor = black and not any? turtles-here]
变量中存储了free-patches
,因此您不必重复自己并测试该条件的所有补丁两次。