Netlogo植绒模型乌龟很无聊

时间:2017-03-12 14:43:17

标签: netlogo

我是Netlogo编程的新手,我需要调整代码,以便在飞行编队的50个刻度之后让海龟感到无聊并且向右旋转90度。我在查看其他一些示例的同时修改了代码,但是我得到了一个非常强烈的预感,它没有做它应该做的事情。你能帮帮我还是给我一个提示?这是相关代码:

turtles-own [
  flockmates         ;; agentset of nearby turtles
  nearest-neighbor  ;; closest one of our flockmates
  flock-tick
]

to setup
  clear-all
  create-turtles population
    [ set color yellow - 2 + random 7  ;; random shades look nice
      set size 1.5  ;; easier to see
      setxy random-xcor random-ycor
      set flockmates no-turtles
      set flock-tick 0 ]
  reset-ticks
end

to go
  ask turtles [ flock ] 
  ;; the following line is used to make the turtles
  ;; animate more smoothly.
  repeat 5 [ ask turtles [ fd 1 ] display ]
  ;; for greater efficiency, at the expense of smooth
  ;; animation, substitute the following line instead:
  ;;   ask turtles [ fd 1 ]
  tick
end

to flock  ;; turtle procedure
  find-flockmates
  if any? flockmates
    [ find-nearest-neighbor
      ifelse distance nearest-neighbor < minimum-separation
        [ separate ]
        [ align
          cohere ] ]
end

to find-flockmates  ;; turtle procedure
  set flockmates other turtles in-cone vision 90
end

to find-nearest-neighbor ;; turtle procedure
  set nearest-neighbor min-one-of flockmates [distance myself]
end

to flock-timer
    if any? flockmates [
    set flock-tick 50
    while [ flock-tick > 0 ] [
      set flock-tick ( flock-tick - 1 ) ]
    if flock-tick = 0  
    [ set heading ( heading - 90 ) ] ]
end

其余代码仍然与库中的模型相同。 我必须承认,我并不确定我在做什么,西北网站对我也没有多大帮助(可能是我自己的错)。

谢谢!

1 个答案:

答案 0 :(得分:3)

如果我们为您的代码添加注释,我们可以看到逻辑问题。

to flock-timer
  if any? flockmates [
    ;the next line sets `flock-tick` to 50
    set flock-tick 50
    ;the next line sets `flock-tick` to 0
    while [flock-tick > 0] [set flock-tick (flock-tick - 1)]
    ;so the next condition will always be satisfied
    if (flock-tick = 0) [
      ;and so the heading will always be changed
      set heading (heading - 90)
    ]
  ]
end

你可能会分手,以便更容易遵循逻辑。

to flock-timer ;turtle proc
  update-flock-tick
  if (flock-tick = 0) [get-bored]
end

to update-flock-ticks ;turtle proc
  ;you can add additional logic here if you wish
  if (flock-tick > 0) [
    set flock-tick (flock-tick - 1)
  ]
end

to get-bored ;turtle proc
  set heading (heading + one-of [-90, 90])
end

这不是一个完整的解决方案,但它应该让你开始。