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) Example: How to avoid running into a wall 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 Boolean Methods – boolean frontIsClear();
|
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) 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
World File: carpet.kwld BACK TO KAREL MAIN |
Program 4-3: The ColumnHarvester
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
World File: stacks.kwld BACK TO KAREL MAIN |
Program 4-5: The PlaceSetter Initial Situation Final Situation 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
World File: topoff.kwld BACK TO KAREL MAIN |