Download Instructor`s Guide - Seidenberg School of Computer Science and
Transcript
of the simulator. It has many static functions you can use to control it. You can call these
from your main (or elsewhere), but I usually put the world statements into a static
initializer in my class. For example, in MyClass above we could add the following:
static
{
World.reset();
World.setSize(10, 10);
World.readWorld("worlds","fig3-1.kwld");
World.setVisible(true);
World.setDelay(30);
World.setTrace(true);
World.showSpeedControl(true);
}
A static block is just an anonymous function that is automatically called when the class
containing it is first loaded. It guarantees initializations. I use it to avoid mixing the robot
code with the manipulation of the environment itself. You can also use the Initialzier
class shown above, even without BlueJ. Note, however, that if you use a static initializer,
don't put one like this in a class intended to be subclassed. The initializer is run when the
class is loaded and it will be loaded when the sub class is loaded. You might get two sets
of world objects overlaid. When you read a world file it doesn't clear the world before
loading though you can explicitly do so with reset, as above.
The size (10, 10) is the default. The first argument is the number of streets. This form of
readWorld lets you specify a folder in which the world file can be found as well as the
name of the file itself. My "worlds" folder is inside my working directory.
SetVisible(true) is probably necessary as the world is not visible by default. SetDelay is
100 (slow) by default and 30 may be too fast for the rendering to keep up, so the robots
will seem to skip. You need to experiment. The trace is on (true) by default. It shows
each robot's state after every change by writing a description to System.out. There are
other options. See the JavaDocs.
2.1 Changing Positions
Move is discussed and the idea of an error is introduced. This is changed from the
original Karel the Robot. If you have not taught with Karel before, it does not matter, but
die-hard robot fans should check the new description of move carefully. If a robot
attempts to move when its front is blocked, it no longer executes the move by turning off.
It executes an error shutoff instead of a move.
2.2 Turning in Place
Turnleft is discussed and there is little change in this edition.
It is important when teaching both the move and turnLeft instructions that you emphasize
where a robot must be. You must stress that a robot is always on a corner or intersection
and always facing one of the four directions. Having experience with students ranging
from junior high through graduate students and even school teachers, it helps to use
visual aids. In lecture, we have been known to play Robot and use chairs for walls and
Page 7