So
how can you make this bunch of termites interact with the woodchips?
To do this, you need to think about all the tasks the termites need
to accomplish. If you break down the tasks, then it's easier to write
procedure for them.
You
want the termites to search for woodchips and place them into piles.
But to place woodchips into piles, termites need to first find a woodchip.
But to find woodchips, they need to be able to move around to get to
the woodchips. Once they've found a woodchip, they need to pick it up.
Then they need to find a place to put it.
And
then to actually put it down.
Therefore,
there are four steps a termite needs to follow to succesfully make woodchip
piles.
a)
First they need to know how to move.
b)
Then they need to search for woodchips.
c)
Next, they need to find a pile for the new chip.
d)
Last, they need to drop the woodchip down in the
pile.
a)
Movement
First,
get the termites to move around, since that's the first thing a termite
needs to know how to do before it can do more complicated things.
To
do this, type this procedure in the Turtle Procedures window:
to
wiggle
fd 1
rt random 50
lt random 50
end
With
this procedure, the termite moves forward 1 unit and then wiggles by
turning a little to the left and to the right.
Try
typing wiggle
a couple
of times in the Turtle Command Center and watch what happens.
b)
Search for Woodchips
Keeping
the main goal in mind, the next task the termites need to be able to
do is find a woodchip.
You
can call the procedure search-for-chip. If a termite sees a yellow
woodchip, then it removes it from the patch.
To
do this, type this procedure in the Turtle Procedures window.
Leave a line below the wiggle procedure.
to
search-for-chip
if pc = yellow
[stamp black jump 20 stop]
wiggle
search-for-chip
end
This
procedure means that every termite checks whether the patch it's on
is yellow, and if it is, it turns the patch to black (to signify that
it picked up that woodchip). It then jumps far away, and stops.
You
can test this procedure by typing search-for-chip
in the Turtle
Command Center. This will result in the termites moving around and
turning many of the yellow woodchip patches to black. Of course, the
red termites will be covering the black patches so you won't be able
to see them yet.
So
now you have search-for-chip which changes yellow woodchips to
black to indicate that they have been picked up and are no longer on
the ground.
You
can also change your search-for-chip procedure to do wiggle
before checking
for a woodchip, and to keep going as long as it hasn't found one.
Find
the search-for-chip procedure in the Turtle Procedures
window and change it to look like this:
to search-for-chip
wig
if pc = yellow
[stamp black
stop]
search-for-chip
end
Now
click on the StarLogo window. WHOOPS! I'm sorry. That was an
error. A popup box displays the following phrase: "i don't know how
to wig in the procedure named search-for-chip in the turtle procedures
page". Sometimes errors will occur during programming, and a popup box
will help you figure out what went wrong where.
Click
OK in the popup box, and change wig
to wiggle
in the search-for-chip procedure. That should fix the bug in
your program.
c)
Find a New Pile
Now
that the termite is carrying the woodchip, it needs to find a new pile
to place it. This procedure will look a little like search-for-chip.
To
do this, type this procedure in the Turtle Procedures window.
Leave a line below the search-for-chip procedure.
to
find-new-pile
if pc = yellow
[stop]
wiggle
find-new-pile
end
All
you had to do for find-new-pile was tell the termite to wiggle,
and if it sees a yellow woodchip, to stop.
Otherwise,
keep looking (do find-new-pile again).
You
can test your latest procedure by typing find-new-pile
in the Turtle Command Center. The termites will move around looking
for an existing woodchip on the ground and then stop.
d)
Find a Place to Drop the Woodchip
You
just wrote a procedure for the termite to find a new pile to drop its
chip. But the termite just stops once it's found it.
Now
you need to write a procedure for the termite to put down its woodchip.
You
know a patch is empty if it's color is black, that is if pc
= black.
To
drop the woodchip, you can make the black patch turn yellow by stamping
it yellow.
To
do this, type this procedure in the Turtle Procedures window.
Leave a line below the find-new-pile procedure.
to
find-empty-patch
wiggle
if pc = black
[stamp yellow
stop]
find-empty-patch
end
Here
you tell the termite to wiggle,
and if it finds an empty (black) patch, stamp the patch yellow (the
same as dropping a yellow woodchip), and stop. If not, keep searching
for an empty patch.
|