It's time to get the turtles interacting with each other. Suppose your frog breed wants to eat the bug breed? How do you breed frogs? Here's some sample code that initially creates 2 frogs and 100 bugs. When a frog comes in contact with a bug, it eats (kills) the bug. I've left a red patch where this event occurs. When 2 frogs come in contact with each other, another frog will hatch.
New commands & techniques
count-breed-here
Returns the number of turtles whose breed is breed which are on the current patch. For example if count-frogs-here > 2 [setc blue] makes the turtle set its color to blue if there are more than two turtles of type frog on its current patch.
ask-breed [list of commands]
Asks all turtles of breed to run [list of commands]. The observer will wait for all of the turtles to finish before continuing.
die
Turtles die, meaning that they stop running all code and disappear forever.
hatch [list of commands]
Make an exact copy of a turtle, including turtles-own variables and state variables. The [list of commands] is then run on the cloned turtle.
Observer procedures
breeds [frogs bugs]
to setup
cg
ct
create-bugs-and-do 100 [setshape termite-shape setc yellow rt 30 jump 7]
create-frogs-and-do 4 [setshape frog-shape setc green]
end
to movebugs
ask-bugs [move-bugs]
end
to movefrogs
ask-frogs [move-frogs]
end
Turtle procedures
to move-bugs
loop
[
seth random 360 fd 1
if (breed = bugs) and (count-frogs-here = 1) [stamp red die]
]
end
to move-frogs
loop
[
seth random 360 fd 3
if (breed = frogs) and (count-frogs-here = 2) [hatch [move-frogs]]
]
end