我正在试图找到一种方法让汽车在我的世界中预留停车位(补丁)。我已经为补丁创建了一个保留的布尔值,但是不知道如何将这个预留链接到特定的乌龟。我想连接乌龟和补丁(我是这个程序的新手),但意识到链接只是龟龟。
除此之外,我正在尝试构建一个循环来搜索最接近目的地的地点(xcor 0)。乌龟将保留距离目的地最近的地点(并且乌龟本身作为决战者,因为xcor -1和xcor 1距离目的地的距离相同)。
我正在错误地引用我的补丁拥有的保留布尔值,但更重要的是,我可以将此预留或其他类型的变量链接到乌龟吗?
patches-own [empty reserved]
to reserve
ifelse ((patch 0 -1 != nobody) and (patch 0 -1 [reserved] = false))
[ask patch 0 -1 [set reserved true
set pcolor red]]
let i 1
[do while reserved = true[
ifelse (patch -i -1 != nobody and (patch -i -1 [reserved] = false))
[ask patch -i 1 [ set reserved true
set pcolor red]
[ if patch i -1 != nobody and (patch i -1 [reserved] = false)
[ask patch i -1 [ set reserved true
set pcolor red]]]]
set i i + 1]
]
end
谢谢!
答案 0 :(得分:2)
首先,一些小问题:
reserved
的{{1}}变量的正确方法是patch 0 -1
。[ reserved ] of patch 0 -1
引用而不是reserved-by
布尔值。您可以通过执行以下操作来分配:reserved
或类似的东西。ask patch 0 -1 [ set reserved-by turtle 0 ]
补丁变量。您在代码中没有使用它,但我怀疑empty
可以更好地满足您的任何用途。主要问题:
很少有循环是NetLogo中最好的方法。一般来说,您应该尝试使用代理集来思考,您可以使用the NetLogo dictionnary中的许多内置函数。在您的具体情况下,它可能是这样的:
not any? turtles-here
然后你可以像这样使用patches-own [ reserved-by ]
to reserve-closest-free-spot [ destination ]
let free-spots patches with [ pycor = -1 and not is-turtle? reserved-by ]
let closest-to-destination free-spots with-min [ distance destination ]
let closest-to-me min-one-of closest-to-destination [ distance myself ]
ask closest-to-me [
set reserved-by myself
set pcolor red
]
end
程序,例如:
reserve-closest-free-spot