我希望一个人走到杂草处,然后在控制台中显示它到达了杂草,然后停下来。每当我启动模型时,一个人走到一棵杂草处,并显示它到达了杂草处,但随后走到另一棵杂草处,一遍又一遍……一遍又一遍。
这是代码(人类应该从杂草中获取 1 种食物)
to action_human
if food < 1 [
ask one-of humans [
let temp-weed one-of weeds
face temp-weed
while [distance temp-weed > 1] [
ifelse distance temp-weed < 1 [ move-to temp-weed ] [ fd 1]
]
show "weed reached"
set food food + 1
]
]
end
您的代码中的其他内容一定是错误的。我使用了与您完全相同的代码,它的行为符合预期的描述。我附上了完整的代码示例。
breed [humans human]
breed [weeds weed]
humans-own [food]
to setup
ca
create-humans 1 [
setxy random-xcor random-ycor
set food 0
]
create-weeds 1 [
setxy random-xcor random-ycor
]
reset-ticks
end
to go
ask humans [action_human]
tick
end
to action_human
if food < 1 [
ask one-of humans [
let temp-weed one-of weeds
face temp-weed
while [distance temp-weed > 1]
[
ifelse distance temp-weed < 1
[
move-to temp-weed] [
fd 1
]
]
show "weed reached" set food food + 1
]
]
end