Karel J Robot - Chapter 5 Programs: Repeating Instructions              BACK TO KAREL MAIN

Instructions that Repeat

We will now learn how to make Karel perform repetitive tasks. In other words, we will learn how to make a command that can cause Karel to perfrom an action over and over. You will see that this saves many lines of code.

In computer science, the act of repeating a block of code is called a loop. There are two types loops we will use: the for loop and the while loop. See the examples below.

The FOR loop – a loop used when you want a robot to perform the same task a specific number of times.                                                

Example 1: a for loop used to make exactly 5 moves:

public void move()
{
     for(int x=1; x<=5; x++)
     {
          move();
     }   
}

Example 2: a for loop used to make n moves:

public void move(int n)
{
     for(int x=1; x<=n; x++)
     {
          move();
     }   
}

The WHILE loop – a loop used when you want a robot to perform a task repeatedly until a condition becomes false (an unknown number of times).                                          

Example 1: a while loop used to make a robot move to a wall an unknown distance away:

public void moveToWall()
{
     while(frontIsClear())
     {
          move();
     }   
}

Example 2: a while loop used to pick up an unknown number of beepers at its current location:

public void pickAllBeepers()
{
     while(nextToABeeper())
     {
          pickBeeper();
     }   
}

 


Program 5-1: The Steeplechaser2 Class (focus on FOR loops)
Karel must run a steeplechase race, jumping over steeples and picking up beepers as he goes. There will be exactly 8 steeples and beepers, so the race is 8 blocks long (the race always ends at 1st Street and 9th Avenue. See the example initial and final situations below.

              

World File: steeple2.kwld                                                                BACK TO KAREL MAIN

Program 5-2: The DiagonalMaker Class (focus on FOR loops)
Karel must make a diagonal of beepers of a length dictated by the programmer. For example, if the programmer asks for 5 beepers, the diagonal will be of length 5. See the example initial and final situations below.


           

World File: blank.kwld                                                                      BACK TO KAREL MAIN

Program 5-3: The SquareMaker Class (focus on FOR loops)
Karel has been hired to build square rooms out of beepers. He is given instructions by you, the programmer, to go to a given location and build a room of a given size. In the example below, he was told to go to (2, 3) and build a room of size 4. It is to be assumed that Karel begins each task at the Origin, facing East, with an infinite number of beepers in his bag.

               

World File: blank.kwld                                                                     BACK TO KAREL MAIN

Program 5-4: The Fetcher Class (focus on WHILE loops)
It’s Karel the robot dog! He can fetch a ball (beeper, of course) but he can’t run directly to it. He has to stick to East-West, North-South movement. He begins his little game at the origin, facing East. To help him out, we place a wall at the point where he needs to turn. He then turns and runs to the place where the ball is. Finally, he must return to the origin and drop the ball off to his master. Below is a sample situation. Your task should be able to handle any such situation.

                

World File: fetch.kwld                                                                     BACK TO KAREL MAIN

Program 5-5: The Tiler Class (focus on WHILE loops)
Now our robot hero will be charged with laying ceramic tile in some rooms in a building. He will begin at the opening to a hallway, then go to each room and tile each available square in the room with a beeper. When he reaches the end of the hallway, he is to return to the entrance to the hallway where he started out. The robot should begin the task with an infinite number of beepers. Below is a sample building. Your task should be able to tile any such building.

    

World File: tiler.kwld                                                                         BACK TO KAREL MAIN

Program 5-6: The TrashCollector Class (focus on WHILE loops)
Karel is now employed by the city sanitation department. He has to collect all of the trash cans on 1st street. The number of houses on the street is unknown, but the end of the street is marked by a wall. He has to empty all trash cans (beepers) at each house. Each house has at least one trash can, but the number varies. It is to be assumed that Karel begins the task at the origin, facing East and the first house is directly in front of him. When finished, Karel should return all beepers to the origin, dump them there and turn himself off. See the example task below.

             

World File: trash.kwld                                                                       BACK TO KAREL MAIN

Program 5-7: The SuperSteepleChaser Class (focus on WHILE loops) - AP ONLY
In this race, the hurdles are arbitrarily high and the course has no fixed finish line. The finish of each race is marked by a beeper that the robot must pick up before turning itself off. The example below shows one possible course.

World File: steeple3.kwld                                                                 BACK TO KAREL MAIN

Program 5-8: The Trailer Class (focus on WHILE loops) - AP ONLY
Karel left himself a trail of beepers to find his way back home. He wants to return home, so he will follow the trail, picking up the beepers as he goes. He is home when he encounters a wall at the end of the trail. It is to be assumed that Karel begins the task facing East at (2,2) with a beeper in front of him that begins the trail. Karel should be turned off when he reaches home. See the example below.

World File: trail.kwld                                                                        BACK TO KAREL MAIN

Program 5-9: The SuperDuperSteepleChaser Class (focus on WHILE loops) - AP ONLY
See the example below - you know the drill by now.

World File: fig6-22.kwld                                                                 BACK TO KAREL MAIN

Program 5-10: The Treasurehunter Class (focus on WHILE loops) - AP ONLY
Program Karel to go on a treasure hunt. The treasure is marked by a corner containing 5 beepers. Other corners (including the corner on which Karel begins) contain clues, with each clue indicating which direction Karel should proceed. The clues are as follows: 1 beeper means Karel should go North, 2 means West, 3 means South and 4 means East. Karel should follow the clues until he reaches the treasure corner where Karel should turn himself off. See the example below:

World File: fig6-27.kwld                                                                 BACK TO KAREL MAIN