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 computers From home, or in lab use UB Virtual Computing Lab We will go over this in lab Guide available at http://www.buffalo.edu/ubit/serviceguides/software/my-virtual-computinglab.html Linked from course website Karel’s World Plain made of streets & avenues Corners Describe intersection of streets and avenues Referenced by avenue number street number Avenue & street numbers Positive integers 1st avenue & Locations Relative Absolute 1st street Streets Origin 12 11 10 9 8 7 6 5 4 3 2 1 N W E S 1 2 3 4 5 6 7 8 9 10 11 12 Avenues Karel’s World Walls Made of neutronium Obstacles Karel cannot pass through a wall Locations Between streets Between avenues Form boundaries on west side & south side Karel’s World Beepers Pinging beeper Situated at intersections Can be picked up carried put down Karel’s Capabilities Can Karel the Robot think? Take a look at the path following example Karel navigates a maze The configuration of the maze is unknown to Karel The end of the maze is determined by a single beeper placed at the exit Karel’s Capabilities Karel can move forward turn in place Karel knows which direction it is facing Karel is equipped with three cameras Cameras face forward right left Range 1/2 block Karel’s Capabilities Karel can hear Can detect a beeper on the same corner Karel is standing Karel’s Beeper Bag Stores beepers Karel can put beepers in the bag and remove beepers from the bag Soundproof Karel 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 language Defined by vocabulary punctuation marks rules 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 world Includes Size & location of each wall Location & number of beepers Karel’s location & direction Karel is facing Initial Situation Karel’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 Situation Karel’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 Instructions move turnleft pickbeeper putbeeper turnoff They allow Karel to move through world & handle beepers Provide 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 Position move Karel moves forward one block Karel remains facing in the same direction If a wall is in Karel’s way & a move instruction is executed, Karel shuts off Error shutoff turnleft Karel pivots 90 degrees to left Location does not change Cannot cause error shutoff Why not? There is no turnright instruction How does Karel turn right? Primitive Instruction Details Handling Beepers pickbeeper 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 up Note that there may be multiple beepers on a corner What if no beeper exists on the corner? Error shutoff putbeeper Karel takes a beeper out of beeper bag and places beeper on corner What if there are not any beepers in the bag? Error shutoff Primitive Instruction Details Handling Beepers pickbeeper 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 up Note that there may be multiple beepers on a corner What if no beeper exists on the corner? Error shutoff putbeeper Karel takes a beeper out of beeper bag and places beeper on corner What if there are not any beepers in the bag? Error shutoff Primitive Instruction Details Completing a Task turnoff Karel must be shut down after task is completed Last instruction in every program Program Structure General Structure BEGINNING-OF-PROGRAM BEGINNING-OF-EXECUTION Instructions, all but last ending with a semicolon (;) END-OF-EXECUTION END-OF-PROGRAM Executing a Program Instructions between BEGINNING-OFEXECUTION 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 program Simulate Systematically execute program just as Karel would perform each instruction Trace Step through program, maintaining each step Verification Simulation & tracing through program to ensure that it works properly Using Karel the Robot in Lab Enter program Compile program Checks for syntax errors, getting program ready to run Setup initial world Initialize world Execute program Run Execution begins Ends with either: a proper shutdown (turnoff instruction) a runtime error Trace Step Unstep Saving Your Work Programs and worlds can be saved! Sample Program Task Pick up beeper located at intersection of avenue 3, and street 4 Karel is at the intersection of avenue 1 and street 1 facing north Beeper placed at intersection of avenue 3 and street 4 Streets Initial World 12 11 10 9 8 7 6 5 4 3 2 1 N W E S Beeper 1 Karel initially has zero beepers in beeper bag Karel’s Initial Position (facing north) 1 2 3 4 5 6 7 8 9 10 11 12 Avenues Sample Program Program BEGINNING-OF-PROGRAM BEGINNING-OF-EXECUTION turnleft; turnleft; turnleft; move; move; turnleft; move; move; move; pickbeeper; turnoff END-OF-EXECUTION END-OF-PROGRAM Sample Program Streets Final World 12 11 10 9 8 7 6 5 4 3 2 1 N W E S Karel’s Final Position Karel ends up with one beeper in the beeper bag 1 2 3 4 5 6 7 8 9 10 11 12 Avenues 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
© Copyright 2026 Paperzz