Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
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