# Karel++ A Gentle Introduction to the Art of Object-Oriented Programming

## Joseph Bergin Mark Stehlik Jim Roberts Richard Pattis

### Copyright 1997, John Wiley & Sons. All Rights Reserved.

ISBN 0-471-13809-6
You can purchase this book from the Wiley Home Page .

## 1 The Robot World

This chapter introduces a class of robots and sketches the world they inhabit. In later chapters, where a greater depth of understanding is necessary, we will amplify this preliminary discussion.

### 1.1 The Robot World

Robots live in a world that is unexciting by today's standards (there are no volcanoes, Chinese restaurants, or symphony orchestras), but it does include enough variety to allow robots to perform simply stated, yet interesting tasks. Informally, the world is a grid of streets that robots can traverse. It also contains special objects that a robot can sense and manipulate.

Figure 1-1 is a map illustrating the structure of the robot world, whose shape is a great flat plane with the standard north, south, east, and west compass points. The world is bounded on its west side by an infinitely long vertical wall extending northward. To the south, the world is bounded by an infinitely long horizontal wall extending eastward. These boundary walls are made of solid neutronium, an impenetrable metal that restrains robots from falling over the edges of the world.

Crisscrossing the world are horizontal streets (running east-west) and vertical avenues (running north-south) at regular, one-block intervals. To help you distinguish between streets and avenues, remember that the A in "Avenue" points north and the V points south. A corner, sometimes called a street corner or intersection, is located wherever a street and an avenue intersect.

One or more robots can occupy any corner, facing any of the four major compass directions. Any number of robots may occupy the same corner, because the streets and avenues are quite wide. We will usually work with only one robot at a time, however. Robots have names so that we can give them instructions individually. When we work with a single robot we will often call it Karel,1 The name Karel is used in recognition of the Czechoslovakian dramatist Karel Capek, who popularized the word robot in his play R.U.R. (Rossum's Universal Robots). The word robot is derived from the Czech word robota, meaning "forced labor." though you are free to name the robots that you create with any names you like.

Both streets and avenues have numbers; consequently, each corner is identified uniquely by its street and avenue numbers. The corner where 1st Street and 1st Avenue intersect is named the origin. The positions of robots and other objects in this world can be described using both absolute and relative locations. The absolute location of the origin, for example, is the intersection of 1st Street and 1st Avenue. An example of a relative location would be to say that a robot is two blocks east and three blocks north of some object in the world. The origin also has a relative location; it is the most southwesterly corner in the robot world. Sometimes we will describe a robot task using language that gives a different interpretation to the robot world, with north as up, south down, and west and east being left and right, respectively. This is how we (in the Northern Hemisphere) normally look at maps, of course.

Besides robots, two other kinds of things can occupy this world. The first of these kinds of things is a wall section. Wall sections are also fabricated from the impenetrable metal neutronium, and they can be manufactured in any desired length and pattern. They are positioned between adjacent street corners, effectively blocking a robot's direct path from one corner to the next. Wall sections are used to represent obstacles, such as hurdles and mountains, around which robots must navigate. Enclosed rooms, mazes, and other barriers can also be constructed from wall sections. Figure 1-2 shows some typical wall arrangements a robot might find in the world.

The second kind of thing in the world is a beeper. Beepers are small plastic cones that emit a quiet beeping noise. They are situated on street corners and can be picked up, carried, and put down by robots. Some tasks require one or more robots to pick up or put down patterns made from beepers or to find and transport beepers. Figure 1-3 shows one possible pattern of beepers. Beepers are small so there can be several on a corner, and they don't interfere with robot movement.

### 1.2 Robot Capabilities

Let's now shift our attention away from the robot world and concentrate on the robots themselves. Robots are mobile; a robot can move forward (in the direction it is facing), and it can turn in place. Robots can also perceive their immediate surroundings using rudimentary senses of sight, sound, direction, and touch.

A robot sees by using its TV camera, which points straight ahead. This camera is focused to detect a wall exactly one-half block away from the robot. A robot also has the ability to hear a beeper, but only if the robot and the beeper are on the same corner; the beepers beep very quietly. By consulting its internal compass, a robot can determine which direction it is facing. Finally, each robot is equipped with a mechanical arm that it can use to pick up and put down beepers. To carry these beepers, each robot wears a soundproof beeper-bag around its waist. A robot can also determine whether it is carrying any beepers in this bag by probing the bag with its arm. A robot can also use its arm to determine whether there are other robots on the same corner that it occupies. Finally, a robot can turn itself off when its task is complete.

As you might expect, robots are made in factories. All robots come from the main factory, Karel-Werke, which can actually supply several different models of robots. When we need a robot for a task, we can use the standard model, or we can write a specification for a new model. Karel-Werke is able to build specialized robots that are modifications or extensions of the existing models.

Whenever we want a collection of robots to accomplish a task in the robot world, we must supply a detailed set of instructions that describe any special features of the robots that are needed and also explain how to perform the task. For most tasks one robot is all that is needed. When a robot is ordered from the factory, it is delivered to the robot world by helicopter. The helicopter pilot sets up the robots according to our specifications and reads each new robot a set of instructions to detail its task, which it is then able to carry out.

What language do we use to program (here we use "program" to mean "write instructions for") robots? Instead of programming these robots in English, a natural language for us, we program them in a special programming language. This language was specially designed to be useful for writing robot programs. The robot programming language-like any natural language-has a vocabulary, punctuation marks, and rules of grammar, but this language, unlike English, for example, is simple enough for robots to understand. However, it is a powerful and concise language that allows us to write brief and unambiguous programs for them.

### 1.3 Tasks and Situations

A task is something that we want a robot to do. The following examples are tasks for robots:

• Move to the corner of 15th St. & 10th Ave.
• Run a hurdle race (with wall sections representing hurdles).
• Escape from an enclosed room that has a door.
• Find a beeper and deposit it on the origin.
• Escape from a maze.

A situation is an exact description of what the world looks like. Besides the basic structure of the world, which is always present, wall sections and beepers can be added. To specify a situation completely, we must provide answers for the following questions.

• What is each robot's current position? We must specify both the robot's location (which corner it is on) and what direction it is facing.
• What is the location and length of each wall section in the world?
• What is the location of each beeper in the world? This information includes specifying the number of beepers in each robot's beeper-bag.

Situations are specified in this book by a small map or brief written description. If we know the number of beepers that each robot has in its beeper-bag, then the maps in Figure 1-4 completely specify different situations. The initial situation for any task is defined to be the situation in which all of the robots are placed at the start of the task. The final situation is the situation that each robot is in when it turns itself off. Figure 1-4 shows six initial situations that are typical for tasks that a single robot will accomplish in the coming chapters.

### 1.4 Problem Set

The purpose of this problem set is to make sure that you have a good understanding of the robot world and the capabilities of robots before moving on to robot programming.

1. Which of the following directions can a robot face?

• northeast
• east
• south-southwest
• north
• 164 degrees
• vertical
• down

2. What objects other than robots can be found in the robot world?

3. Which of the objects listed in Problem 2 can a robot manipulate or change?

4. What reference points can be used in the robot world to describe a robot's exact location?

5. How many robots can we have in a given robot world?

6. Give the absolute location of each robot in each of the worlds shown in Figure 1-5. Give a relative location of each robot in the worlds.

Back to the Book Karel++