运行代码时,有时会出现以下错误:
stack overflow (recursion too deep)
error while observer running ASK
called by procedure GO
called by Button 'go'
这只是间歇性发生,我不确定是什么原因造成的。
to go
ask cars [
speed-up
fd speed
if([pcolor] of patch-ahead 1 = yellow)
[stay]
]
tick
end
to speed-up
let car-ahead one-of turtles-on patch-ahead 3
ifelse car-ahead != nobody
[ slow-down-car car-ahead ]
[ speed-up-car ]
if speed < speed-min [ set speed speed-min ]
if speed > speed-limit [ set speed speed-limit ]
fd speed
separate-cars
end
to separate-cars ;; turtle procedure
if any? other turtles-here [
fd 3
separate-cars
]
end
to stay
set speed 0
ask cars
[
ifelse count-up = 0 [speed-up
fd speed
set color green
] [ set count-up count-up - 1
stay ]
]
end
to slow-down-car [ car-ahead ] ;; turtle procedure
;; slow down so you are driving more slowly than the car ahead of you
set speed [ speed ] of car-ahead - deceleration
end
to speed-up-car ;; turtle procedure
set speed speed + acceleration
end