Using patch color and while loop to control turtle movement in netlogo

I am very new to netlogo, so this is probably a very simple question, but I am stuck. I want to use a while loop (so the commands keep on meeting during the run) and the patch color to dictate how the turtle will move.

If the turtle is not on a red patch, I want it to keep moving towards the nearest red patch.

If it's on the red patch, I want it to stay on the patch

 while [pcolor] of patch-here != red
     [
       face min-one-of patches with [pcolor = red ] [ distance myself ]
       forward 1
     ]

 while [pcolor] of patch-here = red
     [
        stop
     ]

      

When I run this, I get an error (with highlighted [pcolor] patch-here! = Red ") that says" So far this input is expected to be TRUE / False, but got TRUE / FALSE instead. "

Can anyone help me?

+3


source to share


3 answers


You just need to cast []

around the condition of the while loop, for example:

 while [[pcolor] of patch-here != red]
     [
       face min-one-of patches with [pcolor = red ] [ distance myself ]
       forward 1
     ]

      



Also, I don't think your second while loop is right. First of all, it can only work once (since it's just stop

s), so it might be if

. Second, you know that you are only the first while loop, so you know that the patch is red. Thus, the condition will always be true.

+3


source


Here is a minimal but complete example that allows simultaneous movement (as pointed out in the OP's comments). If you create a new NetLogo model and copy it, you will see that it works. You will need to add a button setup

and go

the interface, or you can enter the setting (once) in the command center, and then enter go (several times) to make the turtle move.

to setup
  clear-all
  ask n-of 20 patches [ set pcolor red ]
  create-turtles 20 [ setxy random-xcor random-ycor ]
  reset-ticks
end

to go
  ask turtles with [ [pcolor] of patch-here != red ]
  [ face min-one-of patches with [pcolor = red ] [ distance myself ]
    forward 1
  ]
  tick
end

      



The basic concept here is that everyone tick

is temporary. Your code go

contains instructions for everything that happens in one tick, and then a command tick

(at the end) to advance the clock. This is important for understanding how to think about how NetLogo works, and I suggest you take a look at some of the examples in the Model Library.

The actual code to jump to the nearest red patch is what Brian gave you.

+1


source


there is all the code with a while condition (for dummy users like me;))

to setup
  clear-all
  ask n-of 20 patches [ set pcolor red ]
  create-turtles 20 [ setxy random-xcor random-ycor ]
  reset-ticks
end

to go
  ask turtles 
  [while [[pcolor] of patch-here != red]
     [
       face min-one-of patches with [pcolor = red ] [ distance myself ]
       forward 1
     ]
  ]  
tick
end

      

0


source







All Articles