Download presentation
Presentation is loading. Please wait.
Published byJasmine Walsh Modified over 8 years ago
2
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions
3
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.
4
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!
5
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.
6
5 The General Form Of An if if ( ) { // otherwise known as the // “then” instruction } Note the indentation, the curly braces, and no semicolon after ( ). There will be semicolons in the instruction list.
7
6 How does it work? 1. Robot checks to see if is true or false based on the robot’s current situation 2. If the is true, the robot executes the 3. If the is false, the robot skips the. Note about : The state of the robot doesn’t change, but the sender of the message will obtain information about the state of the robot.
8
7 New Class of Robot class Robot extends ur_Robot {boolean frontIsClear(); boolean nextToABeeper(); boolean nextToARobot(); boolean facingNorth(); boolean facingSouth(); boolean facingEast(); boolean facingWest(); boolean anyBeepersInBeeperBag(); } Use these method names as the condition
9
8 Class Robot Inherits all instructions from ur_Robot 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)
10
9 Example if (Karel.nextToABeeper()) { Karel.pickBeeper(); } Karel.turnLeft(); 1
11
10 Examples if ( karel.frontIsClear() ) { karel.move();// no danger of hitting wall } if ( karel.anyBeepersInBeeperBag() ) { karel.putBeeper();// no danger of error }
12
11 Negative conditions? Suppose we want a negative form of a predicate? We can precede a predicate with the negative operator ! if (! Karel.frontIsClear()) { Karel.turnLeft(); } Karel.move();
13
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.
14
13 Example class Checker_Robot extends Robot { // insert constructor here boolean frontIsBlocked() { return ! frontIsClear(); } boolean not_nextToABeeper() { return ! nextToABeeper(); }
15
14 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
15 The General Form Of An if/else if ( ) { } else { } Used when you have to execute one of two alternatives
17
16 How does it work? 1. Robot checks to see if is true or false based on the robot’s current situation 2. If the is true, the robot executes the 3. If the is false, the robot executes the.
18
17 Extending Robot class BiggerBrains extends Robot { // constructor here boolean beeperIsToLeft() {…} boolean twoBeepersOnCornerOrMore() {…} void faceEast() {…} }
19
18 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
19 boolean twoBeepersOnCornerOrMore() note: you may have to nest if statements you write the predicate
21
20 void faceEast() you write
22
21 IF – ELSE Simplifications simplify: if ( frontIsClear() ) { return true; } else { return false; }
23
22 Simplify if ( ! leftIsBlocked() ) { return true; } else { return false; }
24
23 Simplify if ( leftIsBlocked() ) { return false; } else { return true; }
25
24 Simplify – bottom factoring if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); }
26
25 Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); }
27
26 Problems p.25 #1-5, 7, 8
28
27 Let’s write some programs
29
28 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.
30
29 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. Require the robot to jump if, and only if, faced with a hurdle.
31
30 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.
32
31 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)
33
32 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.
34
33 Complex Carpet Layer #12. 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.
35
34 Boolean Operators Pascal (and, or, not) C++/ Java (&&, ||, !) Karel J (! only)
36
35 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” pqp and qt t f f tf
37
36 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” pqp and q tt t t f f f t f ff f
38
37 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” pqp or qt t f f tf
39
38 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” pqp or q tt t t f t f t t ff f
40
39 Creating “And” & “Or” They don’t really exist – so let’s write our own.
41
40 isBeepOnLeft_AND_isBeepOnRight() Write a predicate which returns true if there is at least one beeper on both sides, false otherwise.
42
41 isBeepOnLeft_AND_isBeepOnRight() Write a predicate which returns true if there is at least one beeper on both sides, false otherwise. boolean isBeepOnLeft_AND_isBeepOnRight() { if (isBeepOnLeft()) if (isBeepOnRight()) { return true; } return false; }
43
42 isBeepOnLeft_OR_isBeepOnRight() Write a predicate which returns true if there is at least one beeper on both sides, false otherwise.
44
43 isBeepOnLeft_OR_isBeepOnRight() Write a predicate which returns true if there is at least one beeper on both sides, false otherwise. boolean isBeepOnLeft_OR_isBeepOnRight() { if (isBeepOnLeft()) { return true; } if (isBeepOnRight()) { return true; } return false; }
45
44 #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.
46
45 #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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.