netlogo:如何将不同颜色的海龟移向相同颜色的补丁

时间:2015-04-17 19:12:35

标签: netlogo

我是一名新的Netlogo学员。我想让我的乌龟一起走向补丁。每只乌龟都找到了相同颜色的补丁。每只乌龟每蜱移动一步。我还计划为每只乌龟添加一个状态,显示进入正确的补丁需要多少步骤。我目前的代码是:

to setup
  clear-all
  ask patch 0 0 [ set pcolor 5 ]
  ask patch -5 0 [ set pcolor 15 ]
  ask patch 5 0 [ set pcolor 25 ]
  ask patch -10 0 [ set pcolor 35 ]
  ask patch 10 0 [ set pcolor 45 ]
  ask patch -15 0 [ set pcolor 55 ]
  ask patch 15 0 [ set pcolor 65 ]
  ask patch -20 0 [ set pcolor 75 ]
  ask patch 20 0 [ set pcolor 85 ]
  crt-turtle
  reset-ticks
end

to crt-turtle
  create-turtles 1000
  ask turtles [ setxy random-xcor random-ycor 
    set shape "car" 
    set color one-of [ 5 15 25 35 45 55 65 75 85 ]
  ]
end

to go
  ask turtles [
    face min-one-of patches with [ pcolor = 35 ] [ distance myself ]
    forward 1
  ]
end

感谢。

1 个答案:

答案 0 :(得分:1)

你几乎就在那里。只需将35更改为[color] of myself,或者更明确一点:

to go
  ask turtles [
    let _mycolor color
    face min-one-of patches with [pcolor = _mycolor] [distance myself]
    forward 1
  ]
end