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!
Example #2
0
 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))
Example #3
0
def create_new_sudoku():
    sudoku = Sudoku()
    valor = False

    conteo_buenas = 0
    conteo_malas = 0

    ini_time = default_timer()
    end_time = 0

    while not valor:
        sus = principal_algorithm(sudoku)
        valor = sudoku.validate_grid(sus)

    #----------------------------------------------------
    # for i in range(1000):
    #     sus = generate_new_sudoku(sudoku)
    #     valor = sudoku.validate_grid(sus)
    #     if valor:
    #         conteo_buenas += 1
    #     else:
    #         conteo_malas += 1
    #----------------------------------------------------
    end_time = default_timer()
    print(end_time - ini_time, "/", conteo_buenas, "/", conteo_malas)
    return sus
Example #4
0
    def setUp(self):
        sg = SudokuGames(end=10)
        # Sample game #3 (hard)
        self.sud_hard_input = table = sg.samples[2]
        self.sud_hard = Sudoku(table, solve=True)

        sud17 = sg.sudoku17
        # First puzzle in the Sudoku-17 list
        self.sud_17_0_input = table = sud17[(*sud17, )[0]]
        self.sud_17_0 = Sudoku(table, solve=True)
Example #5
0
    def test_create(self):
        sudoku_1 = Sudoku.create(self.grid_zeros)
        self.assertTrue(np.all(sudoku_1.grid == 0))

        sudoku_2 = Sudoku.create(self.grid_two_by_two)
        self.assertTrue(np.all((sudoku_2.grid - self.grid_two_by_two) == 0))

        self.assertRaises(ValueError, Sudoku.create, self.grid_wrong_shape)

        self.assertRaises(ValueError, Sudoku.create, self.grid_wrong_element)
        pass
Example #6
0
 def __init__(self,
              problem: Sudoku,
              coordinate_candidates: Dict[Tuple[int, int],
                                          Set[int]] = None):
     self.problem = problem.__copy__()  # type: Sudoku
     self.solution = problem.__copy__()  # type: Sudoku
     cc = coordinate_candidates if coordinate_candidates is not None \
         else {c: self.solution.scan_candidates(c) for c in self.solution.coord_dim.keys()}
     self.coord_candidates = {c: cds
                              for c, cds in cc.items() if bool(cds)
                              }  # type:Dict[Tuple[int, int], Set[int]]
     pass
Example #7
0
def solve():
    """
    Operation to attempt to solve the sudoku puzzle that the GUI represents.

    If there are any error within the sudoku or the puzzle cannot be solved an
    alert pops up, otherwise the GUI is updated with the solved sudoku puzzle.
    """
    # Converts GUI puzzle to Sudoku instance
    if validate_inputs():
        sudoku = Sudoku(SUDOKU)
        if sudoku.is_valid():
            if sudoku.solve():
                update_gui(sudoku)
            else:
                messagebox.showerror(title="Error", message="Cannot solve.")
                update_gui(sudoku)
        else:
            messagebox.showerror(title="Error",
                                 message="This sudoku is invalid.")
Example #8
0
    def test_fill_coord(self):
        sudoku = Sudoku.create(self.grid_two_by_two)
        self.assertFalse(sudoku.fill_coord((0, 0), 12))  # invalid value
        self.assertFalse(sudoku.fill_coord((12, 12), 0))  # invalid coordinate
        self.assertFalse(sudoku.fill_coord((0, 0),
                                           3))  # violate consistency rule

        self.assertTrue(sudoku.grid[(3, 3)] == 0)
        self.assertTrue(sudoku.fill_coord((3, 3), 2))
        self.assertTrue(sudoku.grid[(3, 3)] == 2)
        pass
Example #9
0
def update_gui(sudoku: Sudoku):
    """
    Operation to update the GUI to the state given as a parameter.
    @param sudoku:  The sudoku states to change the GUI to
    @type sudoku:   Sudoku
    """
    for i in range(0, 9, 1):
        for j in range(0, 9, 1):
            try:
                SUDOKU[i][j].current(sudoku.get_box(i, j).get_value())
            except tk.TclError:
                SUDOKU[i][j].current(0)
Example #10
0
 def test_validate_coord(self):
     sudoku = Sudoku.create(self.grid_two_by_two)
     self.assertTrue(
         sudoku.validate_coord([(rdx, cdx) for rdx in range(4)
                                for cdx in range(4)]))
     sudoku.grid[(0, 0)] = 2  # duplicated value
     self.assertFalse(
         sudoku.validate_coord([(0, 0), (0, 1), (0, 2), (0, 3), (1, 0),
                                (2, 0), (3, 0), (1, 1)]))
     self.assertTrue(
         sudoku.validate_coord([(1, 2), (1, 3), (2, 1), (2, 2), (2, 3),
                                (3, 1), (3, 2), (3, 3)]))
     pass
Example #11
0
 def test_get_get_relevant_coordinates(self):
     sudoku = Sudoku.create(self.grid_two_by_two)
     coordinates_0_0 = sudoku.get_relevant_coordinates((0, 0))
     self.assertTrue(coordinates_0_0.__len__() == 0)
     coordinates_2_1 = sudoku.get_relevant_coordinates((2, 1))
     self.assertTrue(coordinates_2_1.__len__() == 3)
     coordinates_2_1_sf = sudoku.get_relevant_coordinates((2, 1),
                                                          skip_filled=False,
                                                          skip_self=True)
     self.assertTrue(coordinates_2_1_sf.__len__() == 7)
     coordinates_2_1_ss = sudoku.get_relevant_coordinates((2, 1),
                                                          skip_filled=True,
                                                          skip_self=False)
     self.assertTrue(coordinates_2_1_ss.__len__() == 4)
     coordinates_2_1_sf_ss = sudoku.get_relevant_coordinates(
         (2, 1), skip_filled=False, skip_self=False)
     self.assertTrue(coordinates_2_1_sf_ss.__len__() == 8)
     pass
Example #12
0
 def test_get_vec(self):
     sudoku = Sudoku.create(self.grid_two_by_two)
     block_indices = [[(0, 0), (0, 1), (1, 0), (1, 1)],
                      [(0, 2), (0, 3), (1, 2), (1, 3)],
                      [(2, 0), (2, 1), (3, 0), (3, 1)],
                      [(2, 2), (2, 3), (3, 2), (3, 3)]]
     for n in range(sudoku.size):
         vec_row = sudoku.get_vec(n, SdkVec.Row)
         grd_row = np.array(self.grid_two_by_two[n])
         self.assertTrue(np.all((grd_row - vec_row) == 0))
         vec_col = sudoku.get_vec(n, SdkVec.Column)
         grd_col = np.array(
             [self.grid_two_by_two[rdx][n] for rdx in range(4)])
         self.assertTrue(np.all((grd_col - vec_col == 0)))
         vec_blc = sudoku.get_vec(n, SdkVec.Block)
         grid_blc = np.array([
             self.grid_two_by_two[rdx][cdx] for rdx, cdx in block_indices[n]
         ])
         self.assertTrue(np.all((grid_blc - vec_blc) == 0))
     pass
Example #13
0
    ]
    sudoku_raw = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [4, 5, 6, 7, 0, 0, 0, 0, 0],
        [7, 8, 9, 1, 2, 3, 4, 5, 6],
        #
        [2, 3, 4, 5, 6, 7, 8, 9, 1],
        [5, 6, 7, 8, 9, 1, 2, 3, 4],
        [8, 9, 1, 2, 3, 4, 5, 6, 7],
        #
        [3, 4, 5, 6, 7, 8, 9, 1, 2],
        [6, 7, 8, 9, 1, 2, 3, 4, 5],
        [9, 1, 2, 3, 4, 5, 6, 7, 8]
    ]

    sudoku = Sudoku.from_list(sudoku_raw)
    print(sudoku)
    print([field.number for field in sudoku.get_row(8)])
    print([field.number for field in sudoku.get_column(8)])
    # printslice(sudoku.get_quadrant(2, 2))
    print([field.number for field in sudoku.get_quadrant(2, 2)])
    print(sudoku.check_quadrant(0, 0))
    print(sudoku.check_quadrant(2, 2))
    print(sudoku.check_column(0))
    print(sudoku.check_row(0))
    print(sudoku.get_state())
    sudoku.solve()
    print(sudoku)

    # sudoku2_raw = [
    #     [3, 9, 0, 6, 0, 0, 0, 4, 7],
Example #14
0
def test_sudoku():

    sdkinstance = Sudoku()
    sdkinstance.printBoard(sdkinstance.test2)
    sdkinstance.userReplace(sdkinstance.userFind, sdkinstance.test2)
    sdkinstance.printBoard(sdkinstance.test2)
Example #15
0
    # for i in range(1000):
    #     sus = generate_new_sudoku(sudoku)
    #     valor = sudoku.validate_grid(sus)
    #     if valor:
    #         conteo_buenas += 1
    #     else:
    #         conteo_malas += 1
    #----------------------------------------------------
    end_time = default_timer()
    print(end_time - ini_time, "/", conteo_buenas, "/", conteo_malas)
    return sus


# Principal main
if __name__ == "__main__":
    sudoku = Sudoku()
    valor = False

    conteo_buenas = 0
    conteo_malas = 0

    ini_time = default_timer()
    end_time = 0

    # while not valor:
    #     sus = principal_algorithm(sudoku)
    #     valor = sudoku.validate_grid(sus)

    # ----------------------------------------------------
    for i in range(1000):
        sus = principal_algorithm(sudoku)
Example #16
0
from sudoku.sudoku import Sudoku

print("Test avec grille de sudoku")

file = open("fichiers/puzzles.sdk", 'r')
board = file.readline().replace('\n', '')
file.close()
#sudoku = Sudoku("029405006640082000000000000908060200000000000005040803000000000000530021500109430")
sudoku = Sudoku(
    "100000000000010000000000000000000001000000000000000000000000000000000000000000100"
)
#sudoku = Sudoku(board)
sudoku.display()
if sudoku.solve():
    sudoku.display()
Example #17
0
from sudoku.sudoku import Sudoku

puzzle = open("fichiers/puzzles.sdk", 'r')
res_puzzle = open("fichiers/puzzle_result.sdk", 'a')

i = 0
for line in puzzle:
    i += 1
    line = line.replace('\n', '')
    sudo = Sudoku(line)
    if sudo.solve():
        res_puzzle.write("{}\n".format(sudo))
        print(i)
puzzle.close()
res_puzzle.close()
Example #18
0
import unittest
from sudoku.sudoku import Sudoku

sudo = Sudoku("200000060000075030048090100000300000300010009000008000001020570080730000090000004")

"""
    pour info 
board_array = [[2 0 0, 0 0 0, 0 6 0],
               [0 0 0, 0 7 5, 0 3 0],
               [0 4 8, 0 9 0, 1 0 0],
               
               [0 0 0, 3 0 0, 0 0 0],
               [3 0 0, 0 1 0, 0 0 9],
               [0 0 0, 0 0 8, 0 0 0],
               
               [0 0 1, 0 2 0, 5 7 0],
               [0 8 0, 7 3 0, 0 0 0],
               [0 9 0, 0 0 0, 0 0 4]]
"""


class MyTestSudoku(unittest.TestCase):

    def test_is_in_row(self):
        self.assertEqual(sudo.is_in_row(0, 3), False)
        self.assertEqual(sudo.is_in_row(0, 2), True)
        self.assertEqual(sudo.is_in_row(1, 7), True)
        self.assertEqual(sudo.is_in_row(8, 4), True)

    def test_is_in_col(self):
        self.assertEqual(sudo.is_in_col(0, 3), True)
Example #19
0
    def setUp(self) -> None:
        grid_no_trial_needed = [[0, 6, 2, 0, 0, 5, 0, 0, 0],
                                [0, 0, 9, 0, 0, 6, 1, 0, 0],
                                [0, 3, 0, 2, 0, 9, 0, 6, 0],
                                [9, 2, 0, 0, 4, 0, 0, 0, 6],
                                [5, 0, 0, 0, 8, 0, 0, 0, 3],
                                [8, 0, 0, 0, 9, 0, 0, 4, 7],
                                [0, 5, 0, 9, 0, 1, 0, 3, 0],
                                [0, 0, 7, 3, 0, 0, 6, 0, 0],
                                [0, 0, 0, 7, 0, 0, 8, 5, 0]]
        self.solver_no_trial_needed = SudokuSolver(
            Sudoku.create(grid_no_trial_needed))

        grid_two_by_two = [[1, 2, 0, 0], [3, 4, 0, 0], [0, 0, 0, 0],
                           [0, 0, 0, 0]]
        self.solver_two_by_two = SudokuSolver(Sudoku.create(grid_two_by_two))

        grid_three_by_three = [[5, 6, 0, 1, 0, 0, 0, 3, 0],
                               [9, 0, 0, 0, 2, 0, 6, 0, 0],
                               [0, 1, 0, 0, 0, 0, 0, 0, 2],
                               [0, 0, 3, 6, 0, 0, 7, 0, 9],
                               [0, 0, 0, 0, 8, 0, 0, 0, 4],
                               [0, 5, 0, 0, 0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 3, 0, 0, 0, 0],
                               [0, 0, 0, 0, 4, 0, 0, 0, 0],
                               [0, 2, 7, 0, 6, 0, 0, 1, 3]]
        self.solver_three_by_three = SudokuSolver(
            Sudoku.create(grid_three_by_three))

        grid_four_by_four = [
            [0, 11, 0, 0, 0, 14, 8, 5, 0, 1, 0, 0, 0, 6, 0, 15],
            [1, 15, 0, 0, 3, 2, 0, 0, 0, 13, 0, 16, 8, 0, 0, 10],
            [4, 14, 9, 0, 0, 13, 10, 12, 0, 5, 8, 15, 0, 3, 2, 11],
            [0, 12, 0, 8, 7, 15, 9, 0, 3, 0, 2, 11, 1, 14, 0, 0],
            [0, 0, 11, 15, 0, 0, 0, 0, 0, 0, 14, 0, 5, 4, 0, 1],
            [0, 0, 0, 0, 12, 3, 15, 14, 10, 2, 0, 1, 9, 8, 0, 0],
            [7, 8, 12, 0, 0, 0, 0, 1, 15, 6, 0, 4, 14, 0, 13, 3],
            [3, 0, 13, 0, 10, 0, 0, 9, 8, 7, 0, 0, 0, 0, 6, 16],
            [15, 2, 10, 13, 5, 16, 0, 0, 0, 0, 7, 0, 3, 11, 0, 8],
            [8, 3, 16, 11, 9, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0],
            [0, 4, 0, 12, 0, 0, 0, 0, 0, 11, 0, 10, 0, 16, 14, 0],
            [0, 0, 0, 1, 2, 10, 0, 0, 0, 0, 0, 8, 4, 0, 0, 12],
            [16, 0, 0, 4, 14, 0, 0, 0, 0, 0, 1, 0, 6, 15, 0, 0],
            [0, 0, 0, 0, 0, 9, 0, 0, 0, 16, 5, 0, 13, 0, 7, 14],
            [0, 7, 2, 0, 6, 1, 13, 0, 9, 8, 15, 0, 11, 10, 16, 4],
            [0, 10, 1, 9, 0, 0, 16, 0, 0, 3, 0, 13, 0, 0, 0, 2]
        ]
        self.solver_four_by_four = SudokuSolver(
            Sudoku.create(grid_four_by_four))

        grid_medium = [[0, 8, 0, 0, 2, 0, 5, 6,
                        0], [0, 0, 0, 1, 0, 0, 0, 0, 7],
                       [0, 0, 0, 0, 0, 0, 0, 0,
                        0], [0, 5, 0, 0, 9, 0, 4, 0, 8],
                       [0, 0, 7, 8, 0, 0, 0, 0,
                        3], [0, 9, 0, 0, 1, 0, 0, 5, 0],
                       [2, 0, 4, 0, 0, 0, 8, 0,
                        0], [0, 6, 0, 0, 8, 5, 0, 0, 0],
                       [0, 0, 0, 2, 0, 0, 1, 0, 0]]
        self.solver_medium = SudokuSolver(Sudoku.create(grid_medium))

        grid_hard = [[0, 0, 5, 0, 0, 8, 0, 0, 0], [1, 0, 6, 0, 0, 3, 0, 0, 4],
                     [0, 0, 2, 0, 0, 0, 0, 0, 7], [0, 0, 1, 0, 0, 4, 0, 0, 0],
                     [0, 0, 0, 0, 8, 0, 0, 6, 0], [0, 7, 0, 0, 0, 0, 0, 0, 0],
                     [9, 0, 0, 5, 3, 0, 0, 0, 0], [0, 0, 0, 6, 0, 0, 8, 0, 1],
                     [0, 0, 0, 0, 2, 0, 0, 4, 9]]
        self.solver_hard = SudokuSolver(Sudoku.create(grid_hard))

        grid_expert = [[0, 0, 0, 4, 0, 0, 0, 0,
                        0], [0, 5, 0, 7, 0, 0, 0, 0, 9],
                       [0, 0, 0, 9, 0, 8, 6, 0,
                        1], [9, 6, 4, 0, 0, 0, 0, 8, 0],
                       [0, 0, 7, 0, 0, 0, 9, 0,
                        0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [3, 1, 0, 0, 0, 2, 0, 0,
                        0], [0, 0, 0, 8, 0, 0, 0, 0, 7],
                       [2, 0, 0, 0, 0, 0, 1, 0, 3]]
        self.solver_expert = SudokuSolver(Sudoku.create(grid_expert))
        pass