Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Standard view
Full view
of .
×
0 of .
Results for:
P. 1
09-karel-examples

# 09-karel-examples

Ratings:

4.0

(1)
|Views: 1,337|Likes:

### Availability:

See More
See less

02/01/2013

pdf

text

original

Mehran Sahami
Handout #9
CS 106A
September 26, 2008
Example Karel ProblemsPortions of this handout by Eric Roberts
Running a steeple chase
In class, we examined aSteepleChase program that allows Karel to run a "Steeple
Chase" (like a hurdles race, but with arbitrarily large hurdles) where:
\ue000
Karel starts at position (1, 1), facing East.
\ue000
The steeple chase is guaranteed to be 9 avenues long.
\ue000
There can be arbitrarily many hurdles that can be of arbitrary size, located
between any two avenues in the world.
\ue000
Karel should "jump" each hurdle one at a time.
For example, if you were to execute theSteepleChase program, you would see
something like the following before-and-after diagram:
Before
After

Below is the program listing of theSteepleChase program, which provides an example of various control structures in Karel, program decomposition, and comments (including the specification of pre- and post-conditions).

\u20132 \u2013
/** File: SteepleChase.java
*-----------------------

* Karel runs a steeple chase the is 9 avenues long.
* Hurdles are of arbitrary height and placement.
*/

import stanford.karel.*;

public class SteepleChase extends SuperKarel {
/** To run a race that is 9 avenues long, we need to move
* forward or jump hurdles 8 times.

*/
public void run() {
for (int i = 0; i < 8; i++) {
if (frontIsClear()) {
move();
} else {
jumpHurdle();
}
}
}/** Pre-condition: Facing East at bottom of hurdle
* Post-condition: Facing East at bottom in next avenue
*
after hurdle
*/

private void jumpHurdle() {
ascendHurdle();
move();
descendHurdle();

}/** Pre-condition: Facing East at bottom of hurdle

* Post-condition: Facing East immediately above hurdle
*/
private void ascendHurdle() {

turnLeft();
while (rightIsBlocked()) {
move();
}turnRight();
}
\u20133 \u2013
/** Pre-condition: Facing East above and immediately after hurdle
* Post-condition: Facing East at bottom of hurdle
*/

private void descendHurdle() {
turnRight();
moveToWall();
turnLeft();

}/** Pre-condition: none

* Post-condition: Facing first wall in whichever direction
* Karel was facing previously
*/
private void moveToWall() {

while (frontIsClear()) {
move();
}
}
}Creating a line of beepers
Consider the problem of writing a methodcreateBeeperLine, which creates a line of
beepers beginning at Karel\u2019s current corner and proceeding forward to the next wall. For
example, if you were to executecreateBeeperLine in an empty world, you would see
something like the following before-and-after diagram:
1
2
3
4
5
6
1234
Before
1
2
3
4
5
6
1234
After
The problem is slightly harder than it looks. Unless you think carefully about the
problem, it is easy to find yourself making a common programming error that keeps the
program from working as you\u2019d like.For example, we might initially be inclined to solve
the problem as follows:
private void createBeeperLine() {

while (frontIsClear()) {
putBeeper();
move();

}
}

## Activity (3)

### Showing

AllMost RecentReviewsAll NotesLikes