What more can robots of that class do?
They can sense more things than the basic ur_Robot:
if ( condition )
{
instructions to be executed if the condition is true
}
Three important things:
You can have nested if ... else statements. Example:
if (Karel.anyBeepersInBeeperBag())
{
// Karel only looks in here if there are Beepers in the bag
//otherwise it skips to the final else
if (Karel.nextToABeeper())
{
// at this point, Karel must have beepers in its bag
// and there must be a beeper on the corner
Karel.pickBeeper();
Karel.turnLeft();
}
else
{
// at this point, Karel has beepers in its bag,
// but there are no beepers on the corner
Karel.putBeeper();
Karel.turnRight();
}
}
else
{
// Karel will only turnLeft() and turnOff() if
// it had no beepers in its beeper bag
Karel.turnOff();
}
Note:
You don't necessarily need an else clause. Maybe you want Karel to turnLeft if it is facing East, but stay in the same position otherwise.
Also, if in the if clause, you turnOff Karel, then you don't need an else either because you know that
it will not execute any other instruction anyway. We will see another case later today.
Example with appropriate comments and indentation:
// A robot of class myRobot can turn around,
// figure out if it is on a busy corner and if its back is blocked
class myRobot : Robot
{
Boolean busyCorner();
Boolean backIsBlocked();
void turnAround();
};
// Makes the robot face the opposite direction
void myRobot :: turnAround()
{
turnLeft();
turnLeft();
}
// Returns true if there is at least one beeper and at least one other robot
// on the robot's corner, returns false otherwise
Boolean myRobot :: busyCorner()
{
if (nextToABeeper())
{
if (nextToARobot())
{
return true;
}
}
return false;
}
// Returns true if there is a wall behind the robot, false otherwise
Boolean myRobot :: backIsBlocked()
{
turnAround();
if ( !frontIsClear())
{
return true;
}
return false;
}
Problem 2: it would be a good idea to comment the program (ie "now the robot is facing West"). This should help you. Also, watch for the curly braces (where should they really be?).
Problem 3: check if there are any unnecessary conditions, testing for things that are always true at that point in the program (for example, testing if there are any beepers in the beeper bag when you have just picked up a beeper!)
Problem 4: as long as you end up in the same state as the one you started in, you may do whatever you wish: turnaround, put beepers, pickbeepers etc... See if you can find a way to have the robot count the number of beepers.
Programming
One important thing: there isn't only one way of solving these problems. As long as your program is structured and does what it is supposed to do, you should be fine.
For problems 6, 7 and 8, you will have to create the world files
yourself. For 7 and 8, the worlds are drawn in the Karel++ book and
you simply need to make the corresponding .wld file.
Problem 6: create 6 different worlds to test it, one for each of the following cases: 0, 1, 2, 3, 4 or 5 beepers on the corner on which the root is standing. Try to determine what is common to some of the situations. Which predicates should you use?
Problem 7: this is a modification of the harvesting problem in the text. There are many ways of harvesting this diamond-shaped area. Pick the one you find the simplest. If you can't come up with any, look at the way they solved the similar problem in the book and try to adapt it.
Problem 8 (grad students and extra credit): As usual, try to break up this problem into smaller problems. How do you know when you are at the top of a barrier? At the bottom? At the end of the race?