CSE 111, Fall 2000

Great Ideas in Computer Science

Lecture Notes #5

Karel the Robot

Named after Karel Capek, author of R.U.R. (1920)
    (which stands for "Rossum's Universal Robots";
    Capek coined the term "robot")

    (for other links, see the Directory of Documents)

Why study Karel the Robot?
    *    as an elaboration of Insight III:
          sequence, choice, repetition, & naming

1.  Karel:  a "robot" (actually, an arrow, which I'll
                               represent here as:  <  )

2.  Karel's "world":    a grid of streets and avenues,
                                with walls and beepers
        (which I won't try to draw here; see the
        Karel website for better pictures)

3.    You should run Karel;
        for information on downloading
        your own copy of Karel, see the website:

        How to Download Your Own Copy of
        Karel the Robot onto a Windows PC

        or use the CIT Windows machines.

4.    Karel can:

    *    move forward (in the direction he faces)
            - i.e., change location, but not direction

    *    turn; i.e., change direction, but not location

    *    perceive:
            - look ahead 1/2 block, without turning
            - look left 1/2 block, without turning
            - look right 1/2 block, without turning
            - hear beeper on same corner
            - the direction he's facing

    *    pick up & put down beepers

    *    determine if he is carrying any beepers in his
          beeper bag

5.    Sample tasks:

        *    go to corner of 6th St. & 25th Ave.
        *    pick up beeper on 3rd St. & 6th Ave.
        *    escape from a room with only one door

6.    To get Karel to do things (to "perform tasks"),
        we give him a program

        *    i.e., an algorithm expressed in his language

7.    To describe a task for him to perform
        (a problem for him to solve),
        we use "situtations":

        *    exact descriptions of Karel's world:

            -    Karel's current position
                    (= location + direction)

            -    location & size of all walls

            -    location & amount of all beepers

        *    initial situation:  when Karel turns on
        *    final situation:    when Karel turns off

        *    task:    gets Karel from initial situation
                        to final situation
                        -    i.e., solves problem

8.    Karel has 5 "primitive" or basic instructions:

SYNTAX
(what the instruction
looks like in
Karel's lang
SEMANTICS
(what the instruction means
(i.e., what Karel does)
move IF Karel sees no walls,
    THEN Karel moves forward
             1 block in direction
             he's facing
     ELSE IF Karel sees a wall,
               THEN "error shutoff"
turnleft Karel pivots 90° left
pickbeeper IF there is at least one beeper
    on Karel's corner,
    THEN Karel picks up one
              beeper & puts it in
              his bag
    ELSE error shutoff
putbeeper IF there is at least one beeper
    in Karel's bag,
    THEN Karel puts it on his
              corner
     ELSE error shutoff
turnoff Karel turns himself off

Note: All programs must have turnoff as last
         instruction.

9.    e.g., init. sit. = K at origin, facing N;
                             1 beeper at 2nd St. & 2nd Ave.

              final sit = K at 2nd St. & 2nd Ave.,
                             facing N,
                             no beepers there

        possible solution:

    But, to tell this to Karel, we must be polite,
    and use some other phrases to get him to
    listen to us, so the full program would look
    like this:     There are several other programs that will do the
    same thing.
    Try to write your own,
        and then test them out on Karel!

10.  Things that can go wrong ("bugs"):

        *    lexical (vocabulary) errors:
                -    using a word not in Karel's language
                        e.g., "turnright"

        *    syntactic (grammatical) errors:
                -    bad grammar or punctuation
                        e.g., no semicolon between instructions,
                                no "beginning-of-execution"

        *    error shutoff:
                -    result of an incorrect program
                -    possible causes:
                        >    move beyond wall or boundary
                        >    pickbeeper where there is no
                              beeper
                        >    putbeeper when Karel has no
                              beepers
                        >    no turnoff

        *    semantic (meaning) errors:
                -    Karel's program is correct,
                      & does exactly what we tell him,
                      successfully,
                        BUT:
                          we didn't tell him what we meant
 
                        e.g.:  for above problem:

                                move;
                                turnleft;
                                turnleft;
                                turnleft;
                                move;
                                turnoff

                        OOPS! We forgot to tell him
                                  "pickbeeper"

    Debugging =  finding & fixing bugs
 


Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)

file: 111F00/lecturenotes5.15sp00.html