Next / Previous / Contents / Shipman's homepage

15. Puzzle.__input(): Read the puzzle file

kkck
# - - -   P u z z l e . _ _ i n p u t

    def __input ( self, inFile ):
        '''Read the puzzle, set up the empty skeleton.

          [ inFile is a readable file ->
              if inFile contains a valid puzzle file ->
                inFile  :=  inFile advanced to end of file
                self.size[0]  :=  max ( self.size[0], number of
                    rows in the framework part of inFile )
                self.size[1]  :=  max ( self.size[1], length of
                    longest row in the framework part of inFile )
                self.__cellMap  +:=  entries mapping cell
                    coordinates from inFile to new Cell instances
                self.__slotMap  +:=  entries mapping slot
                    coordinates from inFile to new Slot instances
                self.__wordBank  :=  a new WordBank instance
                    representing the word list from inFile, with
                    no initial slot choices
              else -> raise SyntaxError ]
        '''

The first task is to partition the input file contents into the framework section and the word list section. Section 16, “Puzzle.__readFile(): Partition the input file” returns these two file sections as two lists of strings, with the trailing newlines removed, and comment lines deleted. Then the framework is sent to Section 17, “Puzzle.__digestGrid(): Parse the framework” to build the cells and slots, and the word list is sent to Section 22, “Puzzle.__digestWords(): Parse the word list” to build the word list.

kkck
        #-- 1 --
        # [ if inFile contains at least one blank line ->
        #     inFile  :=  inFile advanced to end of file
        #     rawGrid  :=  lines from inFile up to the first
        #         blank line
        #     rawWords  :=  lines following the first blank line,
        #         newlines and comments removed
        #   else -> raise SyntaxError ]
        rawGrid, rawWords = self.__readFile ( inFile )

        #-- 2 --
        # [ 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 ]
        self.__digestGrid ( rawGrid )

        #-- 3 --
        # [ if (rawWords is a valid word list) and
        #   (the counts and lengths of the word list match the
        #   counts and lengths of slots in self.__slotMap) ->
        #     self.__wordBank  :=  a WordBank instance representing
        #         the words in rawWords
        #   else -> raise SyntaxError ]
        self.__digestWords ( rawWords )