Next / Previous / Contents / Shipman's homepage

27. Puzzle.show(): Display the state of the puzzle

kkck
# - - -   P u z z l e . s h o w

    def show ( self ):
        '''Return the puzzle state as a multi-line string.
        '''

This method will be able to show the state of the puzzle anytime after the constructor completes.

During checkout, we'll need to be able to relate row and column numbers to positions in the puzzle. Accordingly, this method will add row and column numbers to the display of the puzzle's current state. We'll double-space the horizontal dimension to make the output less crowded. Here's the output that we will generate for the example puzzle:

   0
   0 1 2 3 4 5
  +-----------+
00|K R I S S  |
01|R   D      |
02|O   Y      |
03|S O L V E R|
04|S   L      |
  +-----------+

It's probably safe to assume that we'll never see a puzzle bigger than 99x99, so the row and column numbers will have a fixed format. Also, the first line will show the tens digit of the column number only when it changes, so in the general case this row will contain 0, nine spaces, 1, nine spaces, 2, and so on.

We'll build up the output as a list of lines, and then concatenate them all at the end with newlines. See Section 28, “Puzzle.__showColHeads(): Column headings for .show(); Section 29, “Puzzle.__showRule(): Horizontal rule in .show() output; and Section 30, “Puzzle.__showRow(): Show one row of the puzzle.

kkck
        #-- 1 --
        rowList = []

        #-- 2 --
        # [ rowList  +:=  (tens digit line) + (units digit line)
        #                 (frame line), all sized for self.size ]
        self.__showColHeads ( rowList )
        self.__showRule ( rowList )

        #-- 3 --
        for rowx in range(self.size[0]):
            #-- 3 body --
            # [ rowx is a row index in self ->
            #     rowList  +:=  row labels + frame + (puzzle state
            #                   for row (rowx)) + frame ]
            self.__showRow ( rowList, rowx )

        #-- 4 --
        # [ rowList  +:=  (frame line) ]
        self.__showRule ( rowList )

        #-- 5 --
        if VERBOSE:
            self.showAllChoices()
        return '\n'.join ( rowList )