Next / Previous / Contents / Shipman's homepage

17. Puzzle.__digestGrid(): Parse the framework

kkck
# - - -   P u z z l e . _ _ d i g e s t G r i d

    def __digestGrid ( self, rawGrid ):
        '''Parse the framework section of the puzzle file.

          [ rawGrid is a list of nonempty strings ->
              if rawGrid is a valid framework section ->
                self.size[0]  :=  max ( self.size[0], number of
                    rows in rawGrid )
                self.size[1]  :=  max ( size[1], length of
                    longest row in rawGrid )
                self.__cellMap  +:=  entries for the cells in rawGrid
                self.__slotMap  +:=  entries for the slots in rawGrid
              else -> raise SyntaxError ]
        '''

We look at each framework line in turn. A running maximum of line lengths is maintained in self.size[1]. Each nonblank character in the line becomes a Cell instance in self.__cellMap. See Section 18, “Puzzle.__gridLine(): Parse one line of the framework”.

kkck
        #-- 1 --
        # [ self.__cellMap  +:=  entries for nonblank characters
        #       in rawGrid
        #   self.size[1]  :=  max ( self.size[1], longest line
        #       in rawGrid )
        #   self.size[0]  +:=  number of lines in rawGrid ]
        for line in rawGrid:
            #-- 1 body --
            # [ self.__cellMap  +:=  entries for nonblank
            #       characters in line for row self.size[0]
            #       and the same columns as line
            #   self.size[0]  +:= 1
            #   self.size[1]  :=  max ( self.size[1], len(line) ) ]
            self.__gridLine ( line )

        #-- 2 --
        if self.size[0] == 0:
            raise SyntaxError ( "Empty puzzle frame." )
        print ( "=== Puzzle size: %d rows, %d columns." %
                (self.size[0], self.size[1]) )

Now that we know where the cells are, we scan them first horizontally, then vertically, to locate all the slots. See Section 19, “Puzzle.__findSlots(): Locate all the slots”.

kkck
        #-- 3 --
        # [ self.__slotMap  +:=  entries for the slots in
        #       self.__cellMap ]
        self.__findSlots()