A4 Robotlib Docs
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)
There are test boards numbered 0–16; the “difficulty” increases as the number increases.
You can also create a random board by manually 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.
You can also specify a delay
when instantiating the robot, to control the speed of the animation: it is a number of seconds to wait between moves. The default is 0.25 seconds, so this will be a faster animation than the default:
r = Robot(testboard=7, delay=0.1)
Display Problems
If instantiating the Robot
doesn't make a window pop up like in the screenshot, there is likely a problem with your system and the way the library draws its window. You can change OUTPUT_MODE
at the top of the file to "ascii"
instead of "tk"
to get a printing-characters representation of the board instead of the window.
Interface Scaling
If you have a high-resolution display, you might find the window looks incredibly tiny. If so, change the UI_SCALE
constant at the top of robotlib.py
to 2
or 3
.
Methods
Moving: robot.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.
Looking: robot.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:
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)
Position: robot.current_position()
Return the current position of the robot on the grid.
x, y = r.current_position()
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).
Size: robot.board_size()
Return the dimensions of the board.
w, h = r.board_size()
With the above figure, we will have w==10
and h==10
.
Status: robot.coins_left()
Return the number of uncollected coins on the board.
num = r.coins_left()
With the above figure, we will have num==3
.
Finishing: robot.exit()
Stop the robot and close the game window after one second (or the number of seconds given as an optional argument).
r.exit()
Calling .move
or .sensor
after this method will cause an exception.
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.
testboard=0
: one arbitrarily placed coin, no blockstestboard=1
: four arbitrarily placed coins, no blockstestboard=2
: one block and one coin; different rows/colstestboard=3
: one block and one coin; same rowtestboard=4
: one block and one coin; same coltestboard=5
: one block with coins on each compass pointtestboard=6
: coin between two blocks (same row)testboard=7
: coin between two blocks (same col)testboard=8
: coin surrounded by three blockstestboard=9
: coin surrounded by three blockstestboard=10
: coin surrounded by four blockstestboard=11
: coin surrounded by five blockstestboard=12
: plus sign of blocks with a coin in each cornertestboard=13
: one block in each corner, with coins surrounding eachtestboard=14
: nasty board 1: a “room” full of coinstestboard=15
: nasty board 2: long “hallways”testboard=16
: nasty board 3: a maze