我目前正在模拟一个疏散模型,我试图告诉乌龟进入出口而没有踩黑补丁,但是我的代码无法正常工作。
这是我一直用来解决问题的代码:去
ask people
[face one-of patches with [pycor <= -24 and pycor >= -30 and
pxcor <= 26 and pxcor >= 20 ]
fd 0.5 ]
ask people with [ points = 1 ] [ set color pink ]
ask people with [ points = 0 ] [ set color red ]
ask people with [ color = red ] [ fd 0 ]
tick
end
to avoid-black
ask people [
if [pcolor] of patch-ahead 0.5 = grey - 3
[ set heading ( - heading ) fd 0.5 ]]
end
Ave555,嗨!您来对地方了以寻求帮助!
您的代码有几个不同的问题。
黑色与“灰色-2”不同,因此您对黑色的测试可能会失败,具体取决于有关如何在墙上设置色块颜色的信息。
在您的“执行”步骤中永远不会调用命令“ avoid-black”,因此它无效
我猜您已将屏幕设置为大于默认设置坐标介于-16和16之间,否则您对32个左右补丁的测试将失败。
您可能还没有忘记要求视口不环绕。您必须设置该值,否则您将获得非常奇怪的行为。 (尝试一下)
顺便说一句,将标题设置为-heading不是您想要的。标题为一个角度。您要从其减去180来反转方向。或添加180。
尝试以下代码。逐步完成一次只写一个步骤,您就可以看到它大部分有效。检测到黑色该人旋转180度并跳5步!到目前为止,一切都很好。
但是,他们只是转过身,然后回到墙壁上,因为什么都没有告诉他们不去。
下面的代码非常冗长。一次运行一个步骤,并观察输出。它告诉您避免黑色决策代码在看什么。
我希望这会有所帮助!
;; This code is incomplete but should make it easier to see why the people are not going where you expect
breed [people person]
people-own [ points]
to setup
clear-all
ask patches [ set pcolor white]
ask patches with [pycor < -5] [ set pcolor black ]
create-people 1 [ setxy random-pxcor 5 set points random 2 set color green set size 3 ]
reset-ticks
end
to go
let chosen-patch one-of patches with [pycor <= -12 and pycor >= -15 and
pxcor <= 15 and pxcor >= 10 ]
ask chosen-patch [ set pcolor red]
ask people
[
face chosen-patch
fd 0.5 ]
ask people with [ points = 1 ] [ set color pink ]
ask people with [ points = 0 ] [ set color red ]
ask people with [ color = red ] [ fd 0 ]
avoid-black
tick
end
to avoid-black
;; note: grey -3 is 2. black is zero.
;;let wall-color grey - 3
let wall-color black
ask people [
print " "
type ticks type ", pcolor of patch-ahead is " type [pcolor] of patch-ahead 0.5 type ", and wall-color is " print wall-color
if-else ([pcolor] of patch-ahead 0.5 = wall-color)
[ print "test succeded, wall ahead, reverse and jump"
set heading ( heading - 180 ) fd 5]
[ print "test failed, no wall ahead. Keep going." ]
]
end