Write a program that will make Karel clean a cluttered staircase.
To view the world that contains the cluttered staircase:
Click on the Karel's World window to make it active.
Choose Open World from the File menu.
Select Cluttered Stairs and open.
Back to: Top of this page |
A very encompassing solution could be stated as follows:
Let Karel climb the staircase and pick up beepers along the way.
Breaking the solution into a series of steps will make it easier to implement :
Algorithm -Main
Begin
1. Climb the first step and pick up a beeper.
2. Climb the second step and pick up a beeper.
3. Climb the third step and pick up a beeper.
4. Climb the fourth step and pick up a beeper.
End.
Each step of our algorithm involves "climbing a step". Here we will design a algorithm for climbing a step:
Notice that climbing one step of the staircase involves four steps in our algorithm as follows:
Algorithm-Climbing a Step
Begin
1. Turn left.
2. Move
3. Turn right
4. Move
End.
Notice in our algorithm for climbing a step, 3. Turn right. The Karel language does not have a primitive for "turn right". We can only "turn left". We will use this primitive to construct an algorithm for turning right. Note that by turning left three times, we have actually turned right.
Algorithm-Turn Right
Begin
1. Turn left.
2. Turn left.
3. Turn left
End.
Back to: Top of this page |
Below is our algorithm, coded in the Karel language.
Back to: Top of this page |
In this section, lets run the code of our solution.
Activate the Karel program. Next, activate the source window. Choose Open Source ... from the File menu. Find and open the Karel folder in the dialog box that appears. Choose the file Clean the Stairs and open it.
Next, activate the world window. Choose Open World ... from the File menu. Find and open the Karel folder in the dialog box that appears. Choose the file Cluttered Stairs and open it.
Run the program by choosing Run from the Run menu.
It seems that the program works on this world. What would happen to our program if we changed the world?
Change the world and try running the program again. Does the program still work? No? Go the next section to see how we can improve the program by making it more robust.
Back to: Top of this page |
Change the world Cluttered Stairs so it looks like the following:
Run the source Clean the Stairs. Note that the program bombs when it tries to pick up the beeper that is not there. We can improve on the program, if the program was designed so that Karel would only pick up a beeper if there is one to be picked up. Our main algorithm could be altered to include a test each time Karel goes to pick up a beeper.
Main Algorithm - Improved
Begin
1. Climb the first step and if there is a beeper, pick up the beeper.
2. Climb the second step and if there is a beeper, pick up the beeper.
3. Climb the third step and if there is a beeper, pick up the beeper.
4. Climb the fourth step and if there is a beeper, pick up the beeper.
End.
The code for this improvement would be altered as follows:
Put the changes into the code Clean the Stairs and save it as Clean the Stairs Improved
Run the improved program on the world Cluttered Stairs (2). Now the program is working regardless to which stair has a beeper.
Can we make still further improvements? What if the world looked as follows?:
Would our program climb the stairs, even when there were no more stairs to climb? Yes! What can we do to improve the program. Hint: Before climbing a step, Karel should check to see if there is a step. We could include the test as part of Climbing a Step algorithm.
Make the improvements suggested and rerun your revised code on the above world. Does it work? Can you think of any other improvement? Hint: Can your program clean this staircase?
Main Menu |