karel the robot a gentle introduction to the art of programming

43
Karel the Robot A Gentle Introduction to the Art of Programming

Upload: ashlie-turner

Post on 31-Dec-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Karel the Robot A Gentle Introduction to the Art of Programming

Karel the Robot

A Gentle Introduction to the Art of Programming

Page 2: Karel the Robot A Gentle Introduction to the Art of Programming

Schools Using Karel

•UC-Berkeley

•U. of Washington

•Air Force Academy

•Westpoint (USMA)

•Carnegie-Mellon

•Purdue

•Stanford

Page 3: Karel the Robot A Gentle Introduction to the Art of Programming

What is Karel the Robot?

• Karel is essentially a programmable cursor that can move across the flat world of a CRT screen.

• Shown on the screen is a grid work of vertical and horizontal lines, representing avenues and streets.

Page 4: Karel the Robot A Gentle Introduction to the Art of Programming

Karel’s Capabilities

• Karel is restricted to moving from street corner to street corner, one such move at a time.

• Karel can pivot 90 degrees to the left when requested.

• Karel can only face North, South, East, or West.

Page 5: Karel the Robot A Gentle Introduction to the Art of Programming

Beepers

• To add variety to Karel’s environment, Karel’s designer added beepers, flashing symbols that Karel can detect (we say he “hears” them), pick up, carry, and put down.

• It is possible to program Karel to locate beepers, transport them, or place them in some graphic pattern.

Page 6: Karel the Robot A Gentle Introduction to the Art of Programming

For example, it might look like the figure below on a computer screen.

Page 7: Karel the Robot A Gentle Introduction to the Art of Programming

Walls

• Also in Karel’s environment are impenetrable wall sections that can be placed between streets.

• Karel can “see” wall sections that are immediately to his front or to his left and right sides.

Page 8: Karel the Robot A Gentle Introduction to the Art of Programming

Wall Sections

• Can be used to form a maze that Karel must navigate

Page 9: Karel the Robot A Gentle Introduction to the Art of Programming
Page 10: Karel the Robot A Gentle Introduction to the Art of Programming

Wall Sections

…or to represent hurdles that Karel must “jump” in a hurdle race

Page 11: Karel the Robot A Gentle Introduction to the Art of Programming

Karel’s Capabilities

• Karel is a mobile robot: he can move forward, in the direction he is facing, and he can turn in place.

• Karel possesses rudimentary senses: Sight Sound Direction Touch

Page 12: Karel the Robot A Gentle Introduction to the Art of Programming

• Karel is equipped with a mechanical arm that he can use to pick up and put down beepers.

• To carry these beepers, Karel has a soundproof “beeper bag.”

• Karel can determine if he is carrying any beepers in this bag by probing it with his arm.

Page 13: Karel the Robot A Gentle Introduction to the Art of Programming

Tasks

• A “task” is just something that we want Karel to do.

• Examples: Move to the corner of 3rd street & 5th avenue. Run a hurdle race (with wall sections

representing hurdles). Run a maze.

Page 14: Karel the Robot A Gentle Introduction to the Art of Programming

• Whenever we want Karel to accomplish a task in the world, we must apply a detailed set of instructions that explains how to perform the task. Karel is able to read and follow such a set of instructions, which is called a program.

Page 15: Karel the Robot A Gentle Introduction to the Art of Programming

Situations

• A “situation”, or “world”, is an exact description of what Karel’s world looks like.

• The basic structure of streets, avenues, and outer boundary walls is fixed.

• What else do we need to specify?

Page 16: Karel the Robot A Gentle Introduction to the Art of Programming

•Karel’s position: location & direction•Location & size of wall sections•Location of beepers•Initial number of beepers in beeper_bag

Page 17: Karel the Robot A Gentle Introduction to the Art of Programming

Initial and Final Situations

•The initial situation for any task is defined to be the situation that Karel is placed in at the start of the task.•The final situation is the situation that Karel is in when he turns himself off.

Page 18: Karel the Robot A Gentle Introduction to the Art of Programming

Karel initially understands only six imperative commands:

Move();

TurnLeft();

PickBeeper();

PutBeeper();

TurnOn();

TurnOff();

When these commands, or function calls, are executed in a Karel program, the results are depicted on the screen.

Page 19: Karel the Robot A Gentle Introduction to the Art of Programming

Move();

• A Move() command will graphically show Karel moving from one street corner to the next.

Page 20: Karel the Robot A Gentle Introduction to the Art of Programming

Move();

• A Move() command will graphically show Karel moving from one street corner to the next.

Page 21: Karel the Robot A Gentle Introduction to the Art of Programming

• Should a wall section be in the way, a Move() command would cause an “Error Shutoff”

Page 22: Karel the Robot A Gentle Introduction to the Art of Programming
Page 23: Karel the Robot A Gentle Introduction to the Art of Programming

TurnLeft();

• A TurnLeft() instruction causes Karel to pivot 90 degrees to the left. Karel remains on the same street corner. For example, if Karel is facing east, a TurnLeft() causes him to face north.

Page 24: Karel the Robot A Gentle Introduction to the Art of Programming

TurnLeft();

• A TurnLeft() instruction causes Karel to pivot 90 degrees to the left. Karel remains on the same street corner. For example, if Karel is facing east, a TurnLeft() causes him to face north.

Page 25: Karel the Robot A Gentle Introduction to the Art of Programming

PickBeeper();

• When Karel executes a PickBeeper() instruction, he picks up a beeper from the corner on which he is standing and then deposits it in his beeper_bag.

• On a corner with multiple beepers, Karel randomly picks one up – and only one – of the beepers.

Page 26: Karel the Robot A Gentle Introduction to the Art of Programming

PickBeeper();

• When Karel executes a PickBeeper() instruction, he picks up a beeper from the corner on which he is standing and then deposits it in his beeper_bag.

• On a corner with multiple beepers, Karel randomly picks one up – and only one – of the beepers.

Page 27: Karel the Robot A Gentle Introduction to the Art of Programming

PickBeeper();

• If a PickBeeper() command is issued and there are no beepers on the corner, then he performs an error shutoff.

Page 28: Karel the Robot A Gentle Introduction to the Art of Programming

PutBeeper();

• Karel executes a PutBeeper() instruction by extracting a beeper from his beeper_bag and placing it on his current street corner.

Page 29: Karel the Robot A Gentle Introduction to the Art of Programming

PutBeeper();

• Karel executes a PutBeeper() instruction by extracting a beeper from his beeper_bag and placing it on his current street corner.

Page 30: Karel the Robot A Gentle Introduction to the Art of Programming

More about beepers

• If Karel has no beepers in his beeper_bag and a PutBeeper() command is issued, he performs an error shutoff.

• Beepers are so small that Karel can move right by them; only wall sections and boundary walls can block his movement.

Page 31: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();

• The TurnOn() instruction must be the first instruction to be performed in a robot program. This instruction sets up the situation and prepares Karel so he can perform the task defined by the instructions that follow.

Page 32: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOff();

• When Karel executes a TurnOff() instruction, he turns himself off and is incapable of executing any more instructions. The last instruction to be executed in every robot program must be a TurnOff() instruction.

Page 33: Karel the Robot A Gentle Introduction to the Art of Programming

A complete robot program: Karel’s task is to transport the beeper from 2nd street, 4th avenue to 4th

street and 5th avenue then to move one street further north before stopping.

Page 34: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();

Page 35: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();Move();

Page 36: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();Move();Move();

PickBeeper();

Page 37: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();Move();Move();

PickBeeper();Move();

Page 38: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();Move();Move();

PickBeeper();Move();TurnLeft();

Page 39: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();Move();Move();

PickBeeper();Move();TurnLeft();Move();

Page 40: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();Move();Move();

PickBeeper();Move();TurnLeft();Move();Move();

Page 41: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();Move();Move();

PickBeeper();Move();TurnLeft();Move();Move();PutBeeper();Move();

Page 42: Karel the Robot A Gentle Introduction to the Art of Programming

TurnOn();Move();Move();

PickBeeper();Move();TurnLeft();Move();Move();PutBeeper();Move();TurnOff();

Page 43: Karel the Robot A Gentle Introduction to the Art of Programming

#include “karel.h”

int main()

{

TurnOn();

Move();

Move();

PickBeeper();

Move();

TurnLeft();

Move();

Move();

PutBeeper();

Move();

TurnOff();

}