1 karel j. robot chapter 5 conditionally executing instructions

42
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

Upload: aubrey-ward

Post on 11-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

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

1

Karel J. Robot

Chapter 5

Conditionally Executing

Instructions

Page 2: 1 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.

Page 3: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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!

Page 4: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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.

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

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.

Page 6: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

6

How does it work?

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

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

Page 7: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

7

New Class of Robotpublic 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

Page 8: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

8

Class Robot

Inherits all instructions from UrRobotWe will use the Robot class as our base robot from now onThe 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)

Page 9: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

9

Example

if (nextToABeeper())

{ pickBeeper();

}

turnLeft();

1

Page 10: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

10

Examples

if (frontIsClear() )

{

move(); // no danger of hitting wall

}

if ( anyBeepersInBeeperBag() ){

putBeeper(); // no danger of error}

Page 11: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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

Page 12: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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.

Page 13: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

13

Example

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

public boolean notNextToABeeper() { return ! nextToABeeper(); }}

Page 14: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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

Page 15: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

15

How does it work?

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

Page 16: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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.

Page 17: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

17

Extending Robot

public class BiggerBrains extends Robot{ // constructor here

public boolean beeperIsToLeft() {…}

public void faceEast() {…}

public boolean twoBeepersOnCornerOrMore() {…}

}

try writing these predicates

Page 18: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

18

public boolean beeperIsToLeft()

{

Page 19: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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

Page 20: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

20

public void faceEast()

Page 21: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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

}}

Page 22: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

22

public boolean twoBeepersOnCornerOrMore()

{

Page 23: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

23

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

Page 24: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

24

Simplify – bottom factoring

if ( facingSouth() )

{

turnLeft();

move();

}

else

{

turnRight();

move();

}

Page 25: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

25

Simplify – top factoring

if ( beeperOnLeft() ){

move();turnLeft();

}else{

move();turnRight();

}

Page 26: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

26

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

Page 27: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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)

Page 28: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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.

Page 29: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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.

Page 30: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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)

Page 31: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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.

Page 32: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

32

Boolean Operators

Pascal (and, or, not)

C++/ Java (&&, ||, !)

Page 33: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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 qt t t f f t f f

Page 34: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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 qt t t t f f f t f f f f

Page 35: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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 qt t t f f t f f

Page 36: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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 qt t t t f t f t t f f f

Page 37: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

37

isBeepOnLeftANDisBeepOnRight()

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

Page 38: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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;

}

Page 39: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

39

isBeepOnLeftORisBeepOnRight()

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

Page 40: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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

}

return false;

}

Page 41: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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.

Page 42: 1 Karel J. Robot Chapter 5 Conditionally Executing Instructions

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.