Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Conditionally Executing Instructions

Similar presentations


Presentation on theme: "Chapter 5 Conditionally Executing Instructions"— Presentation transcript:

1 Chapter 5 Conditionally Executing Instructions
Karel J. Robot Chapter 5 Conditionally Executing Instructions

2 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.

3 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!

4 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.

5 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.

6 How does it work? 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.

7 New Class of Robot public class Robot extends UrRobot
{ public boolean frontIsClear(); public boolean nextToABeeper(); public boolean nextToARobot(); public boolean facingNorth(); public boolean facingSouth(); public boolean facingEast(); public boolean facingWest(); public boolean anyBeepersInBeeperBag(); } Use these method names as the <test> condition

8 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)

9 Example if (nextToABeeper()) { pickBeeper(); } turnLeft(); 1

10 Examples if (frontIsClear() ) { move(); // no danger of hitting wall }
if ( anyBeepersInBeeperBag() ) { putBeeper(); // no danger of error }

11 Negative conditions? Suppose we want a negative form of a predicate?
We can precede a predicate with the negative operator ! if (! frontIsClear()) { turnLeft(); } move();

12 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.

13 Example public class CheckerRobot extends Robot
{ // insert constructor here public boolean frontIsBlocked() { return ! frontIsClear(); } public boolean notNextToABeeper() { return ! nextToABeeper();

14 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

15 How does it work? 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 1> If the <test> is false, the robot executes the <instruction list 2>.

16 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.

17 try writing these predicates
Extending Robot public class BiggerBrains extends Robot { // constructor here public boolean beeperIsToLeft() {…} public void faceEast() public boolean twoBeepersOnCornerOrMore() } try writing these predicates

18 public boolean beeperIsToLeft()
{

19 public boolean beeperIsToLeft() { turnLeft(); move();
if ( nextToABeeper() ) turnLeft(); turnLeft(); move(); turnLeft(); return true; } turnLeft(); turnLeft(); move(); turnLeft(); return false; MUST put world back in initial situation that it was in BEFORE the function was called

20 public void faceEast()

21 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(); }

22 public boolean twoBeepersOnCornerOrMore()
{

23 public boolean twoBeepersOnCornerOrMore()
{ if (!nextToABeeper()) return false; else { pickBeeper(); if (nextToABeeper()) { putBeeper(); return true; } { putBeeper();

24 Simplify – bottom factoring
if ( facingSouth() ) { turnLeft(); move(); } else turnRight();

25 Simplify – top factoring
if ( beeperOnLeft() ) { move(); turnLeft(); } else turnRight();

26 Problems p.25 #2-5, 7, 8

27 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)

28 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.

29 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.

30 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)

31 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.

32 Boolean Operators Pascal (and, or, not) C++/ Java (&&, ||, !)

33 AND operator operates on two boolean values (ie, frontIsClear, etc)
will evaluate to true if both of the values are true. false otherwise “Truth table” p q p and q t t t f f t f f

34 AND operator operates on two boolean values (ie, frontIsClear, etc)
will evaluate to true if both of the values are true. false otherwise “Truth table” p q p and q t t t t f f f t f f f f

35 OR operator 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” p q p or q t t t f f t f f

36 OR operator 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” p q p or q t t t t f t f t t f f f

37 isBeepOnLeftANDisBeepOnRight()
Write a predicate which returns true if there is at least one beeper on both sides, false otherwise.

38 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;

39 isBeepOnLeftORisBeepOnRight()
Write a predicate which returns true if there is at least one beeper on both sides, false otherwise.

40 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 false;

41 #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.

42 #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.


Download ppt "Chapter 5 Conditionally Executing Instructions"

Similar presentations


Ads by Google