This site applies only to CMPT 120 D1 (Burnaby) in Summer 2011. See the other instructors' pages for other sections.
You can download
cmptman.pyc
for Python 2.6,
cmptman.pyc
for Python 2.7.
Constants
N
,E
,S
,W
- Constants for each of the four compass directions.
BLOCK
,DOT
,EMPTY
- Constants representing the three possible things in each square on the board: a barrier (thing you can't hit), a dot (thing CMPTman eats), and nothing (no dot, but can be moved through).
The CMPTman
class
This class holds all of the information about the game. When a game 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 CMPTman
is usually fairly simple, since all you have to do in specify which test board you would like to use:
r = CMPTman(testboard=0)
r = CMPTman(testboard=7)
There are currently test boards numbered 0–12 (but more may be added). They range from very hard for CMPTman to very hard for the ghosts.
It is also possible to specificy the board manually when constructing the object. This is more complicated and is described below.
Properties
The module has no public properties.
Methods
This board will be used as an example in the method descriptions below.
moveGhosts(directions)
-
Move each of the ghosts in the corresponding direction (must be one of
N
,E
,S
,W
, orNone
for no move).cmpt.moveGhosts([N, W, None, S])
cmpt.moveGhosts([N, E, S])The first example above will move ghost 0 north, ghost 1 west, ghost 2 will not move, and ghost 3 moves south.
This method will not return until the ghosts have moved and CMPTman (the user) has made two moves.
The length of the
directions
list must be the same as the number of ghosts on the board (the length of the list returned byghostPositions
). currentPosition()
-
Return the current position of CMPTman on the grid.
x, y = cmpt.currentPosition()
With the above figure, we will have
x==7
andy==8
. 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). ghostPositions()
-
Return the current position of CMPTman on the grid.
positions = cmpt.ghostPositions()
With the above figure, this method will return
[(5,5), (3,8), (2,3), (8,2)]
. contents(x, y)
-
Return the object in the given position on the board. Return value will be one of
BLOCK
,DOT
, orEMPTY
.item = cmpt.contents(1,2)
With the above figure, the method will return the following values:
cmpt.contents(1,2) == BLOCK
cmpt.contents(2,3) == DOT # there is a dot under that ghost
cmpt.contents(4,0) == DOT
cmpt.contents(8,8) == EMPTY allContents()
-
Return the contents of each square on the board as a list.
contents = cmpt.allContents()
The contents are returned as a list of width×height elements in row-major order. That is, the first row is read right-to-left, then the second row, the third row, and so on.
boardSize()
-
Return the dimensions (width and height) of the board.
w, h = r.boardSize()
With the above figure, we will have
w==10
andh==10
. dotsLeft()
-
Return the number of uncollected dots on the board.
num = cmpt.dotsLeft()
With the above figure, we will have
num==92
. playing()
-
Return
True
if the game is still going on.while cmpt.playing(): …
This should be used to determine when to exit your program's main loop.
Advanced: designing a board
The constructor for a game also has arguments that allow you to create a custom board. If you don't specify testboard
, you can contruct a board using these arguments. All board positions are expressed as a tuple with two values, the horizontal and vertical position, both count starting from zero.
size
- The size of the board:
(width, height)
. initpos
- The initial position for CMPTman.
ghosts
- A list of initial positions for the ghosts.
blocks
- A list of positions for the blocks on the board.
empty
- A list of squares that initially have no dots.
For example, a board starting like the screenshot above can be created like this:
p = CMPTman(size=(10,10), initpos=(7,8), ghosts=[(5,5), (3,8), (2,3), (8,2)], blocks=[(6,1), (1,2), (5,4), (4,6), (7,6), (2,8)], empty=[(8,8)])
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 game with each to get a good look at each one.
testboard=0
: an empty board with 4 ghoststestboard=1
: an empty board with 2 ghoststestboard=2
: three barrier squares and 4 ghoststestboard=3
: six barrier squares and 4 ghoststestboard=4
: six barrier squares and 2 ghoststestboard=5
: a grid of barriers, 2 ghoststestboard=6
: grid of horizontal linestestboard=7
: grid of vertical linestestboard=8
: ghosts in enclosed roomstestboard=9
: ghosts in enclosed but empty (dot-free) roomstestboard=10
: scattered “plus signs”testboard=11
: a board divided into 4 “rooms”testboard=12
: approximation of the original Pac-Man board
More test boards may be added.