我一直在寻找把每只海龟放在一个包裹里的方法,但我不想让两只海龟在同一个包裹里。有人知道怎么做吗?(不使用芽)。
发布于 2015-04-21 03:23:17
假设你有比海龟更多的补丁,你所需要的就是:
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将在海龟的上下文中被调用,而不是在修补程序上下文中被调用。
https://stackoverflow.com/questions/29761545
复制相似问题