CSE111 Spring 2009 H Kershner Karel the Robot Extending the Primitive Commands We know how to write programs using Karel s primitive commands move turnleft pickbeeper putbeeper turnoff We know how to navigate between Karel s World view Karel s Program view and Karel s Execution or Run view We know how to Compile a program which means to translate it from Karel s programming language which humans can understand as well as Karel into machine code that the computer inside Karel understands AND we immediately saw that writing programs with only the basic 5 primitives is very tedious It is hard to keep track of exactly what we are asking Karel to do We have already seen how Karel can to go out into the world and retrieve beepers while going around obstacles in its path In this example Karel is to go out into the world and retrieve the beepers found in two boxes bring the beepers home and deposit them at the origin The World view looked something like this As we saw in our first example it can be painful to keep track of all the move instructions and all the turnleft commands This is true even if we are using two screen images of Karel one for the World and one for the program In this lesson we will learn how we can simplify our coding making it easier for the programmer to write and to understand Karel will still only understand the five basic commands but we can expand Karel s vocabulary well sort of We began the programming process with a problem statement Problem Statement Karel s world contains two boxes each with one beeper Go out into Karel s world retrieve the beepers from the boxes return to the origin take beepers out of beeper bag Copyright 2008 by Helene G Kershner CSE111 Spring 2009 H Kershner Karel the Robot Extending the Primitive Commands We then defined the end result of the program Output and the starting state of our program Input Define the Output Return to Origin with two beepers in beeper bag Place two beepers at Origin Define the Input Start at Origin facing North There are NO beepers in the beeper bag Working with a problem statement output and input we defined the initial version of our algorithm our initial solution to Karel s problem Karel starts at origin with no beepers Move Karel until one block North of first beeper box Turn right Move to above first beeper box Turn to face down Pick up beeper Turn around Move one block Turn left Move 1 block Turn right Move 2 blocks Turn right Move 1 block Turn to face down Move one block Pick up beeper Turn around Move one block Go back to origin Put down two beepers Turn off Looking over this algorithm we recognized that few if any of these statements were at a level that Karel could understand Our next task was to translate the initial algorithm into Karel s primitive commands Our initial program looked something like this beginning of program beginning of execution move move move turnleft turnleft turnleft move move turnleft turnleft turnleft move pickbeeper turnleft turnleft move turnleft Copyright 2008 by Helene G Kershner CSE111 Spring 2009 H Kershner Karel the Robot Extending the Primitive Commands move turnleft turnleft turnleft move move turnleft turnleft turnleft move turnleft turnleft turnleft move pickbeeper turnleft turnleft move turnleft move move turnleft move move move move move turnleft turnleft putbeeper putbeeper turnoff end of execution end of program While this certainly solved Karel s problem statement the constant repetition of commands to do simple tasks such as turn right or turn around made the process very tedious Defining New Instructions Since Karel is very good at following instructions his designers included the concept of creating new instructions which simplifies the program for his human handlers but from Karel s perspective still uses just the 5 basic instructions Here is the structure of a Karel program Notice that Karel s basic instructions are located between the Beginning of Execution and End of Execution The definitions of new terms are placed between the Beginning of program and the Beginning of Execution BEGINNING OF PROGRAM Definitions BEGINNING OF EXECUTION Instructions END OF EXECUTION END OF PROGRAM Copyright 2008 by Helene G Kershner CSE111 Spring 2009 H Kershner Karel the Robot Extending the Primitive Commands Creating a new instruction is not a difficult task and it is not complex It does require care and attention to detail The benefit is that the program can be much more natural and easy to understand To create a new instruction for Karel we use this command DEFINE NEW INSTRUCTION new name here AS begin instruction s end The begin and end indicate where the definition starts and finishes For every begin there must be a corresponding end Let s try a simple new definition DEFINE NEW INSTRUCTION turnright AS begin turnleft turnleft turnleft end Our Karel programming language now contains the five primitives and the new instruction turnright The following version of Karel s two box program uses turnright instead of three turnleft commands beginning of program define new instruction turnright as begin turnleft turnleft turnleft end beginning of execution move move move turnright move move turnright move pickbeeper turnleft turnleft move turnleft move turnright move move turnright move turnright move pickbeeper Copyright 2008 by Helene G Kershner CSE111 Spring 2009 H Kershner Karel the Robot Extending the Primitive Commands turnleft turnleft move turnleft move move turnleft move move move move move turnleft turnleft putbeeper putbeeper turnoff end of execution end of program Not only is this program shorter than the earlier version but it is easier to understand Definitions are not standardized The programmer can create them however the programmer chooses For example the programmer might have decided to define the term turn 180 which means turn around and then use turn 180 to define turnaround The basic program would be unchanged but the definitions section would contain two definitions one of which uses the other This is perfectly legal The definitions section would look like beginning of program define new instruction turn 180 as begin turnleft turnleft end define new instruction turnright as begin turn 180 turnleft end beginning of execution Notice that the definition of turnright makes use of the turn 180 command The most important concept here is that a definition must be defined before it is used So turn 180 had to be defined before turnaround Using the turn 180
View Full Document
Unlocking...