Пример #1
0
    def solve(self):

        """Attempts to solve the puzzle, returning either None if there is no solution, or a board with the correct MISSING entries."""
        solution = None

        #we use push and pop so that we can solve (variants) repeatedly without having to start from scratch each time.
        self.context.push()

        self.__addFacts()

        smt_stat = self.context.check_context(None)

        if smt_stat != Status.SAT:
            print 'No solution: smt_stat = {0}\n'.format(smt_stat)
        else:
            #get the model
            model = Model.from_context(self.context, 1)

            #return the model as a board with ONLY the newly found values inserted.
            solution = SudokuBoard.newBoard()

            for i in xrange(9):
                for j in xrange(9):
                    if self.game.puzzle[i][j] == 0:
                        solution[i][j] = model.get_value(self.variables[i][j])

            model.dispose()

        self.context.pop()

        return solution
Пример #2
0
 def __createVariables(self):
     """Creates the matrix of uninterpreted terms that represents the logical view of the board."""
     int_t = Types.int_type()
     variables = SudokuBoard.newBoard()
     for i in xrange(9):
         for j in xrange(9):
             variables[i][j] = Terms.new_uninterpreted_term(int_t)
     return variables