Robot has executed an error shutoff
2 Primitive Instructions and Simple Programs
8
Figure 2-1 A Figure 2-1 B
Figure 2-2
2.2 TURNING IN PLACE
o o
o o
o o o
o o
o o
o o
o o
o
o o
o o o
o o
o o o
o o
o o
o o o
o o
o o o
o o
o o
o o
o o o
o o
o o o
o o
o o
o o
o o o
o o
o o
o o
In c ntrast, Figure 2-2 sh ws an inc rrect attempt t m ve. When this r b t tries t execute a
instructi n in this situati n, it sees a wall secti n. Relying n its self-preservati n instinct, it perf rms an err r shut ff.
The sec nd primitive instructi n that changes a r b t’s p siti n is . This
instructi n changes the directi n in which the r b t is facing but d es n t alter its l cati n.
A r b t executes a instructi n by piv ting 90 t
the left. The r b t remains n the same street c rner while exe- cuting a
instructi n. Because it is imp ssible f r a wall secti n t bl ck a r b t’s turn,
cann t cause an err r shut ff.
A r b t always starts a task n s me c rner, facing either n rth, s uth, east, r west. A r b t cann t travel fracti ns f a bl ck r turn at ther than 90 angles. Alth ugh
move move
move
move
turnLeft
turnLeft turnLeft
turnLeft turnLeft
8
8
o o o o
o o
o o
o
o o o
o o
o o
o A R b t in the Initial
A R b t in the Final Sit- Situati n Bef re a
Instructi n uati n After Executing a
Instructi n
The Result f a R b t Attempting
t When Its Fr nt Is Bl cked In an Err r
Shut ff
2.4 Handling Beepers
9
2.3 FINISHING A TASK
2.4 HANDLING BEEPERS
o o o
o o
o o o
o o
o o
o o
o o
o o
o o
o o
o o
o o o
o o
o o
o o
o o
o o
o o
o o o
o o
o o
o o
o o o
o o
o o o
o o o
o o
o o
o o
o o
o o o
o
o o o
o o
o o
o o
o o
o o o
o o
o o
o o
o o o
o o
o o
o o
o o o
o o
o o o
o o
o o
o o o
o o o
o o
o o o
o o
o o
o o
o o
o o o
o o
o o
o o o
o o o
o o
o o o
o o
o o
and change the r b t’s p siti n, after executing either f these
instructi ns, the r b t still is n s me c rner and still is facing ne f the f ur c mpass directi ns.
Karel-Werke’s designer purp sely did n t pr vide a built-in instruc-
ti n. W uld adding a t the primitive instructi ns all w the r b t t
perf rm any task it cann t acc mplish with ut ne? A m ment’s th ught—and the right flash f insight—sh ws that the
instructi n is unnecessary; it d es n t permit r b ts t acc mplish any new tasks. The key bservati n f r verifying this
c nclusi n is that a r b t can manage the equivalent f a instructi n by
executing three instructi ns.
We need a way t tell a r b t that its task is finished. The instructi n fulfills
this requirement. When a r b t executes a
instructi n, it turns ff and is incapable f executing any m re instructi ns until restarted n
an ther task. The last instructi n executed by every r b t in a pr - gram must be a
instructi n.
Every r b t understands tw instructi ns that permit it t handle beepers. These tw instructi ns perf rm pp site acti ns.
When a r b t executes a instructi n, it picks
up a beeper fr m the c rner n which it is standing and then dep sits the beeper in its beeper-bag. If a
in- structi n is attempted n a beeperless c rner, the r b t per-
f rms an err r shut ff. On a c rner with m re than ne beeper the r b t picks up ne, and nly ne, f the beepers and then
places it in the beeper-bag.
A r b t executes a instructi n by extracting a
beeper fr m its beeper-bag and placing the beeper n the cur- rent street c rner. If a r b t tries t execute a
instructi n with an empty beeper-bag, the r b t perf rms an err r shut ff. If the r b t has m re than ne beeper in its
beeper-bag, it extracts ne, and nly ne, beeper and places it
n the current c rner. Beepers are s small that r b ts can m ve right by them; nly wall secti ns and b und-
ary walls can bl ck a r b t’s m vement. R b ts are als very adept at av iding each ther if tw
r m re sh w up n the same c rner simultane usly.
move turnLeft
turnRight turnRight
turnRight turnRight
turnLeft
turnOff turnOff
turnOff turnOff
pickBeeper pickBeeper
pickBeeper
putBeeper putBeeper
putBeeper
2 Primitive Instructions and Simple Programs
1 0 2.5
ROBOT DESCRIPTIONS
o o o
o o o o
o o
o o
o o
o o o
o o o
o o o
o o
o o o
o o o
o o
o o o
o o o
o o o
o o
o o o
o o
o o
o o o o
o o o
o o o
o o
o o
o o
o o
o o
o oo
o o
o o
o o
o o
o o
o o o
o o o
o o
o o o
o o
o o
o o
o o
o o
o o
o o
o o
o o
o o o
o All r b ts pr duced by Karel-Werke have at least the capabilities just described. As
we will see, such r b ts are very primitive, and we might like r b ts with additi nal abilities. Theref re, we must have s me way t describe th se extra abilities s that
the fact ry can build a r b t t ur specificati ns. Karel-Werke empl ys a simple
r b t pr gramming language t describe b th r b t abilities and the lists f r b t in- structi ns, called
The simple m del f r b t described ab ve is called the class. The specificati n f the
class in the r b t pr gram- ming language f ll ws.
F ll wing the m del class name is a list f instructi ns f r this kind f r b t. The list is always written in braces
and , and the final brace is always f ll wed by a semic l n. Each entry is als terminated by a semic l n.
The five instructi ns, thr ugh
, name acti ns that can perf rm. We defined each f these acti ns in the f reg ing secti ns, and we will
see many examples f their use thr ugh ut this b k. The w rd
prefixes each f these instructi ns t indicate that they return n feedback when executed. Later we
will see additi nal instructi ns that d pr duce feedback when executed, rather than changing the state f the r b t as these instructi ns all d . The matching parentheses
that f ll w the instructi n names mark them as the names f acti ns that a r b t will be able t carry ut.
A sample task f r an might be t start at the rigin, facing east, and
then walk three bl cks east t a c rner kn wn t have a beeper, pick up the beeper, and
n that c rner. A c mplete pr gram t acc mplish this task is sh wn next. In this pr gram we name the r b t
, but we c uld use any c nvenient name.
1
programs.
1
ur_Robot ur_Robot
class ur_Robot {
void move; void turnOff;
void turnLeft; void pickBeeper;
void putBeeper; };
{ }
move putBeeper
ur_Robots void
ur_Robot turnOff
Karel task
{ ur_Robot Karel1, 1, East, 0;
Deliver the robot to the origin 1,1, facing East, with no beepers.
Karel.move;
ur ur
o o
o o o
o o
o oo
oo is a German prefix meaning “ riginal” r “primitive.” The pr nunciati n f
is similar t the s und f “
r” in “p r.”
Initial Situation Final Situation
2.6 A Complete Program
1 1
Figure 2-3
2.6 A COMPLETE PROGRAM