Next / Previous / Contents / Shipman's homepage

5.18. SudokuSolver.__set(): Store a value in a cell

This method changes the value of a cell in the puzzle. It also notifies the state change observer callback if there is one.

sudosolver.py
# - - -   S u d o k u S o l v e r . _ _ s e t   - - -

    def __set ( self, x, value ):
        """Set or clear one cell of the board.

          [ (x is an integer in [0,BOARD_L)) and
            (value is an integer in [0,MAT_L]) ->
              if  self.changeObserver is not None ->
                notify self.changeObserver that cell x is being
                set to value
              in any case ->
                self.__board[x]  :=  value ]
        """

The interface specification stipulates that the change observer callback is called after the state has changed, so our first task is to store the new value in the board. We also track the number of state changes in self.nStateChanges.

sudosolver.py
        #-- 1 --
        self.__board[x]     =   value
        self.nStateChanges  +=  1

If there is a change observer callback, we have to translate the internal index x to row and column values. See Section 5.19, “SudokuSolver.__xToRowCol(): Translate board position to row and column”.

sudosolver.py
        #-- 2 --
        # [ if self.changeObserver is not None ->
        #     notify self.changeObserver that cell x is being
        #     set to value
        if  self.changeObserver is not None:
            row, col  =  self.__xToRowCol ( x )
            self.changeObserver ( self, row, col, value )