Karel J Robot - Chapter 4: Conditional Instructions     BACK TO KAREL MAIN


Conditional Instructions

Real robots use artificial intelligence (AI) to make decisions. The use of AI enables robots to “think” and do things on their own. It is time to work with such robots using Karel. We begin with a programming structure that permits such decision-making: the if statement.

The IF statement

if(condition)
{
   list of statements
}

Example: How to avoid running into a wall

if(karel.frontIsClear())
{
   karel.move();
}

This code will allow karel to move forward if there is no wall in front of him, but will do nothing if there is a wall in front. The frontIsClear() method comes from a different, yet standard, robot class called, simply, Robot (see its definition and methods below).

The Robot Class – This class is a subclass of the UrRobot class, so it has all of the UrRobot class as well as those defined below. The Robot class is built in; you do not need to define it.

public class Robot extends UrRobot
{
   boolean frontIsClear();
   boolean nextToABeeper();
   boolean nextToARobot();
   boolean facingNorth();
   boolean facingSouth();
   boolean facingEast();
   boolean facingWest();
   boolean anyBeepersInBeeperBag();
}


You may have noticed that none of these methods have the word void in their headings, but rather the word boolean. The term boolean, named after mathematician George S. Boole, refers to a system of logic which only deals with two values, specifically true and false. A boolean method returns a value of true or false based on the current state of the robot and the given condition. So these methods, unlike void ones, communicate back when they are called. Their response determines whether the robot will act or not act on another command or commands. As such, they must be used inside the parentheses of an if statement.


The IF-ELSE statement - If a second action should be performed when the condition is false, it is useful to use an if-else statement. See the format below:

if(condition)
{
   list of statements
}
else
{
   another list of statements
}

So if the condition is TRUE, the statements in the if block are enacted; if the condition is FALSE, the statements in the else block are enacted.

 

Program 4-1: The Hurdler Class (focus on if statements)
Karel must run a hurdle race, jumping over hurdles or running straight when no hurdle is present. There will be at most, 8 hurdles, so the race is 8 blocks long (the race always ends at 1 st Street and 9 th Avenue. However, the arrangement of the hurdles can change from race to race. So be sure that your hurdler can run any such race. See the example initial and final situations below.

World File: hurdles.kwld                                                            BACK TO KAREL MAIN

Program 4-2: The CarpetLayer Class
Karel has a new job, carpeting rooms along an 8-block section of his world. A room is a “two-space” column that is enclosed by wall segments to the west, north and east. The door is to the south. Karel is to place a beeper in each space of the room. He begins the job with an infinite number of beepers in his bag. As with the previous tasks, Karel should be able to carpet any such 8-block section. See the example initial and final situations below.

          

               

World File: carpet.kwld                                                                      BACK TO KAREL MAIN

Program 4-3: The ColumnHarvester
Karel must now harvest 8 columns of beepers. There are exactly 3 beepers in any given column. Again, it is necessary to program Karel so that he can harvest any such field, not just the one in the example. See the example initial and final situations below. Karel begins the task at (1,1), facing East.

columns
           

World File: columns.kwld                                                                  BACK TO KAREL MAIN

Program 4-4: The Stacker
Karel needs to complete “stacks” of beepers – three high. He begins at (1, 1) and checks to see if there is a beeper there. If there is, he completes the stack by adding two more beepers on top, for a total of three. If there is no beeper there, he does nothing and moves on. He completes his task when he has reached (1, 8) and completed all necessary stacks. See an example initial and final situation below.

      Initial Situation                                             Final Situation

          stacker1      stacker2     

World File: stacks.kwld                                                                        BACK TO KAREL MAIN

Program 4-5: The PlaceSetter
Karel needs to set a “place” (two beepers), anywhere there is a table (wall) to his left. He begins at (1, 1) and checks to see if there is a wall to his left. If there is, he sets the place by putting two beepers at the current location. If there is no wall to the left, he does nothing and moves on. He completes his task when he has reached (1, 8) and set all necessary places. See an example initial and final situation below.

      Initial Situation                                                Final Situation

    places1       places2
           

World File: places.kwld                                                                  BACK TO KAREL MAIN

Program 4-6: The ChainMaker Class
Karel has been assigned the task of completing a chain of beepers. The chain is 5 blocks long and each link must contain 2 beepers. He begins the job with 10 beepers in his bag. See the example initial and final situations below. Karel begins the task at (1,1), facing East.

      Initial Situation                                            Final Situation

         

World File: chain.kwld                                                                        BACK TO KAREL MAIN

Program 4-7: The EggPicker Class (focus on the AND operator)
Karel has now been hired to pick eggs and place them in cartons. The role of the egg will be played by our versatile beeper. An egg is ready to be picked as long as a hen is not sitting on it (hens are characterized by horizontal walls). Karel must walk the hallway of the chicken coop, which is along 1 st street. If there is an egg in any given cage in the coop, he pick it up as long as a hen is not sitting on it. When finished, Karel should go to 3rd Street and 1 st Avenue, which is where the eggs will be placed in cartons, and shut off. As with the previous tasks, Karel should be able to pick eggs in any such arrangement. See the example initial and final situations below.

 

          

World File: eggs.kwld                                                                         BACK TO KAREL MAIN

Program 4-8: The TopOff Class
Karel will now go down first street and "top off" any beepers that he encounters in the first 8 blocks. Topping off involves putting a beeper on top of any existent beeper that is there. One catch - Karel can only start the task with 5 beepers. So when he runs out of beepers, he can no longer "top off," even when he encounters a beeper that needs "topping off." See the example initial and final situations below.

     topoff_1           topoff_2

World File: topoff.kwld                                                             BACK TO KAREL MAIN

Program 4-9: The SteepleChaser Class - AP ONLY
Karel will now run a steeple chase which is like a hurdle race with a twist. Each steeple can be 1, 2 or 3 wall sections high. Like the previous hurdle race, the number of steeples is unknown and they are placed in random locations. The race begins at (1,1) and ends at (1,9). See the example race below. In completing this problem, you must write and use a boolean method called rightIsClear().

World File: fig5-5.kwld                                                             BACK TO KAREL MAIN

 

Program 4-10: The BeepleChaser Class - AP ONLY
Karel will now run a different type of steeple chase in which the steeples are made of beepers. Instead of jumping the steeples, the robot picks them up. Each steeple is made up of columns of beepers that are 1, 2 or 3 beepers high (each location contains no more than one beeper however and there are no gaps in the columns). As before, the number of steeples is unknown and they are placed in random locations. The race begins at (1,1) and ends at (1,8). The world will be clear of all beepers when the race is finished. See the example race below.

 

          

World File: fig5-7.kwld                                                                         BACK TO KAREL MAIN

Program 4-11: The Custodian Class - AP ONLY
Karel has been hired as the new school custodian. One of his responsibilities is to patrol the halls and put brooms away in their closets. As he navigates a hall and finds a broom (played by our trusty beeper) he places it in the closet immediately to the left (if one is there). If there is not a closet to the left of the broom, he leaves the broom there. All hallways are 4 spaces long. See the example world shown below.

   

World File: closets.kwld                                                             BACK TO KAREL MAIN