将海龟放在没有发芽的包裹中
Setting turtles in parcel without sprout
我一直在寻找将每只乌龟放在一个包裹中的方法,但我不想在同一个包裹中放置两只乌龟。有人知道怎么做吗? (不使用新芽)。
假设你的补丁比海龟多,你只需要:
ask turtles [
move-to one-of patches with [ not any? turtles-here ]
]
有趣的旁注:
人们常常想把 other
粘在那里,例如:
; bad code:
ask turtles [
move-to one-of patches with [ not any? other turtles-here ]
]
但这会在补丁上下文中调用 other
,因此 它实际上没有做任何事情。为了达到不排除乌龟已经在的补丁的预期效果,你可以这样写:
ask turtles [
let me self
move-to one-of patches with [ not any? turtles-here with [ self != me ] ]
]
是否值得麻烦取决于您的具体情况。
最后,请注意:
; bad code:
ask turtles [
move-to one-of patches with [ not any? [ other turtles-here ] of myself ]
]
不会工作,因为 turtles-here
将在海龟的上下文中调用,而不是在补丁上下文中调用。
我一直在寻找将每只乌龟放在一个包裹中的方法,但我不想在同一个包裹中放置两只乌龟。有人知道怎么做吗? (不使用新芽)。
假设你的补丁比海龟多,你只需要:
ask turtles [
move-to one-of patches with [ not any? turtles-here ]
]
有趣的旁注:
人们常常想把 other
粘在那里,例如:
; bad code:
ask turtles [
move-to one-of patches with [ not any? other turtles-here ]
]
但这会在补丁上下文中调用 other
,因此 它实际上没有做任何事情。为了达到不排除乌龟已经在的补丁的预期效果,你可以这样写:
ask turtles [
let me self
move-to one-of patches with [ not any? turtles-here with [ self != me ] ]
]
是否值得麻烦取决于您的具体情况。
最后,请注意:
; bad code:
ask turtles [
move-to one-of patches with [ not any? [ other turtles-here ] of myself ]
]
不会工作,因为 turtles-here
将在海龟的上下文中调用,而不是在补丁上下文中调用。