RoBOTL Module 2

by Pia Haugerud and Professor Lemone



In the first module we defined robots, placed them in the World 
and let them perform simple actions, like Move and Turn. 
This module will present the remaining instructions for a basic_bot.
You might want to clear the Code box between the steps in this module.

 
STEP 1 - in which we let Adam repeat his actions at different speeds.


Clear the Code box of any old contents. In the RoBOTL Applet Window,
select the Examples menu and choose the example file "Adam Walks the Walls".
The content of the file should now be displayed in the Code box.

        Run the program, and observe Adam's movements.
        
While the program is running, use the Options menu to alter the delay from
the default value of 250 ms to 50 ms. You will see that a change in value
to the smaller causes the robot to move faster (the delay in milliseconds
beween instructions is shorter). You can try out the other delay-options 
and find whatever value is good for the computer you are curreently using.

The value can be changed back at any time!


 
STEP 2a - where we examine Adam's twists and turns

This is what the program, "Adam Walks the Walls", looks like:

execute
{
  new basic_bot adam;
  tell adam:
  {
   turnleft;
   
   Move 19;
   turnleft;
   turnleft;
   turnleft;

    Move 19; 
    iterate 3 times turnleft;
  }
}

You probably noticed Adam moving along the Walls of the World, and
making round turns before smashing into the walls? 

Look at the program listing above, and note that the round turns are
caused by a repetitive use of the "turnleft" command. Because the 
robot turns in 90 degere increments every time turnleft is called, 
calling it twice causes a 180 degree turn (turn around), and three 
turnlefts equals a turn to the right (actually a 270 degree turn to 
the left!).

The obvious way of making the robot keep on turning is by repeating the
turnleft statement as done in the first lines of the program.

THE SMART WAY of doing it is by using the statement ITERATE, which means
repeat, like this: 

Iterate  times ;

This reads; repeat  times ,
and is a technique we can use whenever we know how many times we want
to repeat an action.

 
STEP 2b - in which we do multiple iterations 


Modify the program to make Adam make it all the way around the World,
without crashing into the walls. 


Your program probably looks something like this:

execute
{
  new basic_bot adam;
  tell adam:
  {
   turnleft;
   Move 19;
   iterate 3 times turnleft;

   Move 19; 
   iterate 3 times turnleft;

   Move 19;
   iterate 3 times turnleft;
   
   Move 19;
   iterate 3 times turnleft;
   
  }
}



An even more compact (but somewhat harder to read) version would be:

execute
{
  new basic_bot adam;
  tell adam:
  {
   turnleft;
   
   iterate 4 times {
      Move 19;
      iterate 3 times turnleft;
      }

  }
}


The trick here is to 

        1) see the similarity in codelines, in this case:

      Move 19;
      iterate 3 times turnleft;
      
and 

        2) find how to express the repetition by including an outer iterate. 

Note that we in this case use the curly brackets to embrace
all the code lines that are involved in the outer iterate statement.
 
Got it? If you have, jump on to STEP 3, else try looking at the examples
in STEP 2 again.


The use of a statement within the same statement is often referred to
as nesting. You will see more of this as we move on and start working 
with conditions in module 4 and loops in module 5.


 
STEP 3 - where we change the colors of the World


You have seen that the Options menu is accessible at all times, and that
it contained a Delay options that let you control the speed of the
program execution.
The Options menu also contains options for color control, and you can apply
the color schemes from the menu to a program as it is running. 


Use Options - Color to change the color scheme to Frogland, or find another
combination you like better. A value from the Option menu can be set and reset
at any time, and will immediately slam into action.



 
EXERCISE

Write a program in which Adam walks a full circle around the walls and Eve
walks a 13 by 13 square in the opposite direction. 
You may reuse the code you already used in STEPs 2 and 3 if you wish.


Click View Code or Load Code to see an example that solves the problem,
and bear in mind - as usual - there are more than one CORRECT way to
write a program!



 
KEYWORDS
Iterate, Option - Color, Option - Delay



Send questions and comments to: Karen Lemone