Karel J. Robot Chapter 5 Conditionally Executing Instructions 1 That was then… In the preceding chapters, a Robot’s exact initial situation was known at the start of the task. When we wrote our programs, this information allowed Karel to find beepers and avoid running into walls. However, these programs worked only in their specific initial situation. If a robot tried to execute one of these programs in a slightly different initial situation, the robot would almost certainly perform an error shutoff. 2 This is now. What a robot needs is the ability to survey its local environment and then decide from that information what to do next. Now I have a brain! 3 if instruction They provide robots with their decision ability. They allow a robot to test its environment and, depending on the result of the test, decide which instruction to execute next. The if instructions enable us to write much more general programs for our robots that accomplish the same task in a variety of similar, but different, initial situations. 4 The General Form Of An if if ( <test>) { <instruction list> // otherwise known as the // “then” instruction } Note the indentation, the curly braces, and no semicolon after (<test>). There will be semicolons in the instruction list. 5 How does it work? 1. 2. 3. Robot checks to see if <test> is true or false based on the robot’s current situation If the <test> is true, the robot executes the <instruction list> If the <test> is false, the robot skips the <instruction list>. Note about <test>: The state of the robot doesn’t change, but the sender of the message will obtain information about the state of the robot. 6 New Class of Robot public class Robot extends UrRobot { } public boolean frontIsClear(); public boolean nextToABeeper();Use these public boolean nextToARobot(); method public boolean facingNorth(); names as the <test> public boolean facingSouth(); condition public boolean facingEast(); public boolean facingWest(); public boolean anyBeepersInBeeperBag(); 7 Class Robot Inherits all instructions from UrRobot We will use the Robot class as our base robot from now on The new methods of the class are predicates Preceded by the boolean return type These methods return either true or false (functions such as move() or turnLeft() have a void return type, because they return no information to the robot; merely cause the robot to behave in a particular way) 8 Example if (nextToABeeper()) { pickBeeper(); } turnLeft(); 1 9 Examples if (frontIsClear() ) { move(); // no danger of hitting wall } if ( anyBeepersInBeeperBag() ) { putBeeper(); // no danger of error } 10 Negative conditions? Suppose we want a negative form of a predicate? We can precede a predicate with the negative operator ! if (! frontIsClear()) { turnLeft(); } move(); 11 Writing New Predicates Since the predicates return a boolean value (true or false), we need to use the return instruction. Return instructions are only legal in predicates. They can’t be used in ordinary (void) methods, nor in the main task block. 12 Example public class CheckerRobot extends Robot { // insert constructor here public boolean frontIsBlocked() { return ! frontIsClear(); } public boolean notNextToABeeper() { return ! nextToABeeper(); } } 13 The General Form Of An if/else if ( <test>) { <instruction list 1> } else { <instruction list 2> } Used when you have to execute one of two alternatives 14 How does it work? Robot checks to see if <test> is true or false based on the robot’s current situation 2. If the <test> is true, the robot executes the <instruction list 1> 3. If the <test> is false, the robot executes the <instruction list 2>. 1. 15 You try Write a new predicate leftIsBlocked that determines whether there is a wall exactly one-half block away on a robot’s left. Be sure that when it terminates, the robot is on the same corner and facing in the same direction. 16 Extending Robot public class BiggerBrains extends Robot { // constructor here public boolean beeperIsToLeft() {…} try writing these predicates public void faceEast() {…} public boolean twoBeepersOnCornerOrMore() {…} } 17 public boolean beeperIsToLeft() { 18 public boolean beeperIsToLeft() { MUST put world back in initial turnLeft(); situation that it move(); was in BEFORE the function was if ( nextToABeeper() ) called { turnLeft(); turnLeft(); move(); turnLeft(); return true; } turnLeft(); turnLeft(); move(); turnLeft(); return false; } 19 public void faceEast() 20 public void faceEast() public void faceEast() { if (facingWest()) { if (!facingEast()) { turnLeft(); turnLeft(); turnLeft(); if (!facingEast()) } turnLeft(); else if (facingNorth()) if(!facingEast()) { turnRight(); turnLeft(); } } else if (facingSouth()) {turnLeft(); } } 21 public boolean twoBeepersOnCornerOrMore() { 22 public boolean twoBeepersOnCornerOrMore() { if (!nextToABeeper()) return false; else { pickBeeper(); if (nextToABeeper()) { putBeeper(); return true; } else { putBeeper(); return false; } } } 23 Simplify – bottom factoring if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); } 24 Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); } 25 Problems p.25 #2-5, 7, 8 26 Sparse Harvester The field to be harvested has been hit by a storm, so that not every corner has a beeper. Let us use conditionals to modify Harvester to solve this problem. Note: This is an advantage of using object oriented programming. The previously written Harvest class can be reused. All we need to do is create a new version of the harvestCorner method in a new subclass of Harvester. (see Chapter 3 p. 19) 27 Hurdle Jumper We want to program the robot to run a mile long hurdle race, where vertical wall sections represent hurdles. The hurdles are only one block high and are randomly placed between any two corners in the race course. One of the many possible race courses for this task is in Figure 5-2 on page 12. Require the robot to jump if, and only if, faced with a hurdle. 28 SteepleChaser #6. Program a robot to run a mile-long steeplechase. The steeplechase course is similar to the hurdle race, but here barriers can be one, two, or three blocks high. 29 MazeWalker #9. Write an instruction followWallRight for the MazeWalker class, assuming that whenever a robot executes this instruction there is a wall directly to the right. Figure 4-6 shows four of the different position changes that the robot must be able to make. This instruction is the cornerstone for a program that directs a robot to escape from a maze (next chapter) 30 Carpet Layer #11. A robot has been hired to carpet some “small rooms” along a one-mile section of its world. A small room is a corner that has a wall segment immediately to the west, north, and east. The door is to the south. Karel is to put a single beeper in only the small rooms and no other corners. Figure 4-8 shows one set of initial and final situations. You may assume that Karel has exactly eight beepers in its beeper-bag. 31 Boolean Operators Pascal (and, or, not) C++/ Java (&&, ||, !) 32 AND operator p t t f f operates on two boolean values (ie, frontIsClear, etc) will evaluate to true if both of the values are true. false otherwise “Truth table” q p and q t f t f 33 AND operator p t t f f operates on two boolean values (ie, frontIsClear, etc) will evaluate to true if both of the values are true. false otherwise “Truth table” q p and q t t f f t f f f 34 OR operator p t t f f operates on two boolean values (ie, frontIsClear, etc) will evaluate to true if at least one of the values are true. false otherwise “Truth table” q p or q t f t f 35 OR operator p t t f f operates on two boolean values (ie, frontIsClear, etc) will evaluate to true if at least one of the values are true. false otherwise “Truth table” q p or q t t f t t t f f 36 isBeepOnLeftANDisBeepOnRight() Write a predicate which returns true if there is at least one beeper on both sides, false otherwise. 37 isBeepOnLeftANDisBeepOnRight() Write a predicate which returns true if there is at least one beeper on both sides, false otherwise. public boolean isBeepOnLeftANDisBeepOnRight() { if (isBeepOnLeft()) if (isBeepOnRight()) { return true; } return false; } 38 isBeepOnLeftORisBeepOnRight() Write a predicate which returns true if there is at least one beeper on both sides, false otherwise. 39 isBeepOnLeftORisBeepOnRight() Write a predicate which returns true if there is at least one beeper on both sides, false otherwise. public boolean isBeepOnLeftORisBeepOnRight() { if (isBeepOnLeft()) { return true; } if (isBeepOnRight()) { return true; } return false; } 40 #13 Write a predicate that will return true if and only if the robot executing it is both next to a beeper AND its left is blocked. Write a predicate that will return true if the robot executing it is either next to a beeper OR its left is blocked. 41 #14 Write a predicate that will return true if and only if the robot executing it has exactly two beepers in its beeper-bag. Write a predicate that will return true if and only if the robot executing it is on a corner with at most two beepers. 42