cse 111 karel the robot' thanks to dr. kris schindler for this (and all karel the robot slides)
TRANSCRIPT
CSE 111
Karel the Robot'
Thanks to Dr. Kris Schindler for this (and all Karel the Robot slides)
Logging into Karel
Available on all cybrary computersFrom home, or in lab use UB Virtual
Computing LabWe will go over this in lab
Guide available at http://www.buffalo.edu/ubit/service-guides/software/my-virtual-computing-lab.html
Linked from course website
Karel’s World
Plain made of streets & avenuesCorners
Describe intersection of streets and avenuesReferenced by
avenue numberstreet number
Avenue & street numbersPositive integers
Origin1st avenue & 1st street
LocationsRelativeAbsolute
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12N
SW E
Av en u e s
Stre
ets
Karel’s World
WallsMade of neutroniumObstacles
Karel cannot pass through a wall
LocationsBetween streetsBetween avenues
Form boundaries on west side & south side
Karel’s World
BeepersPinging beeperSituated at intersectionsCan be
picked upcarriedput down
Karel’s Capabilities
Can Karel the Robot think?Take a look at the path following example
Karel navigates a mazeThe configuration of the maze is unknown to KarelThe end of the maze is determined by a single beeper placed at
the exit
Karel’s Capabilities
Karel canmove forwardturn in place
Karel knowswhich direction it is facing
Karel is equipped with three cameras
Cameras faceforwardrightleft
Range1/2 block
Karel’s Capabilities
Karel can hearCan detect a beeper on the same corner Karel is
standing
Karel’s Beeper BagStores beepers
Karel can put beepers in the bag and remove beepers from the bag
SoundproofKarel cannot hear beepers in the bag
Karel’s Tasks
How does Karel know what to do?Karel can follow a detailed set of instructions
Called a program
The language Karel understands is called a programming languageDefined by
vocabularypunctuation marksrules of grammar
Simple, yet powerful
Tasks & Situations
What is a task?Something we want Karel to do
What is a situation?Description of Karel’s worldIncludes
Size & location of each wallLocation & number of beepersKarel’s location & direction Karel is facing
Initial SituationKarel’s world at beginning of task
Includes Karel’s initial location, direction Karel is facing, and the number of beepers in the beeper bag
Final SituationKarel’s world after task is completed
Includes Karel’s final location, direction Karel is facing, and the number of beepers in the beeper bag
Primitive Instructions
Instructionsmoveturnleftpickbeeperputbeeperturnoff
They allow Karel to move through world & handle beepersProvide for avoiding obstacles & transporting
beepers
Program & Instruction Execution
An instruction is executed when Karel performs the action associated with the instruction
A program is executed when the instructions in a program are carried out
Primitive Instruction Details
Changing Positionmove
Karel moves forward one blockKarel remains facing in the same directionIf a wall is in Karel’s way & a move instruction is
executed, Karel shuts off Error shutoff
turnleftKarel pivots 90 degrees to leftLocation does not changeCannot cause error shutoff
Why not?
There is no turnright instructionHow does Karel turn right?
Primitive Instruction Details
Handling Beeperspickbeeper
Karel picks up a beeper from the corner on which it is standing and deposits the beeper in the beeper bag
Only one beeper is picked upNote that there may be multiple beepers on a corner
What if no beeper exists on the corner?Error shutoff
putbeeperKarel takes a beeper out of beeper bag and places beeper
on cornerWhat if there are not any beepers in the bag?
Error shutoff
Primitive Instruction Details
Handling Beeperspickbeeper
Karel picks up a beeper from the corner on which it is standing and deposits the beeper in the beeper bag
Only one beeper is picked upNote that there may be multiple beepers on a corner
What if no beeper exists on the corner?Error shutoff
putbeeperKarel takes a beeper out of beeper bag and places beeper
on cornerWhat if there are not any beepers in the bag?
Error shutoff
Primitive Instruction Details
Completing a Taskturnoff
Karel must be shut down after task is completedLast instruction in every program
Program Structure
General Structure
BEGINNING-OF-PROGRAMBEGINNING-OF-EXECUTION
Instructions, all but last ending with a semicolon (;)
END-OF-EXECUTION
END-OF-PROGRAM
Executing a Program
Instructions between BEGINNING-OF-EXECUTION and END-OF-EXECUTION sequentially executed until a turnoff instruction or error shutoff is encountered.
Since Karel is a cyber robot, we simulate & trace the programSimulate
Systematically execute program just as Karel would perform each instruction
TraceStep through program, maintaining each step
VerificationSimulation & tracing through program to ensure that it
works properly
Using Karel the Robot in Lab
Enter programCompile programChecks for syntax errors, getting program ready to
run
Setup initial worldInitialize worldExecute programRun
Execution beginsEnds with either:
a proper shutdown (turnoff instruction)a runtime error
TraceStepUnstep
Saving Your Work
Programs and worlds can be saved!
Sample Program
TaskPick up beeper located at intersection of avenue 3,
and street 4
Initial WorldKarel is at the intersection of
avenue 1 and street 1 facing north
Beeper placed at intersection of avenue 3 and street 4
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
1K arel’s In itia l
P osition(fac in g n orth )
K arel in itia llyh as zero be epers
in bee per bag
B e eper
Av en u e s
Stre
ets
N
SW E
Sample Program
ProgramBEGINNING-OF-PROGRAM
BEGINNING-OF-EXECUTION
turnleft;
turnleft;
turnleft;
move;
move;
turnleft;
move;
move;
move;
pickbeeper;
turnoff
END-OF-EXECUTION
END-OF-PROGRAM
Sample Program
Final World
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
K arel’s F in a lP osition
K arel e nds u pw ith on e beepe r
in th e beeper bag
Av en u e s
Stre
ets
N
SW E
References
Richard E. Pattis (revised by Jim Roberts & mark Stehlik), Karel the Robot, John Wiley & Sons, Inc., 2nd edition, 1995, pp 1-11
Your Task in Lab
Run tutorial, learning how to program using Karel the Robot