06 Programming in Karel
06 Programming in Karel
Programming in Karel
The Karel the Robot Learns Java book includes several examples that illustrate the use of control statements in Karels world. Whenever possible, I like to solve different problems in lecture so that you can see a different set of examples. Today, for example, I will introduce the concept of control statements in the context of teaching Karel to climb a mountain. In the text, you can read a similar introduction in the context of having Karel repair potholes in a roadway. By offering both examples, you are in a better position to understand the general principles as opposed to the details of a particular problem. In lecture, however, I also want to get you to think about the problems and solve them as we go. If I include the solution in the handout, its far too tempting just to look at the answer instead of trying to work things out on the fly. Thus, I use these handouts to describe the problems, and then put the solutions up on the web so that you can look over the solutions after class. The problem we will solve today is that of getting Karel to climb mountains. Like everything else in Karels world, the mountain is abstract and must be constructed from the available materials, specifically beepers and walls. The goal is to get Karel to climb a mountain marked out by walls, put down a beeper to serve as a flag, and then to climb back down the other side. This problem is illustrated in the following diagram: before
4 3 2 1 1 2 3 4 5 6 7 4 3 2 1 1 2 3 4 5 6 7
after
At first, the goal is simply to solve the specific problem posed by this mountain. From there, however, the more interesting task is to generalize the problem so that Karel can climb larger mountains with the same stair-step structure, like this:
5 4 3 2 1 1 2 3 4 5 6 7 8 9
Move forward one square Turn 90 degrees to the left Pick up a beeper from the current square Put down a beeper on the current square
At the end of class, we designed (but did not implement) a program to solve the following problem:
In patterns of this sort, the boldfaced words are fixed parts of the pattern; the italicized parts represent the parts you can change. Thus, every helper method will include the keywords private and void along with the parentheses and braces shown. You get to choose the name and the sequence of statements performs the desired operation.
Once you have made this definition, you can use turnRight in your programs in exactly the same way you use turnLeft. In a sense, defining a new method is analogous to teaching Karel a new word. The name of the method becomes part of Karels vocabulary and extends the set of operations the robot can perform.
Control Statements
In addition to allowing you to define new methods, Karel also includes three statement forms that allow you to change the order in which statements are executed. Such statements are called control statements. The control statements available in Karel are:
The for statement, which is used to repeat a set of statements a predetermined number of times. The while statement, which repeats a set of statements as long as some condition holds. The if statement, which applies a conditional test to determine whether a set of statements should be executed at all. The if- else statement, which uses a conditional test to choose between two possible actions.
Define a method backup that moves Karel backward one square, leaving Karel facing in the same direction.
As with most control statements, the for statement pattern consists of two parts:
The header line, which specifies the number of repetitions The body, which is the set of statements affected by the for
The following method creates a square of four beepers, leaving Karel in its original position:
private void makeBeeperSquare() { for (int i = 0; i < 4; i++) { putBeeper(); move(); turnLeft(); } }
Note that most of the header line appears in boldface, which means that it is a fixed part of the for statement pattern. The only thing you are allowed to change is the number of repetitions, which is indicated by the placeholder count.
Conditions in Karel
Karel can test the following conditions:
positive condition frontIsClear() leftIsClear() rightIsClear() beepersPresent() beepersInBag() facingNorth() facingEast() facingSouth() facingWest() negative condition frontIsBlocked() leftIsBlocked() rightIsBlocked() noBeepersPresent() noBeepersInBag() notFacingNorth() notFacingEast() notFacingSouth() notFacingWest()
The simplest example of the while statement is the method moveToWall, which comes in handy in lots of programs:
private void moveToWall() { while (frontIsClear()) { move(); } }
Climbing Mountains
For the rest of today, well explore the use of methods and control statements in the context of teaching Karel to climb stair-step mountains that look something like this:
4 + + + + + + +
An if-else statement for situations in which you must choose between two different actions:
if (condition) { statements to be executed if the condition is true } else { statements to be executed if the condition is false }
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
The initial version will work only in this world, but later examples will be able to climb mountains of any height.