Write a program that will make a robot clean a cluttered staircase.
To view the world that contains the cluttered staircase:
Run the karelpp.exe application.
Choose Open from the File menu.
Select the file stairs.wld from the karelwin folder and open.
Back to: Top of this page |
A very encompassing solution could be stated as follows:
Let the robot 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.
/* Begin Program */ class Stair_Sweeper:ur_Robot { void climbStair(); void turnRight(); }; void Stair_Sweeper::turnRight() { turnLeft(); turnLeft(); turnLeft(); } void Stair_Sweeper::climbStair() { turnLeft(); move(); turnRight(); move(); } /* Begin Task */ task { /* Declarations */ Stair_Sweeper Alex (1, 1, East, 0); /* Statements */ Alex.climbStair(); Alex.pickBeeper(); Alex.climbStair(); Alex.pickBeeper(); Alex.climbStair(); Alex.pickBeeper(); Alex.turnOff(); } /* End Task */ /* End Program */
Back to: Top of this page |
In this section, lets run the code of our solution. From the previous step, we assume that you have Karel++ running and you have the stairs.wld world opened. It should look similar to the figure to the right, but without the STAIR.KPP window (yet).
You can open Notepad and type the above Karel++ code in, or you can copy it from above and paste it in to notepad. Save the
resulting file as a text file called myStairs.kpp
In the Karel++ application choose Open from the File menu. In the resulting dialog box, in the lower left where it says "List files of type", choose "Karel++ Source" as the type. Find your myStairs file and open it.
Run the program by clicking the Play button, the third button down (a single rihgt-pointing triangle) on
tool pallet at the right.
If you mis-typed anything, the Karel++ software will tell you and will not run the program. For instance if you left out the semi-colon after the first Alex.climbStair(); command in the program, when you hit the play button, the Karel++ software would respond with:
Parse Error: syntax error File "C:\WINDOWS\DESKTOP\KARELWIN\STAIR.KPP": Line 26.You would then open stair.kpp in Notepad, go to line 26, figure that the semi-colon is missing, add the missing semicolon, save the file, and try it again in Karel++.
Back to: Top of this page |
The previous solution was good if you knew that there was a beeper on every stair. However, if you don't know which stairs have beepers, the above Karel++ program would fail if it tried to pick up a beeper that wasn't there, as depicted in the digram to the right.
We can improve on the program by designing it so that Alex 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 Alex 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.
We would code this improved algorithm using Conditional Statements.
Change your Mystairs.kpp program so that the task part now uses Condtional Statements, like this:
task { Stair_Sweeper Alex(1, 1, East, 0); Alex.climbStair(); if (Alex.nextToABeeper()) { Alex.pickBeeper(); } Alex.climbStair(); if (Alex.nextToABeeper()) { Alex.pickBeeper(); } Alex.climbStair(); if (Alex.nextToABeeper()) { Alex.pickBeeper(); } Alex.turnOff(); }Save this new program as MyStairs1.kpp. Run this program with the original stairs.wrld world. It should still work. It will now also work on worlds that don't have beepers on every stair.
Using Loops
As a final improvement, note that the task part of the program has three repetitions of the same Alex.climbStair(); followed by the same conditional statement with a Alex.pickBeeper(); . What if we changed the number of stairs from four to six? We'd have to copy more code into the task. However, if we used a loop statement, the program would be easier to change. In general, if you see code repeated many times in sequence, it is usually better placed in an loop statement.
Change the task part of myStairs.kpp to now look like this:
task { Stair_Sweeper Alex(1, 1, East, 0); loop (3) { Alex.climbStair(); if (Alex.nextToABeeper()) { Alex.pickBeeper(); } } Alex.turnOff(); }Save this revised program as myStairs2.kpp and try it in Karel++. This improved program should work as the original did, but it will be more flexible due to the conditional statements, and easier to modify due to the loop statement.