我有这个代码,其中海龟在遇到墙壁和关闭的大门时会改变方向。
一开始没关系,但接下来会发出这条消息。
OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
error while turtle 259 running OF
called by procedure GO
called by Button 'Go'
我可以发送整个模型。
if state = 1 [fd speed
ifelse [pcolor] of patch-at-heading-and-distance 0 1 = black or [pcolor] of patch-at-heading-and-distance 0 1 = red
[ lt random-float 90]
[fd 1]
; ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
; [ fd 1 ]
if (pxcor >= 89 and pxcor <= 90) and (pycor > 5 and pycor < 10) [facexy (87 + random 3) 25 fd speed]
;if (pxcor >= -10 and pxcor <= 1) and (pycor >= 6 and pycor <= 23 ) [facexy ( 7 + random 3) (26 + random 3) fd speed]
if (pxcor >= 85 and pxcor <= 90) and (pycor = 26) [
let choice random 2
fd speed
if choice = 1 [leftbench]
if choice = 0 [facexy (87 + random 3) 76]
]
if (pxcor >= 83 and pxcor <= 92) and (pycor >= 75 and pycor <= 77) [rightbench fd speed]
if pcolor = brown and not any? other turtles-here
[ move-to patch-here
set seated? true
set pcolor orange
]
]
答案 0 :(得分:2)
你的模型是否在一个或两个方向关闭了世界边缘?
如果是这样,那么你的代码必须处理有时这个补丁的可能性:
patch-at-heading-and-distance 0 1
如果乌龟靠近世界边缘,将不存在。如果发生这种情况,那么patch-at-heading-at-distance
将返回nobody
,当您尝试执行时:
[pcolor] of patch-at-heading-and-distance 0 1 = black
您收到错误,因为无法从不存在的补丁中检索到pcolor
。
解决此问题的典型方法是执行以下操作:
let target patch-at-heading-and-distance 0 1
if is-patch? target and ...
最后,我想知道你是否真的是patch-at-heading-and-distance 0 1
。它相当于patch-at 0 1
,它指的是乌龟以北的补丁;不知道你为什么要那样。如果你想在乌龟之前测试补丁(如果乌龟使用fd
将会登陆的补丁),请使用patch-ahead
。