def test_brute_force_solve_sudoku(): board = Sudoku(boards.unsolved_easy) sudoku_solver.brute_force_solve_sudoku(board) assert (board.board_numbers == boards.solved_easy).all() board = Sudoku(boards.unsolved_hard) sudoku_solver.brute_force_solve_sudoku(board) assert (board.board_numbers == boards.solved_hard).all()
def test_find_empty_location(): board = Sudoku(boards.unsolved_easy) location = [0, 0] empty = sudoku_solver.find_empty_location(board.board_numbers, location) assert empty board = Sudoku(boards.solved_easy) location = [0, 0] empty = sudoku_solver.find_empty_location(board.board_numbers, location) assert not empty
def test_crosshatch_box(): sudoku_board = Sudoku(boards.unique_candidate_test) assert sudoku_board.board_numbers[7, 0] == 0 sudoku_solver.crosshatch_box(sudoku_board, 6) assert sudoku_board.board_numbers[7, 0] == 4 sudoku_board = Sudoku(boards.unsolved_hard) assert sudoku_board.board_numbers[5, 6] == 0 sudoku_solver.crosshatch_box(sudoku_board, 5) assert sudoku_board.board_numbers[5, 6] == 8
def test_solve_board(): board = Sudoku(boards.unsolved_easy) sudoku_solver.solve_board(board) assert sudoku_solver.verify_board(board) assert (board.board_numbers == boards.solved_easy).all() board = Sudoku(boards.unsolved_very_hard) sudoku_solver.solve_board(board) assert sudoku_solver.verify_board(board) assert (board.board_numbers == boards.solved_very_hard).all() board = Sudoku(boards.empty) sudoku_solver.solve_board(board) assert sudoku_solver.verify_board(board)
def test_check_location_is_safe(): board = Sudoku(boards.unsolved_easy) safe = sudoku_solver.check_location_is_safe(board.board_numbers, 0, 0, 1) assert safe safe = sudoku_solver.check_location_is_safe(board.board_numbers, 0, 0, 5) assert not safe
def test_add_cell_with_valid(): sudoku_board = Sudoku(boards.unsolved_easy) row = 3 column = 7 value = 5 sudoku_board.add_cell(row, column, value) assert sudoku_board.board_numbers[row][column] == value
def test_solve_naked_subset_column(): column = 0 row = 1 sudoku_board = Sudoku(boards.naked_pair_test) assert sudoku_board.board_numbers[row, column] == 0 sudoku_solver.solve_naked_subset_column(sudoku_board, column) assert sudoku_board.board_numbers[row, column] == 1
def test_used_in_box(): board = Sudoku(boards.unsolved_easy) used = sudoku_solver.used_in_box(board.board_numbers, 0, 0, 5) assert used used = sudoku_solver.used_in_box(board.board_numbers, 0, 0, 1) assert not used
def test_solve_naked_subset_row(): column = 1 row = 0 sudoku_board = Sudoku(boards.naked_pair_test) sudoku_board.board_numbers = sudoku_board.board_numbers.T assert sudoku_board.board_numbers[row, column] == 0 sudoku_solver.solve_naked_subset_row(sudoku_board, row) assert sudoku_board.board_numbers[row, column] == 1
def test_add_cell_with_invalid(): sudoku_board = Sudoku(boards.unsolved_easy) row = 3 column = 7 value = 's' with pytest.raises(Exception): # noinspection PyTypeChecker sudoku_board.add_cell(row, column, value)
def reset_clicked(self): board = boards.empty for row in range(9): for column in range(9): self.text_boxes[row][column].delete(0, tk.END) self.sudoku = Sudoku(board, self)
def test_get_possible_cell_values_with_unsolved_easy(): sudoku_board = Sudoku(boards.unsolved_easy) possible_values = sudoku_solver.get_possible_cell_values( sudoku_board, 0, 2) assert possible_values == [1, 2, 4] possible_values = sudoku_solver.get_possible_cell_values( sudoku_board, 0, 1) assert possible_values == [3] possible_values = sudoku_solver.get_possible_cell_values( sudoku_board, 8, 8) assert possible_values == [9]
def solve_clicked(self): board = boards.empty self.solve_button.config(state='disabled') self.reset_button.config(state='disabled') for row in range(9): for column in range(9): current_box = self.text_boxes[row][column] if str.isdigit(current_box.get()) and len(current_box.get()) == 1: current_box.config(state='disabled') board[row][column] = int(current_box.get()) self.sudoku = Sudoku(board, self) try: sudoku_solver.solve_board(self.sudoku) self.reset_button.config(state='enabled') self.close_button.config(state='enabled') except tk.TclError: pass
def test_get_box_from_cell_with_valid(): sudoku_board = Sudoku(boards.unsolved_easy) box1 = sudoku_board.get_box_from_cell(0, 0) expected_box1 = array([[5, 3, 0], [6, 0, 0], [0, 9, 8]]) assert (box1 == expected_box1).all() box2 = sudoku_board.get_box_from_cell(1, 3) expected_box2 = array([[0, 7, 0], [1, 9, 5], [0, 0, 0]]) assert (box2 == expected_box2).all() box6 = sudoku_board.get_box_from_cell(3, 8) expected_box6 = array([[0, 0, 3], [0, 0, 1], [0, 0, 6]]) assert (box6 == expected_box6).all() box7 = sudoku_board.get_box_from_cell(8, 2) expected_box7 = array([[0, 6, 0], [0, 0, 0], [0, 0, 0]]) assert (box7 == expected_box7).all() box9 = sudoku_board.get_box_from_cell(6, 6) expected_box9 = array([[2, 8, 0], [0, 0, 5], [0, 7, 9]]) assert (box9 == expected_box9).all()
def test_get_box_from_index(): sudoku_board = Sudoku(boards.unsolved_easy) box1 = sudoku_board.get_box_from_index(0) expected_box1 = array([[5, 3, 0], [6, 0, 0], [0, 9, 8]]) assert (box1 == expected_box1).all() box2 = sudoku_board.get_box_from_index(1) expected_box2 = array([[0, 7, 0], [1, 9, 5], [0, 0, 0]]) assert (box2 == expected_box2).all() box3 = sudoku_board.get_box_from_index(2) expected_box3 = array([[0, 0, 0], [0, 0, 0], [0, 6, 0]]) assert (box3 == expected_box3).all() box4 = sudoku_board.get_box_from_index(3) expected_box4 = array([[8, 0, 0], [4, 0, 0], [7, 0, 0]]) assert (box4 == expected_box4).all() box5 = sudoku_board.get_box_from_index(4) expected_box5 = array([[0, 6, 0], [8, 0, 3], [0, 2, 0]]) assert (box5 == expected_box5).all() box6 = sudoku_board.get_box_from_index(5) expected_box6 = array([[0, 0, 3], [0, 0, 1], [0, 0, 6]]) assert (box6 == expected_box6).all() box7 = sudoku_board.get_box_from_index(6) expected_box7 = array([[0, 6, 0], [0, 0, 0], [0, 0, 0]]) assert (box7 == expected_box7).all() box8 = sudoku_board.get_box_from_index(7) expected_box8 = array([[0, 0, 0], [4, 1, 9], [0, 8, 0]]) assert (box8 == expected_box8).all() box9 = sudoku_board.get_box_from_index(8) expected_box9 = array([[2, 8, 0], [0, 0, 5], [0, 7, 9]]) assert (box9 == expected_box9).all()
parser.add_argument( "-f", "--fast", dest="solve_fast", help="Solve quickly without displaying values being added in.", default=False, action="store_true") args = parser.parse_args() if args.use_gui: gui = sudoku_gui.Gui() gui.solve_fast = args.solve_fast gui.window.mainloop() else: print("Welcome to Soduku solver...") correct = "no" sudoku = None while correct.strip() != "yes": print( "Please enter the Soduku board in rows of 9, separating numbers with a space." ) print("Enter 0 for any number that is unknown.") board = read_board_in() sudoku = Sudoku(board) print("You entered the following board...") sudoku.print_board() print("Is that correct? (yes/no)") correct = input() sudoku_solver.solve_board(sudoku)
def test_check_column_for_value(): sudoku = Sudoku(boards.unique_candidate_test) assert sudoku_solver.check_column_for_value(sudoku, 2, 4) assert not sudoku_solver.check_column_for_value(sudoku, 2, 5)
def test_solve_all_single_value_cells_with_unsolved_easy(): sudoku_board = Sudoku(boards.unsolved_easy) sudoku_solver.solve_all_single_value_cells(sudoku_board) solved_board = sudoku_board.board_numbers expected_board = boards.solved_easy assert (array(expected_board) == solved_board).all()
def test_solve_all_naked_subsets(): sudoku_board = Sudoku(boards.naked_pair_test) sudoku_solver.solve_all_naked_subsets(sudoku_board) assert sudoku_board.board_numbers[0, 0] == 0
def test_verify_row_with_valid_rows(): sudoku_board = Sudoku(boards.solved) for i in range(9): assert sudoku_solver.verify_row(sudoku_board, i)
def test_verify_board_with_invalid(): sudoku_board = Sudoku(boards.invalid) assert not sudoku_solver.verify_board(sudoku_board)
def test_verify_board_with_valid(): sudoku_board = Sudoku(boards.solved) assert sudoku_solver.verify_board(sudoku_board)
def test_verify_box_with_invalid_boxes(): sudoku_board = Sudoku(boards.invalid) for i in range(9): assert not sudoku_solver.verify_box(sudoku_board, i)
def test_solve_all_crosshatch_boxes(): sudoku_board = Sudoku(boards.unique_candidate_test) assert sudoku_board.board_numbers[7, 0] == 0 sudoku_solver.solve_all_crosshatch_boxes(sudoku_board) assert sudoku_board.board_numbers[7, 0] == 4
def test_init_with_valid_board(): sudoku_board = Sudoku(boards.solved) board_out = sudoku_board.board_numbers assert (array(boards.solved) == board_out).all()
def test_verify_column_with_invalid_columns(): sudoku_board = Sudoku(boards.invalid) for i in range(9): assert not sudoku_solver.verify_column(sudoku_board, i)
def test_print_board(capsys): sudoku_board = Sudoku(boards.unsolved_easy) sudoku_board.print_board() expected = boards.printed_board out, err = capsys.readouterr() assert out == expected