2

我想将我的乌龟移动到其视野中最近的红色或绿色色块。我尝试了这段代码,但它没有移动。出了什么问题?

while [collectedDirt = 5]
[
  ask turtle 0 [ 
                   let nearest-patch min-one-of (patches with [pcolor = red or pcolor = green] in-cone 15 20)[distancemyself]
                   face nearest-patch
                   fd distance nearest-patch

               ]
  set collectedDirt collectedDirt + 1
  search-dirt ;; research whether there is red patch in-cone because of new position
] 
4

1 回答 1

4

您可能想为我们提供更多背景信息,但我的猜测是您需要类似的东西while [collectedDirt < 5]而不是while [collectedDirt = 5]. 如果这个块是你的海龟“收集污垢”的唯一方法,那么代码可能永远不会被执行......

编辑:

如果视锥中没有红色/绿色斑块,您可能还需要添加一个条件:

if is-patch? nearest-patch [ 
  face nearest-patch
  fd distance nearest-patch
  set collectedDirt collectedDirt + 1
]
于 2012-02-19T21:46:46.917 回答