Next / Previous / Contents / Shipman's homepage

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   - - -

    def write ( self, outFile ):
        """Write the puzzle state as plain text.

          Output format (minus the enclosing box):
            +---------------------+
            |7 2 .   . . 5   1 . .|
            |. 1 5   9 8 .   4 . .|
            |. 8 .   . . 1   . . 6|
            |                     |
            |. 4 .   . 3 .   6 . 5|
            |3 . .   6 . 2   . . 7|
            |9 . 1   . 7 .   . 2 .|
            |                     |
            |5 . .   7 . .   . 9 .|
            |. . 2   . 9 6   3 5 .|
            |. . 4   2 . .   . 6 8|
            +---------------------+
        """

To generate the output, we run two stacked loops.

sudosolver.py
        #-- 1 --
        for  rowx in range(MAT_L):
            #-- 1 body --
            # [ rowx is in [0,MAT_L) ->
            #     outFile  +:=  a representation of row rowx of self ]

            #-- 1.1 --
            # [ if ( ( rowx > 0 ) and
            #        ( rowx % SUBMAT_L ) == 0 ) ) ->
            #     outFile  +:=  an empty line
            #   else -> I ]
            if  ( ( rowx > 0 ) and
                  ( ( rowx % SUBMAT_L ) == 0 ) ):
                print >> outFile

            #-- 1.2 --
            # [ outFile  +:=  a representation of row rowx of self ]
            self.writeRow ( outFile, rowx )

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