# Karel’s world – week 5

“For what I’ve seen of programmers,
they’re either good at all languages or good at none.
The guy who’s a good C programmer will be good at Erlang;
it’s an incredibly good predictor.
I’ve seen exceptions to that but the mental skills necessary to be good at one language seem to cover to other languages.”

— Joe Armstrong, designer of the Erlang computer language

Handout: 3_Intro_1_karel

## Reeborg, our Karel simulator, and Python, its language

The author of our robot simulator, Reeborg, is the Canadian physicist André Roberge (see our Hall of Fame).

The simulator runs in Python, the most popular computer language in the world, used extensively in academia, research and industry, and often used as a first language because of its clarity.

Python’s popularity in 2015: 31.2%

## The Reeborg GUI

First, identify the different parts of the interface:

#### Karel’s world

• this is the grid where Karel lives
• the cells may be empty or contain an object:
• one or more tokens that Karel can pick up
• an ’empty’ token where we should put tokens to end the mission correctly
• Karel’s destination, or ‘home’ square
• there can be walls between cells
• On the top-left area, we can see how many tokens Karel has in its bag

#### code editor

• The code editor has a ‘Python code’ tab and a ‘library’ tab
• We write our programs in the ‘Python code’ area only
• The ‘library’ area will have functions that our program can use
• Our programs should not be longer than 30 lines

#### Control buttons

On the top-left area, from left to right, we these buttons:

• play button: runs the program
• step button: runs only the next command
• pause button: stops the program temporarily
• stop button: stops the program without reseting the world
• rewind button: stops the program and resets the world

## Solving a mission

We are now going to solve a mission based on the world that we saw before. Karel needs to pick up the yellow token, put it in the basket and then go home. So let’s get started.

show it to me

A comment is text that is not part of the program; it has information for the programmer, not for the computer, i.e., it is like a margin note. In Python, any text that follows a hashmark # is a comment.

Let’s start the program with the following comment:

# The goal of this mission is to put the token
# in a basket and then go home


Now, when we run the program (i.e., when we press the ‘play’ button), the program will run and fail because, although the program is valid, it does not achieve any of the goals of the mission, i.e., there is no token in the basket and there is no robot on the green square.

#### System commands

show it to me

The following are a few functions that will appear often in our missions.

• sound( True/False ): turns the sound effects on and off
• think(n): sets a delay between commands, in milliseconds. Delays shorter or equal to 250 ms turn off the sound effects.
• say(message): prints a message in a pop-up box.
• pause(): pauses the program until we press the play or step button

Let’s add them in our program:

sound(True)
think(1000)

# main program
msg = "I am ready to start. Press play to continue"
say(msg)
pause()


With these commands we are:

1. turning the sounds on
2. setting the delay between commands to 1 second
3. assigning a string to the variable msg
4. printing the message to the screen with the function say()
5. pausing the program

#### Built-in commands

show it to me

An actuation command (or function) allows a robot to act. Karel has 4 built-in actuation functions:

move()       # move to the cell in front of the droid
turn_left()  # do a 90° turn to the left (in place)
take()       # pick one token from floor and put in bag
put()        # get token from bag and put it on floor


Let’s use these commands to accomplish the first task, i.e., to put the token in the basket, adding the following code:

move()       # step on the cell that has the token
take()       # take token and put it in bag
turn_left()  # turn to left of the robot, not our left
move()       # step on the cell that has the basked
put()        # put on the floor the token in the bag


When we run the program now, it still fails but indicates that the tokens are now in their correct final location.

Important:

A mission fails if…
…we crash Karel against a wall
…we ask him to take a token while standing on an empty cell
…we ask him to put a token but his bag of tokens is empty

#### Repeat

show it to me

Repeat() repeats a single command multiple times:

repeat( command, n )  # repeat command n times


For example, Karel has placed the token in the basket; now he needs to turn around to go home. To turn around we can tell him to turn_left() twice or, better, use the repeat() command to tell him to do turn_left() 2 times.

In summary, instead of writing this:

turn_left()
turn_left()


we are going to write this:

repeat(turn_left, 2)  # turn_around


Both pieces of code do the same thing but the second version is better because does the same thing in a more clear way. Again:

the 2nd version is better because it is clearer than the first one, not because it is shorter.

#### The library

show it to me

Although Karel only knows 4 commands when we turn him on, we can teach him new commands that are based on the 4 commands that he already knows. We have provided you with 3 additional commands that you can find in the library:

• left()
• right()
• turn_around()

In particular, turn_around() does exactly what we just did ourselves: turns the robot around by using repeat(). Thus, instead of repeating the code, we just use the command already written in the library. We tell Python that we want to use the commands in the library placing the following line at the top of our program:

from my_lib import  *


which means ‘allow my program to use everything in my_lib‘.

Hence, we can modify our program to use the library like this:

from my_lib import  *
....
turn_around()


## 3rd grade – Almost there

Work it out

What? The training mission is incomplete: Karel needs to go to the home square. Please, finish the mission and take Karel home for a well-deserved rest after all this token taking-and-putting.

## 4th grade – A dozen chicks

work it out

Ahh!.. the chicks got out of the pen. Quick.. return them to the pen and then take Karel home.