CMPT 120 Assignment 4 robotlib Module

This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.

You can download robotlib.pyc for Python 2.3, robotlib.pyc for Python 2.4, or robotlib.pyc for Python 2.5.

Constants

N, E, S, W
Constants for each of the four compass directions.
WALL, BLOCK, COIN
Constants representing the three possible things visible to the robot's sensor: a wall (edge of the grid), a block (barrier that the robot can't hit), and a coin (object that the robot should collect).

The Robot class

This class holds all of the information about your robot. When a robot object is created, a window with the board is displayed using the Tkinter module (so you need to have it installed—it is installed if you can use IDLE).

Instantiating

The constructor for a Robot is fairly complicated, since you have to specify all of the information about the board. There are many keyword arguments that can be specified to affect the game.

There are two basic ways to create the board. The easiest is to use one of the given test cases:

r = Robot(testboard=0)
r = Robot(testboard=7)

There are currently test boards numbered 0–16; the “difficulty” increases as the number increases.

You can also create a random board by specifying information about the board you want created:

r = Robot(coins=5, blocks=2, width=10, height=10, robotposition=[2,3] )

This will create a 10×10 board with five coins and two blocks (all randomly placed). The initial position of the robot will be (2, 3). The position will be random if you don't specify a robotposition.

Note that if you create a random board, it may be impossible to get all of the coins. There is no code in the module that ensures that it's possible to reach all of the coins from the robot's initial position. It doesn't matter what your robot does if it's impossible to collect all of the coins.

Properties

delay
The length of time to pause between each move, in seconds. This affects the speed of the animation. The default is 0.25, but it can be changed at any time.
r.delay = 0.1

Methods

move(direction)

Move the robot one square in the given direction (must be one of N, E, S, W)

r.move(N)
r.move(E)

Each move takes the amount of time given by the delay property. This slows down the animation so you can see what's going on.

sensor(direction)

Check the robot's sensor in the given direction (must be one of N, E, S, W). Returns two values: the object seen by the sensor and the distance to the object.

obj, dist = r.sensor(N)
obj, dist = r.sensor(W)

The object will be one of:

  • WALL: the closest object in the given direction is a wall (edge of the grid).
  • BLOCK: the closest object in the given direction is a block (barrier in the grid).
  • COIN: the closest object in the given direction is a coin.

The distance is the number of squares away the object is. A distance of 1 means that the object is in the adjacent square. For example, consider this board:

sample robot grid

In this configuration, each of these conditions will be true:

r.sensor(N) == (COIN, 1)
r.sensor(E) == (WALL, 4)
r.sensor(S) == (BLOCK, 4)
r.sensor(W) == (COIN, 4)
currentPosition()

Return the current position of the robot on the grid.

x, y = r.currentPosition()

With the above figure, we will have x==6 and y==3. Positions on the board are numbered so that the upper-left corner is (0,0). The horizontal values increase to the right (east) and vertical values increase down (south).

boardSize()

Return the dimensions of the board.

x, y = r.boardSize()

With the above figure, we will have x==10 and y==10.

coinsLeft()

Return the number of uncollected coins on the board.

num = r.coinsLeft()

With the above figure, we will have num==3.

Advanced: designing a board

The constructor for a robot also has arguments that allow you to create a custom board.

The boardlayout argument is a list of items that are to be placed on the board. An item is expressed by a 3-tuple containing the x-position, y-position, and type of object.

Again, you can use the robotposition argument to set the robot's initial position.

For example, this code was used to create the board pictured above:

r = Robot( width=10, height=10,
        boardlayout=[(4,7,BLOCK), (5,7,BLOCK), (6,7,BLOCK), (7,7,BLOCK),
                 (6,2,COIN), (6,9,COIN), (2,3,COIN)],
        robotposition=(6,3)
        )

If you have boards that you think are particularly interesting tests and should be included as a testboard, please email the constructor to Greg.

The test boards

Below is a short description of each of the test boards provided. Remember that these aren't the only possible tests— your code should work with other boards as well. In fact, they won't be the ones we use to test your code, but they will be similar.

Of course, you could also just instantiate your robot with each to get a good look at each one.