def setUp(self): self.grid = [[None, None, None, 2, 6, None, 7, None, 1], [6, 8, None, None, 7, None, None, 9, None], [1, 9, None, None, None, 4, 5, None, None], [8, 2, None, 1, None, None, None, 4, None], [None, None, 4, 6, None, 2, 9, None, None], [None, 5, None, None, None, 3, None, 2, 8], [None, None, 9, 3, None, None, None, 7, 4], [None, 4, None, None, 5, None, None, 3, 6], [7, None, 3, None, 1, 8, None, None, None]] self.doku = Sudoku(self.grid)
def test_string_to_grid(self): test_s = Sudoku() test_s.grid = test_s.string_to_grid( ".........;..6..1.38;.2.9.3.1.;..8...42.;" "....4.69.;.54......;...53..6.;.1269.7.4;.6.......;") assert type( test_s.string_to_grid( ".........;..6..1.38;.2.9.3.1.;..8...42.;" "....4.69.;.54......;...53..6.;.1269.7.4;.6.......;")) == list for i in range(0, 9): assert test_s.grid[i] == s_27.grid[i]
def test_find_none_v2(self): grid = [[5, 9, 6, 2, 6, 7, 7, 8, 1], [6, 8, 6, None, 7, None, None, 9, None], [1, 9, None, None, None, 4, 5, None, None], [8, 2, None, 1, None, None, None, 4, None], [None, None, 4, 6, None, 2, 9, None, None], [None, 5, None, None, None, 3, None, 2, 8], [None, None, 9, 3, None, None, None, 7, 4], [None, 4, None, None, 5, None, None, 3, 6], [7, None, 3, None, 1, 8, None, None, None]] self.doku_2 = Sudoku(grid) self.assertEqual((1, 3), self.doku_2.find_next_empty())
def solve(sudoku: Sudoku, row, col): if col == 9: if row == 8: print(sudoku) return solve(sudoku, row + 1, 0) return if sudoku.sudoku_finished(): print(sudoku.sudoku) return if sudoku.needs_solving(row, col): for value in range(1, 10): new_sudoku = sudoku.place_value(row, col, value) if not new_sudoku.sudoku_has_errors(): solve(new_sudoku, row, col + 1) else: solve(sudoku, row, col + 1) sudoku.place_value(row, col, 0)
def test_generate_sudoku(self): # Check whether the correct amount of empty cells was removed # / generated. s_gen_1 = Sudoku() s_gen_1.generate_full_sudoku() empty_cells = 48 s_gen_1.generate_sudoku(empty_cells) counter = 0 for y in range(0, 9): for x in range(0, 9): if s_gen_1.grid[y][x] == 0: counter += 1 if s_gen_1.solved_grid[y][x] == 0: counter += 1 assert counter == empty_cells # Test for valid solution. generated_solution = s_gen_1.solved_grid s_gen_1.solved_grid = s_gen_1.empty_grid s_gen_1.quick_solve(False) assert generated_solution == s_gen_1.solved_grid # Test whether the generated Sudoku is valid assert s_gen_1.validate() is True
from numpy import random import src.sudoku_solver as solver from src.sudoku import Sudoku starting_sudoku = Sudoku([[0, 0, 0, 0, 0, 0, 2, 0, 0], [0, 8, 0, 0, 0, 7, 0, 9, 0], [6, 0, 2, 0, 0, 0, 5, 0, 0], [0, 7, 0, 0, 6, 0, 0, 0, 0], [0, 0, 0, 9, 0, 1, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 4, 0], [0, 0, 5, 0, 0, 0, 6, 0, 3], [0, 9, 0, 4, 0, 0, 0, 7, 0], [0, 0, 6, 0, 0, 0, 0, 0, 0]]) starting_sudoku2 = Sudoku([[0, 7, 0, 0, 0, 0, 0, 0, 9], [5, 1, 0, 4, 2, 0, 6, 0, 0], [0, 8, 0, 3, 0, 0, 7, 0, 0], [0, 0, 8, 0, 0, 1, 3, 7, 0], [0, 2, 3, 0, 8, 0, 0, 4, 0], [4, 0, 0, 9, 0, 0, 1, 0, 0], [9, 6, 2, 8, 0, 0, 0, 3, 0], [0, 0, 0, 0, 1, 0, 4, 0, 0], [7, 0, 0, 2, 0, 3, 0, 9, 6]]) solver.solve(starting_sudoku2, 0, 0)
def setUpClass(cls): cls.sudoku = Sudoku()
# Example sudoku 2; Can't be solved by SudokuSolver: matrix2 = [[[], [9], [], [7], [], [4], [], [], []], [[], [], [8], [], [2], [], [7], [], []], [[], [], [2], [], [6], [], [], [], [9]], [[1], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [7], [6]], [[2], [], [3], [], [7], [], [1], [], []], [[], [], [], [], [8], [], [4], [], [7]], [[3], [2], [7], [], [], [], [], [9], []], [[], [4], [], [1], [], [], [6], [], []]] # Example sudoku 3; Can be solved by SudokuSolver: matrix3 = [[[], [9], [], [], [2], [], [], [], [4]], [[6], [1], [], [4], [], [9], [], [3], []], [[4], [], [], [], [5], [], [9], [7], []], [[], [], [1], [], [6], [4], [], [], [9]], [[], [2], [], [], [], [5], [1], [], []], [[], [6], [8], [], [7], [3], [], [2], []], [[], [], [9], [7], [], [6], [2], [5], []], [[1], [], [], [5], [], [2], [], [], []], [[], [3], [5], [], [], [], [], [], [7]]] sudoku = Sudoku.makeSudoku(matrix3) print(sudoku.getMatrix()) solvesudoku = sudoku.makeSudoku(SudokuSolver.actuallySolve(sudoku).getMatrix()) print("solved sudoku:", solvesudoku.getMatrix())
if __name__ == '__main__': # test1 with open('tests/easy_tests.json', 'r') as test_file: tests = json.loads(test_file.read()) game = [[8, 0, 0, 5, 7, 4, 3, 1, 2], [0, 5, 4, 0, 0, 0, 8, 7, 6], [0, 3, 0, 0, 0, 0, 4, 5, 9], [6, 8, 3, 1, 9, 5, 2, 4, 7], [4, 0, 0, 3, 8, 2, 1, 6, 5], [5, 1, 2, 7, 4, 6, 9, 8, 3], [0, 4, 0, 0, 0, 0, 5, 2, 1], [0, 0, 0, 4, 5, 0, 0, 9, 8], [0, 0, 5, 0, 1, 0, 0, 3, 4]] temp_timer = Timer('temp') for _ in range(10): temp = Sudoku(tests[0]) # cellwise(temp) # print(temp) num_empty = temp.number_empty() temp_timer.start() print(temp) done, guesses = random_backtracking(temp, test=True) # done, guesses = random_priority_backtracking_manual(temp, test=False) print(guesses) print(temp) # print(backtracking(temp)) # print(numberwise_backtracking(temp)) # print(priority_backtracking_manual(temp)) # done, guesses = human_mixed_backtracking(temp, numberwise, test=False) # done, guesses = human_mixed_priority_backtracking_manual(temp, numberwise, test=False) # print(guesses / num_empty)
from src.sudoku import Sudoku # 20 given fields. Currently not solvable within a reasonable time frame. s_20: Sudoku = Sudoku([[0, 3, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 9, 5, 0, 0, 0], [0, 0, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 0], [4, 0, 0, 8, 0, 0, 0, 0, 1], [0, 0, 0, 0, 2, 0, 0, 0, 0], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 7, 0]]) # 27 given fields s_27: Sudoku = Sudoku([[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 6, 0, 0, 1, 0, 3, 8], [0, 2, 0, 9, 0, 3, 0, 1, 0], [0, 0, 8, 0, 0, 0, 4, 2, 0], [0, 0, 0, 0, 4, 0, 6, 9, 0], [0, 5, 4, 0, 0, 0, 0, 0, 0], [0, 0, 0, 5, 3, 0, 0, 6, 0], [0, 1, 2, 6, 9, 0, 7, 0, 4], [0, 6, 0, 0, 0, 0, 0, 0, 0]]) # 32 given fields s_32: Sudoku = Sudoku([[0, 9, 2, 0, 0, 0, 8, 7, 0], [6, 0, 0, 4, 0, 7, 0, 0, 2], [3, 0, 0, 5, 0, 2, 0, 0, 9], [0, 7, 1, 0, 0, 0, 5, 2, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 4, 6, 0, 0, 0, 9, 8, 0], [7, 0, 0, 1, 0, 5, 0, 0,
import src.sudoku_solver as sudoku_solver from src.sudoku import Sudoku correct_sudoku = Sudoku([[9, 5, 7, 6, 1, 3, 2, 8, 4], [4, 8, 3, 2, 5, 7, 1, 9, 6], [6, 1, 2, 8, 4, 9, 5, 3, 7], [1, 7, 8, 3, 6, 4, 9, 5, 2], [5, 2, 4, 9, 7, 1, 3, 6, 8], [3, 6, 9, 5, 2, 8, 7, 4, 1], [8, 4, 5, 7, 9, 2, 6, 1, 3], [2, 9, 1, 4, 3, 6, 8, 7, 5], [7, 3, 6, 1, 8, 5, 4, 2, 9]]) starting_sudoku = Sudoku([[0, 0, 0, 0, 0, 0, 2, 0, 0], [0, 8, 0, 0, 0, 7, 0, 9, 0], [6, 0, 2, 0, 0, 0, 5, 0, 0], [0, 7, 0, 0, 6, 0, 0, 0, 0], [0, 0, 0, 9, 0, 1, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 4, 0], [0, 0, 5, 0, 0, 0, 6, 0, 3], [0, 9, 0, 4, 0, 0, 0, 7, 0], [0, 0, 6, 0, 0, 0, 0, 0, 0]]) starting_sudoku_current_cell_test = Sudoku([[1, 3, 4, 5, 6, 7, 2, 0, 0], [0, 8, 0, 0, 0, 7, 0, 9, 0], [6, 0, 2, 0, 0, 0, 5, 0, 0], [0, 7, 0, 0, 6, 0, 0, 0, 0], [0, 0, 0, 9, 0, 1, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 4, 0], [0, 0, 5, 0, 0, 0, 6, 0, 3], [0, 9, 0, 4, 0, 0, 0, 7, 0],
def test_sudoku_valid_format(valid_sudoku): assert Sudoku(valid_sudoku).is_valid()
def test_partial_sudoku(masked_valid_sudoku): assert Sudoku(masked_valid_sudoku).is_valid()
def test_sudoku_outlier_numbers(valid_sudoku): invalid_sudoku = valid_sudoku invalid_sudoku[0][0] = 12 with pytest.raises(ValueError) as e_info: Sudoku(invalid_sudoku)
def test_sudoku_invalid_format(): with pytest.raises(ValueError) as e_info: Sudoku(np.array([1, 2, 3]))
class TestSudoku(unittest.TestCase): def setUp(self): self.grid = [[None, None, None, 2, 6, None, 7, None, 1], [6, 8, None, None, 7, None, None, 9, None], [1, 9, None, None, None, 4, 5, None, None], [8, 2, None, 1, None, None, None, 4, None], [None, None, 4, 6, None, 2, 9, None, None], [None, 5, None, None, None, 3, None, 2, 8], [None, None, 9, 3, None, None, None, 7, 4], [None, 4, None, None, 5, None, None, 3, 6], [7, None, 3, None, 1, 8, None, None, None]] self.doku = Sudoku(self.grid) def test_find_none(self): self.assertEqual((0, 0), self.doku.find_next_empty()) def test_find_none_v2(self): grid = [[5, 9, 6, 2, 6, 7, 7, 8, 1], [6, 8, 6, None, 7, None, None, 9, None], [1, 9, None, None, None, 4, 5, None, None], [8, 2, None, 1, None, None, None, 4, None], [None, None, 4, 6, None, 2, 9, None, None], [None, 5, None, None, None, 3, None, 2, 8], [None, None, 9, 3, None, None, None, 7, 4], [None, 4, None, None, 5, None, None, 3, 6], [7, None, 3, None, 1, 8, None, None, None]] self.doku_2 = Sudoku(grid) self.assertEqual((1, 3), self.doku_2.find_next_empty()) def test_check_in_row_true(self): self.assertEqual(True, self.doku.check_in_row(2, 0)) def test_check_in_row_false(self): self.assertEqual(False, self.doku.check_in_row(3, 0)) def test_check_in_column_true(self): self.assertEqual(True, self.doku.check_in_column(1, 0)) def test_check_in_column_false(self): self.assertEqual(False, self.doku.check_in_column(4, 0)) def test_check_in_square_true(self): self.assertEqual(True, self.doku.check_in_square(6, 0, 0)) def test_check_in_square_false(self): self.assertEqual(False, self.doku.check_in_square(8, 6, 6)) def test_construct_inner_matrix_0_0(self): self.assertEqual([[None, None, None], [6, 8, None], [1, 9, None]], self.doku.construct_inner_matrix(1, 1)) def test_construct_inner_matrix_2_2(self): self.assertEqual([[None, None, None], [6, 8, None], [1, 9, None]], self.doku.construct_inner_matrix(2, 2)) def test_construct_inner_matrix_3_3(self): self.assertEqual([[1, None, None], [6, None, 2], [None, None, 3]], self.doku.construct_inner_matrix(3, 3)) def test_check_valid_true(self): self.assertEqual(True, self.doku.check_valid(5, 0, 0)) def test_check_valid_false(self): self.assertEqual(False, self.doku.check_valid(8, 0, 0)) def test_solve(self): solution = [[4, 3, 5, 2, 6, 9, 7, 8, 1], [6, 8, 2, 5, 7, 1, 4, 9, 3], [1, 9, 7, 8, 3, 4, 5, 6, 2], [8, 2, 6, 1, 9, 5, 3, 4, 7], [3, 7, 4, 6, 8, 2, 9, 1, 5], [9, 5, 1, 7, 4, 3, 6, 2, 8], [5, 1, 9, 3, 2, 6, 8, 7, 4], [2, 4, 8, 9, 5, 7, 1, 3, 6], [7, 6, 3, 4, 1, 8, 2, 5, 9]] self.assertEqual(solution, self.doku.solve())
def test_generate_full_sudoku(self): s_gen_1 = Sudoku() s_gen_1.generate_full_sudoku() assert s_gen_1.validate() is True s_gen_2 = Sudoku() s_gen_2.generate_full_sudoku() assert s_gen_2.validate() is True s_gen_3 = Sudoku() s_gen_3.generate_full_sudoku() assert s_gen_3.validate() is True
def main(): # Initialize root window and start the loop root = tk.Tk() Gui(root, Sudoku()) root.mainloop()
from src.sudoku import Sudoku import numpy as np if __name__ == '__main__': s = Sudoku(25) print(np.array(s.new_game())) print(np.array(s.board)) # s.get()