Abha Trivedi
Abha Trivedi

Reputation: 205

How to ask turtles to avoid a patch with specific color at patch-ahead 1 but turtles move forward by bigger number (not fd 1) in one step in NetLogo

I am trying to ask turtles with any heading (random 360) to avoid the patches with red color. But, I observed that if a turtle is asked to move " fd 1 + random-float 2.0" then sometime turtles turns (set heading heading - 180) when there is a red patch ahead and sometimes(even most of the times) do not turn. Also when I ask the turtles to move " fd 1 " or " fd 0.1 + random-float 0.9 " the code works all fine. Hopefully the reason behind is the number of patches I am asking the turtles to move in one step. What will be the next patch for the move "fd 0.1 + random-float 0.9" and how could I make this working with patch-ahead 1. My code and the interface is added.

enter image description here

to setup
    clear-all
    ask patches [set pcolor green ]
    ask patches with [pycor = 3] [set pcolor red]

   create-turtles 40      
   [ 
      set color blue
      set xcor random-pxcor
      set ycor random-pycor
      set heading random 360
      set size 1
      set speed 1 + random-float 2.0
  ]
end

to go
  ask turtles [
    fd speed
    avoid-walls
  ]
end

to avoid-walls
   if [pcolor] of patch-ahead 1 = red [set heading heading - 180]
end

Upvotes: 2

Views: 2866

Answers (1)

David Merinos
David Merinos

Reputation: 1295

Try using in-cone instead of patch-ahead

to avoid-walls
   let front-patches patches in-cone 2 75
   if pcolor of one-of front-patches = red [set heading heading - 180]
end

Upvotes: 3

Related Questions