你是对的 - 与while
循环你的海龟将卡在他们来到的第一个蓝色补丁,因为如果他们踩到邻近的补丁,他们会立即想要移回到最接近的蓝色补丁(一他们刚刚离开)。此外,while循环中发生的所有事情都发生在单个tick中 - 如果您只希望它们移动到最近的蓝色补丁作为您的设置的一部分,请使用move-to
。如果他们移动到最接近的蓝色补丁是对您很重要,则可能在此使用if
声明而不是while
。
此外,您正在描述两种不同的“运动模式”。首先,你希望海龟移动到你想让他们遵循的电路。然后,如果他们在该电路中,则希望他们按照有序路径在电路中寻找下一个蓝色补丁,然后移动到该补丁。因此,建立两个独立的程序并在适当的时间打电话可能会更容易。如果海龟知道他们下一步应该去哪里(当前目标)以及他们应该执行哪种移动模式,这可能也会有所帮助。所以,你可以建立turtles-own
变量,如:
turtles-own [
on-circuit?
my-target
]
请务必设置这些变量在你的设置,使它们不是不确定变量的缺省“0”:
to setup
ca
reset-ticks
ask (patch-set patch 5 5 patch 5 -5 patch -5 5 patch -5 -5) [
set pcolor blue
]
crt 1 [
set on-circuit? false ;;; so a starting turtle knows which movement procedure to use
set my-target nobody
setxy random 30 - 15 random 30 - 15
pd
]
end
然后,你可以运行你的go
程序,使得如果他们的“电路?”,海龟会尝试进入电路。是假的,如果他们的“在线?”他们会走路?是真的。
to go
ask turtles [
ifelse on-circuit? = false [ ;;; do this if turtle is not yet on the circuit
get-to-circuit
]
[ ;;; do this if the turtle has been designated as on the circuit
walk-circuit
]
]
tick
end
现在你让你的get-to-circuit
和walk-circuit
程序。我会告诉你我是如何设置我的get-to-circuit
,而是看你能不能找出walk-circuit
的休息:
to get-to-circuit
set my-target min-one-of other patches with [pcolor = blue ] [ distance myself ]
face my-target
fd 1
if distance my-target < 1 [
move-to my-target
;;; This lets the turtle know it can switch to "walk-circuit" on the next tick
set on-circuit? true
set heading one-of [ 0 90 180 270 ]
]
end
to walk-circuit
if my-target = nobody [
set my-target one-of (other patches with [ pcolor = blue ]) in-cone 10 180
]
?
?
? ...
它完美!谢谢! –