Next / Previous / Contents / Shipman's homepage

18. Puzzle.__gridLine(): Parse one line of the framework

kkck
# - - -   P u z z l e . _ _ g r i d L i n e

    def __gridLine ( self, line ):
        '''Scan one line of the framework.

          [ line is a nonempty string ->
             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) ) ]
        '''

First we scan the columns of this row, looking for nonblank characters. Each character becomes a new Cell instance, indexed within self.__cellMap.

kkck
        #-- 1 --
        # [ self.__cellMap  +:=  entries for nonblank
        #       characters in line for row self.size[0]
        #       and the same columns as line ]
        for colx in range(len(line)):
            #-- 1 body --
            # [ if line[colx] is nonblank ->
            #     self.__cellMap[self.size[0], colx]  :=
            #         a new Cell instance at (self.size[0], colx)
            #         with text line[colx]
            #   else -> I ]
            text = line[colx]
            if text != ' ':
                rowx = self.size[0]
                coord = Coord ( rowx, colx )
                self.__cellMap[(rowx, colx)] = Cell ( coord, text )

Maintain a running count of lines, and a running maximum line length, so we will eventually know the final dimensions of the framework.

kkck
        #-- 2 --
        self.size[0] += 1
        self.size[1] = max ( self.size[1], len(line) )