class SudokuPuzzle(): def __init__(self, size=9, custom=None, verbose=False, _diff=200, _retries=5): """ Accepts the following kwargs: size, custom and verbose. Uses these kwargs to initialise a 'solution' Sudoku, unless custom input was provided. In that case, an original sudoku is initialised and then solved to create the solution. """ self.size = size self.original = Sudoku(size=size, custom=custom, verbose=verbose) self.solution, branching = self.original.solve_smart( returnBranching=True) if custom is None: # Re-fill the original starting from the solution self.original, self.difficulty = self.solution.make_puzzle( diff=_diff, retry=_retries) self.calculation_time = self.original.solve_smart( ).calculation_time else: self.difficulty = self.original._diff_from_branching(branching) self.calculation_time = self.solution.calculation_time # Calc_time in ms! def generatePuzzleFromSolution(self): print(self.solution)
def test_easy(self): custom_input = [ [0, 0, 0, 8, 0, 4, 0, 5, 0], [8, 0, 7, 0, 5, 6, 4, 0, 1], [0, 6, 0, 1, 0, 0, 2, 0, 0], [0, 0, 0, 7, 6, 3, 0, 0, 0], [0, 7, 0, 0, 0, 0, 0, 4, 0], [0, 0, 0, 2, 4, 1, 0, 0, 0], [0, 0, 6, 0, 0, 5, 0, 2, 0], [9, 0, 5, 4, 7, 0, 6, 0, 8], [0, 8, 0, 6, 0, 9, 0, 0, 0], ] expected_result = [ [1, 9, 3, 8, 2, 4, 7, 5, 6], [8, 2, 7, 9, 5, 6, 4, 3, 1], [5, 6, 4, 1, 3, 7, 2, 8, 9], [2, 4, 8, 7, 6, 3, 1, 9, 5], [6, 7, 1, 5, 9, 8, 3, 4, 2], [3, 5, 9, 2, 4, 1, 8, 6, 7], [7, 1, 6, 3, 8, 5, 9, 2, 4], [9, 3, 5, 4, 7, 2, 6, 1, 8], [4, 8, 2, 6, 1, 9, 5, 7, 3], ] sudoku_example = Sudoku(size=9, custom=custom_input) sudoku_result = Sudoku(size=9, custom=expected_result) self.assertTrue(sudoku_example.solve_smart().equals(sudoku_result))