To define a new robot, you must use the NewRobotType and IsLikeA statements. The IsLikeA must be the first statement in the NewRobotType definition. One is never used without the other.
The definition must come before any executable statements, so it must appear within a define block. For example, if we wanted to define another type of robot called another_bot and then wanted to create one named twin to try it out, we could do the following:
define
{
NewRobotType another_bot
{
IsLikeA basic_bot;
}
}
execute
{
new basic_bot karel at 1,1;
tell karel:
{
if (FrontIsClear) Move 1;
TurnOff;
}
new another_bot twin at 3,3;
tell twin:
{
if (FrontIsClear) Move 1;
TurnOff;
}
}
This is not all that useful at the moment because another_bot is exactly
like a basic_bot.
We can't tell twin to do anything that we couldn't
tell karel.
What would make it
useful is to add enhancements to the definition of another_bot.
This can include defining
new instructions and/or specifiying a starting number of beepers in the robot's
bag (a basic_bot starts without any beepers in its bag).
You can also declare integer values that you want the robot to remember.
Allowable enhancements are:
Now we can do something useful with a new robot type, as shown below:
define
{
NewRobotType smarter_bot
{
IsLikeA basic_bot;
DefineInitialBeepers 5;
DefineNewInstruction turnright as iterate 3 times TurnLeft;
DefineNewInstruction moveright as
{
if (RightIsClear)
{
turnright;
Move 1;
}
}
}
}
execute
{
new basic_bot karel at 1,1;
tell karel:
{
iterate 4 times
{
if (RightIsClear)
{
iterate 3 times TurnLeft;
Move 1;
}
}
TurnOff;
}
new smarter_bot egghead at 3,3;
tell egghead: iterate 4 times moveright;
tell egghead: TurnOff;
}
You can also go:
Created on 19 June 1995
Last revised 22 January 1997