Good afternoon, I'm trying to make my turtles to move between a set of 4 blue patches. I can make them get to those patches but after that they just stay there, and what I need is to them continuously move (in order) to the next blue patch to their right. I don't know how to do it.
This is the section of the code I'm talking about:
to move-turtles
ask turtles
[while [[pcolor] of patch-here != blue]
[
face min-one-of patches with [pcolor = blue ] [ distance myself ]
forward 1
]
]
tick
end
By advance, thank you!
You're right- with a while
loop your turtles will be stuck on the first blue patch they come to, since if they step onto a neighboring patch they will immediately want to move back to the closest blue patch (the one they just left). Additionally, everything that happens in the while loop occurs within a single tick- if you just want them to move to the nearest blue patch as part of your setup, just use move-to
. If their movement to the nearest blue patch is important to you, it's probably better here to use an if
statement rather than a while
.
Additionally, you are describing two different "modes" of movement. First, you want the turtles to move to a circuit that you want them to follow. Then, if they are in that circuit, you want them to follow an ordered path where they target the next blue patch in the circuit and then move to that patch. Accordingly, it's probably easier to set up two separate procedures and call them at the appropriate time. It would probably also help if the turtles know where they are supposed to go next (a current target) and which movement mode they should be executing. So, you could set up turtles-own
variables such as:
turtles-own [
on-circuit?
my-target
]
Make sure you set those variables in your setup so that they are not the default "0" of undefined variables:
to setup
ca
reset-ticks
ask (patch-set patch 5 5 patch 5 -5 patch -5 5 patch -5 -5 ) [
set pcolor blue
]
crt 1 [
set on-circuit? false ;;; so a starting turtle knows which movement procedure to use
set my-target nobody
setxy random 30 - 15 random 30 - 15
pd
]
end
Then, you can run your go
procedure such that the turtles will try to get onto the circuit if their "on-circuit?" is false, and they will walk the circuit if their "on-circuit?" is true.
to go
ask turtles [
ifelse on-circuit? = false [ ;;; do this if turtle is not yet on the circuit
get-to-circuit
]
[ ;;; do this if the turtle has been designated as on the circuit
walk-circuit
]
]
tick
end
Now you make your get-to-circuit
and walk-circuit
procedures. I'll show you how I set up my get-to-circuit
, but see if you can figure out the rest of the walk-circuit
:
to get-to-circuit
set my-target min-one-of other patches with [pcolor = blue ] [ distance myself ]
face my-target
fd 1
if distance my-target < 1 [
move-to my-target
;;; This lets the turtle know it can switch to "walk-circuit" on the next tick
set on-circuit? true
set heading one-of [ 0 90 180 270 ]
]
end
to walk-circuit
if my-target = nobody [
set my-target one-of ( other patches with [ pcolor = blue ] ) in-cone 10 180
]
?
?
? ...