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

Slides:



Advertisements
Similar presentations
1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Advertisements

Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 of 3 Karel Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Copyright, Joseph Bergin
Nested If Statements While Loops
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps.
CMPUT 101 Lab # 2 September 17, :00 – 16:50.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright.
Karel The Robot In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
Conditionals How do we solve tasks in which every particular of a task is not specifically known? – A robot needs the ability to survey its immediate environment.
Polymorphism Are there different ways to solve the Harvester problem? – Robot teams – instead of one robot to solve a problem, let’s get a team of robots.
Karel as a Turing Machine CSE /03/04. Facts Any programming language which satisfies Boehm & Jacopini's conditions can be expressed by means of.
Robot? What’s a Robot? Introducing Karel-the-Robot.
Karel JRobot Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Chapter 5 Conditionally Executing Instructions
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Karel the Robot A Gentle Introduction to the Art of Programming.
Programming Errors Lexical errors – occur whenever Karel reads a word that is not in his vocabulary. Example in English: We are asking directions and instead.
Thanks to Dr. Kris Schindler for this (and all Karel the Robot slides)
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
Chapter 5.  We’ve been using UrRobot – knows only 5 things  Let’s now use a new type of Robot: Robot!  i.e. – public class MileMover extends Robot.
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Karel J. Robot Chapter 6 Instructions That Repeat.
1 Chapter 5 - IF CH5 – Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Karel J Robot Chapter 5.
1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)
Karel J. Robot Chapter 6 Instructions That Repeat.
CS 106A, Lecture 3 Problem-solving with Karel
Karel – Primitive Instructions
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
In the beginning… software
Eric Roberts and Jerry Cain
Copyright © 2008 by Helene G. Kershner
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
Karel J Robot.
karel_part4_functions_2
Karel – Primitive Instructions
Karel J Robot.
CS 106A, Lecture 2 Programming with Karel
Algorithm The key is the step-by-step instructions.
Karel the Robot – Making Decisions
SSEA Computer Science: CS106A
slides courtesy of Eric Roberts
CH5 – Conditional Statements
Algorithm The key is the step-by-step instructions.
Nested If Statements While Loops
CSE 111 Karel the Robot.
Karel – Primitive Instructions
Presentation transcript:

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

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

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

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

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

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

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

Karel the Robot – Making Decisions Problem Statement: Karel is required to jump over a long row of hurdles picking up beepers placed between the hurdles as Karel finds them. The beepers are randomly placed and Karel cannot be sure the beepers will be in the same place the next time Karel goes through the hurdles. Karel will come home and face North. Define Output: Come home after going over the hurdles with all the beepers found after the hurdles in the beeper bag. Define Input: Karel has no beepers in his beeper bag when he starts out. Karel starts out facing North. Copyright © 2008 by Helene G. Kershner

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

Karel the Robot – Making Decisions Initial Algorithm Move to 6 th street Turn right Jump over the hurdle Check if beeper If beeper pick it up Jump over the hurdle Check if beeper If beeper pick it up Jump over the hurdle Check if beeper If beeper pick it up Jump over the hurdle Check if beeper If beeper pick it up Jump over the hurdle... After all hurdles are jumped Turn right Go home Turn around Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Check if beeper If next to beeper, pick it up IF next-to-a-beeper THEN BEGIN pickbeeper; END; Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Revised Algorithm Move to 6 th street Turn right Jump over the hurdle If next to beeper, pick it up Jump over the hurdle If next to beeper, pick it up Jump over the hurdle If next to beeper, pick it up Jump over the hurdle If next to beeper, pick it up Jump over the hurdle... After all hurdles are jumped Turn right Go to 4 th street Go to 1 st avenue Move to Origin (home) Turn around Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions beginning-of-program define-new-instruction turnright as begin turnleft; end; beginning-of-execution iterate 5 times begin move; end; turnright; move; turnright; move; if next-to-a-beeper then begin pickbeeper; end; turnleft; turnleft; move; turnright; move; turnright; move; if next-to-a-beeper then begin pickbeeper; end; turnleft; turnleft; move; turnright; move; turnright; move; if next-to-a-beeper then begin pickbeeper; end; turnleft; turnleft; …….. REPEAT A BUNCH BRING KAREL HOME Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions beginning-of-program define-new-instruction turnright as begin turnleft; end; beginning-of-execution iterate 5 times begin move; end; turnright; move; turnright; move; if next-to-a-beeper then begin pickbeeper; end; turnleft; turnleft; move; turnright; move; turnright; move; if next-to-a-beeper then begin pickbeeper; end; turnleft; turnleft; move; turnright; move; turnright; move; if next-to-a-beeper then begin pickbeeper; end; turnleft; turnleft; move; turnright; REPEAT A BUNCH BRING KAREL HOME Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions beginning-of-program define-new-instruction turnright as begin turnleft; end; define-new-instruction turnaround as begin turnleft; end; beginning-of-execution iterate 5 times begin move; end; turnright; move; iterate 12 times begin move; turnright; move; if next-to-a-beeper then begin pickbeeper; end; turnaround; move; turnright; end; BRING KAREL HOME turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Problem Statement: Karel starts at the Origin and travels through a simple handed maze turning right at each corner of the maze. Karel 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

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.

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

Karel the Robot – Making Decisions beginning-of-program define-new-instruction turnright as begin turnleft; end; define-new-instruction move5 as begin iterate 5 times begin move; end; beginning-of-execution turnright; move; turnleft; move; move5; turnright; move5; turnright; move5; turnright; move5; turnleft; move; turnright; move; turnright; turnoff; end-of-execution end-of-program

Karel the Robot – Making Decisions What instruction can we use in our program that will require that Karel check whether a move is possible? Look at the Maze, regardless of its size when Karel reaches the corner, Karel makes a right turn. But how does Karel know if Karel is at the corner? An If statement = Decision What question, using an IF can Karel ask to determine whether a corner has been reached?

Karel the Robot – Making Decisions Remember: To make decisions, Karel uses the IF statement. IF THEN BEGIN ; END; These are the 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

Karel the Robot – Making Decisions S0, in our example, what question, what test condition can we use to have Karel decide if Karel is at a corner? front-is-clear front-is-blocked left-is-clear left-is-blocked right-is-clear right-is-blocked next-to-a-beeper not-next-to-a-beeper any-beepers-in-beeper-bag no-beepers-in-beeper-bag

Karel the Robot – Making Decisions You can write your program using any of the following: front-is-clear front-is-blocked left-is-clear left-is-blocked right-is-clear right-is-blocked next-to-a-beeper not-next-to-a-beeper any-beepers-in-beeper-bag no-beepers-in-beeper-bag

Karel the Robot – Making Decisions You can write your program using any of the following: front-is-clear, front-is-blocked, left-is-clear, left-is-blocked, right-is-clear, right-is-blocked Depending on which one you choose, your IF statement will be different and some of your ending instructions might be different but the basic idea for the program will be the same. If front-is-clear then If front-is-blocked then begin move; turnright; end; turnright; move;

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

Karel the Robot – Making Decisions What if we know that Karel is working on a 4-sided maze where Karel will always need to turn right, BUT we do NOT know in advance how long each side is? Our program work only and exactly when the maze is five blocks in each direction. How can we design our program (code) so that it works for any right-handed, 4-sided maze? The goal in writing a program is to make it general enough to solve any similar problem.

Karel the Robot – Making Decisions 3-squares/wide 5-squares/wide 7-squares/wide

Karel the Robot – Making Decisions We could use the same program rewriting it each time to reflect the different number of squares per side on each maze. BUT, Doing that would mean every time we have a different maze we would need a different program and the iterate command that moves Karel along the side would change. iterate 4 times begin if front-is-blocked then begin turnright; end; iterate 5 times begin move; end;

Karel the Robot – Making Decisions While this was easy to do, it does not give us code that works for any right turning maze. We, the programmer would need to write, essentially, a new program for each new maze. BUT, we CAN design Karel programs so that Karel CAN do all the work of deciding when to turn without our having to know how long each side is. Karel is a computer. Karel does not get bored. Karel does not get tired asking the same question over and over and over and over and….. again

Karel the Robot – Making Decisions Let’s examine how Karel asks more complex questions. We will return to our simple Maze problem in a moment.

Karel the Robot – Making Decisions Problem Statement: Karel has been told to place two beepers on each street corner between 1st street and 2nd avenue and 1st street and 10th avenue and return home. This would be a very easy task except there was a beeper party last night and there are beepers scattered around on random street corners. Define Output: There will be two beepers on every corner on 1st street between 2nd avenue and 10th avenue. Karel will be at the origin. Define Input: Karel is next to 20 beepers. He is at the Origin facing east. Copyright © 2008 by Helene G. Kershner

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

Karel the Robot – Making Decisions Initial Algorithm Pickup 18 beepers Move If next to a beeper, put a beeper down Otherwise put down 2 beepers Move If next to a beeper, put a beeper down Otherwise put down 2 beepers Move If next to a beeper, put a beeper down Otherwise put down 2 beepers... turnaround move 9 streets turnoff Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Revised Algorithm Pickup 18 beepers Repeat 9 times Move If next to a beeper, put a beeper down Otherwise put down 2 beepers turnaround move 9 streets turnoff Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions IF / THEN / ELSE IF THEN BEGIN ; END ELSE BEGIN ; END; Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Looking at our revised algorithm, We have the statement: If next to a beeper, put a beeper down Otherwise put down 2 beepers Anytime we use a word like OTHERWISE in English an IF/THEN/ELSE is used in Karel’s world. Translating our algorithm into Karel’s language would produce: IF next-to-a-beeper THEN BEGIN putbeeper; END ELSE (another word for Otherwise) BEGIN putbeeper; END; Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions Looking at Karel’s code IF next-to-a-beeper THEN BEGIN putbeeper; END ELSE (another word for Otherwise) BEGIN putbeeper; END; 1) When Karen is next-to-a-beeper (the test is TRUE), one beeper get put down. 2) When Karen is NOT next-to-a-beeper (the test is FALSE), two beepers get put down. 3) BEGIN / END statements must match up or Karel (not to mention the programmer) gets very confused. Copyright © 2008 by Helene G. Kershner

Karel the Robot – Making Decisions beginning-of-execution ITERATE 18 TIMES BEGIN pickbeeper; END; ITERATE 9 TIMES BEGIN move; IF next-to-a-beeper THEN BEGIN putbeeper; END ELSE BEGIN putbeeper; END; turnaround; ITERATE 9 TIMES BEGIN move; END; turnoff; end-of-execution HINT: This code is similar to what is needed to solve your 2 nd Project. Copyright © 2008 by Helene G. Kershner