Presentation is loading. Please wait.

Presentation is loading. Please wait.

Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.

Similar presentations


Presentation on theme: "Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright."— Presentation transcript:

1 Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright © 2008 by Helene G. Kershner

2 Karel the Robot – Review Karel’s programs have a standard format. beginning-of-program Put the definition of new instruction here beginning-of-execution Primitives Use newly defined instructions turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner

3 Karel the Robot – Review Karel can “learn” new words by using the DEFINE-NEW-INSTRUCTION AS BEGIN Instructions from primitives or; Instructions from other new instructions listed above this one; END; Copyright © 2008 by Helene G. Kershner

4 Karel the Robot – Review For Example: define-new-instruction Turnright as begin turnleft; end; Copyright © 2008 by Helene G. Kershner

5 Karel the Robot – Review Karel can repeat instructions by using the Iterate Command. ITERATE TIMES Begin ; End; For Example: Iterate 5 times begin move; end; Copyright © 2008 by Helene G. Kershner

6 Karel the Robot – Making Decisions “Teaching” Karel to Make Decisions To make decisions, Karel uses the IF statement. Basically, Karel can determine if a limited set of conditions is True or False. (pp 66-67 of text) front-is-clearfront-is-blocked left-is-clearleft-is-blocked right-is-clearright-is-blocked next-to-a-beepernot-next-to-a-beeper any-beepers-in-beeper-bagno-beepers-in-beeper-bag Copyright © 2008 by Helene G. Kershner

7 Karel the Robot – Making Decisions What does an IF statement look like? IF THEN BEGIN ; END; For Example: If front-is-clear THEN begin move; end; Copyright © 2008 by Helene G. Kershner

8 Karel the Robot – Making Decisions Problem Statement: Karel has 5 beepers in his beeper bag. His task is to make sure that the five corners between 3rd Street and 3rd Avenue and 3rd Street and 7th Avenue all have one beepers. Two of these corners already have beepers on them. Define Output: There must be a beeper at every corner on 3rd Street between 3rd Avenue and 7th Avenue. Define Input: Karel has to put 5 beepers in his bag. He is located on 3rd Street and 1st Avenue facing East. Copyright © 2008 by Helene G. Kershner

9 Karel the Robot – Making Decisions Copyright © 2008 by Helene G. Kershner

10 Karel the Robot – Making Decisions Initial Algorithm Pickup 5 beepers Move 2 avenue blocks Check if beeper If no beeper, put one down Move 1 avenue block Check if beeper If no beeper, put one down Move 1 avenue block Check if beeper If no beeper, put one down Move 1 avenue block Check if beeper If no beeper, put one down Move 1 avenue block Check if beeper If no beeper, put one down Copyright © 2008 by Helene G. Kershner

11 Karel the Robot – Making Decisions Check if beeper If no beeper, put one down IF not-next-to-a-beeper THEN BEGIN putbeeper; END; Copyright © 2008 by Helene G. Kershner

12 Karel the Robot – Making Decisions Revised Algorithm Pickup 5 beepers Move 2 avenue blocks If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Copyright © 2008 by Helene G. Kershner

13 Karel the Robot – Making Decisions beginning-of-program beginning-of-execution pickbeeper; move; IF not-next-to-a-beeper THEN begin putbeeper; end; move; IF not-next-to-a-beeper THEN begin putbeeper; end; move; IF not-next-to-a-beeper THEN begin putbeeper; end; move; IF not-next-to-a-beeper THEN begin putbeeper; end; move; IF not-next-to-a-beeper THEN begin putbeeper; end; move; turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner

14 Karel the Robot – Making Decisions beginning-of-program beginning-of-execution iterate 5 times begin pickbeeper; end; move; iterate 5 times begin if not-next-to-a-beeper then begin putbeeper; end; move; end; turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner

15 Karel the Robot – Making Decisions Problem Statement: Karel starts at the Origin and travels through a simple right-handed maze and returns back to the Origin facing North. Define Output: Karel ends at the Origin facing North. Define Input: Karel starts at the Origin facing North, the maze is above and to his right. Copyright © 2008 by Helene G. Kershner

16 Karel the Robot – Making Decisions Looking at the maze we know: 1. Each side is 5 spaces long This is a good place to use the Iterate Command. 2. Each turn is to the right Karel can check to see if he is in the corner. This is a good place for the If – Then Command. 3. Everything is repeated 4 times This is another good place for the Iterate Command.

17 Karel the Robot – Making Decisions Initial Algorithm move Karel to entrance of maze go up first side at corner turn right go up second side at corner turn right go up third side at corner turn right go up fourth side at opening turn left move home turn right

18 Karel the Robot – Making Decisions Revised Algorithm move Karel to entrance of maze Repeat 4 times go up side of maze If at corner turn right at opening turn left move home turn right It turns out that in Karel’s world it is better to test if he is at the corner before he moves.

19 Karel the Robot – Making Decisions beginning-of-program define-new-instruction turnright as begin turnleft; end; beginning-of-execution turnright; move; turnleft; move; iterate 4 times begin if front-is-blocked then begin turnright; end; iterate 5 times begin move; end; turnleft; move; turnright; move; turnright; turnoff; end-of-execution end-of-program


Download ppt "Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright."

Similar presentations


Ads by Google