在 «ask turtles» 方法中:如何读取与海龟相对角度和距离的补丁变量?

In an «ask turtles» method: How can I read a variable of a patch in a relative angle and distance from a turtle?

我希望 [形状为 [shape = "sheep"]] 的海龟移动到左侧或右侧,具体取决于特定相关斑块周围有多少 [有吸引力的(绿色)斑块]。

How the turtles with [shape = "sheep"] should count the patches (yellow)

(不正确的)代码如下所示:

to move-sheep
  ask turtles with [shape = "sheep"] [
    right random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead 60 2)^ 2)
    left random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead -60 2)^ 2)
    forward 1
    #(some other commands…)
  ]
end

谢谢^^

如果你真的告诉我们问题是什么,那就容易多了。你说你的代码是错误的,但不是你怎么知道它是错误的。它是否报告错误(如果是,错误是什么以及报告错误的行)?移动的乌龟走错路了吗?

无论如何,解决这个问题的最简单方法是在尝试移动之前先做一些小事。如果您简单地计算绿色补丁的数量并将其打印出来,您就可以测试代码。一旦你引入运动,你将如何判断它是否正确计数?

我仍然不完全确定你在问什么。但我认为这段代码可能会帮助您诊断问题。它会告诉您目标补丁周围的计数,因此您可以查看它是否进行了正确的计数。一旦你知道计数有效,你就可以修改移动。

to testme
  clear-all
  ask patches [set pcolor one-of [yellow green]]
  create-turtles 1 [set heading 30 set color black]
  ask one-of turtles
  [ ask patch-right-and-ahead 60 2 [set pcolor red]
    type "Right: "
    print count ([neighbors4] of patch-right-and-ahead 60 2) with [pcolor = green] 
    ask patch-right-and-ahead -60 2 [set pcolor red]
    type "Left: "
    print count ([neighbors4] of patch-right-and-ahead -60 2) with [pcolor = green]
  ]
end