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()
def crosshatch_box(sudoku: sudoku_board.Sudoku, box_number: int) -> bool: all_values = [1, 2, 3, 4, 5, 6, 7, 8, 9] unused_values = [] box = sudoku.get_box_from_index(box_number) box_values = box.flatten() for value in all_values: if value not in box_values: unused_values.append(value) solved_value = False # Loop through each value that is not in the box. for value in unused_values: possible_rows = [] possible_columns = [] # Check each row that goes through the box. for row in get_rows_from_box_index(box_number): # If the value is not in that row then it could be in that row of the box. if not check_row_for_value(sudoku, row, value): possible_rows.append(row) # Check each column that goes through the box. for column in get_columns_from_box_index(box_number): # If the value is not in that column then it could be in that column of the box. if not check_column_for_value(sudoku, column, value): possible_columns.append(column) # Remove duplicates from possible rows and columns. possible_rows = list(set(possible_rows)) possible_columns = list(set(possible_columns)) # A cell position is only possible if the value is 0. Save the index if it is. possible_index = [] for row in possible_rows: for column in possible_columns: if sudoku.board_numbers[row, column] == 0: possible_index.append([row, column]) # If there is only one possible value for this cell then add it to the board. if len(possible_index) == 1: sudoku.add_cell(possible_index[0][0], possible_index[0][1], value) solved_value = True return solved_value