Next / Previous / Contents / Shipman's homepage

5.10. SudokuSolver.writeRow(): Write one row of the puzzle

This function writes one row of the puzzle's state to the given outFile. For the overall output design, see Section 5.9, “SudokuSolver.write(): Output the state of the puzzle”.

sudosolver.py
# - - -   S u d o k u S o l v e r . w r i t e R o w   - - -

    def writeRow ( self, outFile, rowx ):
        """Display one row in plain text.

          [ (outFile is a writeable file) and
            (rowx is in [0,MAT_L) ->
              outFile  +:=  a representation of row rowx of self ]
        """

        #-- 1 --
        for  colx in range(MAT_L):
            #-- 1 body --
            # [ sys.stdout  +:=  a representation of the cell at
            #       row rowx and column colx ]

            #-- 1.1 --
            # [ c  :=  self's cell at (rowx,colx) in character
            #          form ]
            cell  =  self.get ( rowx, colx )
            if  cell == 0:  c = '.'
            else:           c = chr ( ord ( '0' ) + cell )

            #-- 1.2 --
            # [ sys.stdout  +:=  c ]
            if  ( ( colx > 0 ) and
                  ( ( colx % SUBMAT_L ) == 0 ) ):
                print >>outFile, " ",
            print >>outFile, c,

Having written the line's contents, it is now our job to terminate it.

sudosolver.py
        #-- 2 --
        print >>outFile

For a discussion of the verification of this method, see Section 7.5, “Inspection: SudokuSolver.writeRow().