Exemple #1
0
def solve(gameBoard):
    """ Run the sudoku solving algorithm on the given Board. """
    
    if gameBoard.isFull(): return gameBoard
    
    chosenSquare = mrv.ordering(gameBoard)
    for someValue in lcv.ordering(gameBoard, chosenSquare):
        
        # Place the provisional value on a test board.
        testBoard = gameBoard.copy()
        testBoard.placeValueAt(someValue, chosenSquare)
        
        # Winnow domains by forward checking:
        fwdcheck.checkBoard(testBoard, chosenSquare)
        if not testBoard.areAllDomainsOpen(): continue
        if runStats: bookkeeping.boardsPassedFwdcheck += 1
        
        # Winnow domains further by AC3:
        ac3.checkBoard(testBoard)
        if not testBoard.areAllDomainsOpen(): continue
        if runStats: bookkeeping.boardsPassedAc3 += 1
        
        # Provisional value OK,
        # punt down and try next square.
        if doProg: bookkeeping.reportPunt()
        puntBoard = solve(testBoard)    # <--------- Recursive Call
        if doProg: bookkeeping.reportShoonk()
        if puntBoard is not None: return puntBoard
        
        
    if runStats: bookkeeping.impossibleBoards += 1
    return None  # Implicitly no solution
Exemple #2
0
 def winnowDomainsByExistingValues(self):
     """
     Used to establish existing domains
     when a brand new (uncopied) board is created.
     """
     for someSquare in [(row, col) for row in range(9) for col in range(9)
                        if (self.values[row][col] is not None)]:
         fwdcheck.checkBoard(self, someSquare)