Next / Previous / Contents / TCC Help System / NM Tech homepage

14. class Cell: One cell in the maze

In addition to the cell-key, each instance of this class also contains a .cellSet attribute that is used in Kruskal's algorithm. When each instance is constructed, this attribute is a Python set containing itself. When a wall is removed between two cells that are in different sets, the two sets are merged.

mazeratty
# - - - - -   c l a s s   C e l l

class Cell(object):
    '''Represents one cell of the maze.

      Exports:
        Cell(cellKey):
          [ cellKey is a cell-key ->
              return a new Cell instance with those coordinates and
              its .cellSet attribute containing a singleton set
              containing that instance ]
        .cellKey:  [ as passed to the constructor ]
        .cellSet:  [ a set of Cell instances; read/write ]
    '''

Because there may be many cells, we use a __slots__ special class variable to declare that instances will have only these two attributes.

mazeratty
    __slots__ = ('cellKey', 'cellSet')
    def __init__(self, cellKey):
        '''Constructor.
        '''
        self.cellKey = cellKey
        self.cellSet = set([self])