def test_get_set_cell_state(supply_board_from_file): board = supply_board_from_file[BOARD] rows, columns = board.get_size() location_to_change = Location(0, 0) for row in range(rows): for column in range(columns): assert board.get_cell_state(Location(row, column)) == CellState.UNSET board.set_cell_state(location_to_change, CellState.FILL) for row in range(rows): for column in range(columns): location = Location(row, column) if location == location_to_change: assert board.get_cell_state(location) == CellState.FILL else: assert board.get_cell_state(location) == CellState.UNSET board.set_cell_state(location_to_change, CellState.NO_FILL) for row in range(rows): for column in range(columns): location = Location(row, column) if location == location_to_change: assert board.get_cell_state(location) == CellState.NO_FILL else: assert board.get_cell_state(location) == CellState.UNSET board.set_cell_state(location_to_change, CellState.UNSET) for row in range(rows): for column in range(columns): assert board.get_cell_state(Location(row, column)) == CellState.UNSET
def solve_(self, guess: Guess = Guess(Location(-1, -1), CellState.FILL)): added_info = self.add_info() if self.board.is_board_solved(): return True if self.guess_(self.next_guess_location(guess)): return True if guess.location != Location(-1, -1): for info in added_info: info.unset_cell() return False
def get_next_guess(self, last_guess: Guess): next_location = last_guess.location if last_guess.location == Location(-1, -1): next_location = Location(0, 0) while next_location == last_guess.location or self.board.get_cell_state( next_location) != CellState.UNSET: if next_location.column == self.columns - 1: if next_location.row == self.rows - 1: return None next_location = Location(next_location.row + 1, 0) else: next_location = Location(next_location.row, next_location.column + 1) return Guess(next_location, CellState.FILL)
def test_is_row_solved(supply_board_from_file): board = supply_board_from_file[BOARD] _, columns = board.get_size() assert not board.is_row_solved(0) for index in range(columns): board.set_cell_state(Location(0, index), CellState.NO_FILL) assert not board.is_row_solved(0) for index in [0, 1, 4, 6, 8]: # List of indexes where solution is filled. board.set_cell_state(Location(0, index), CellState.FILL) assert not board.is_row_solved(0) board.set_cell_state(Location(0, 9), CellState.FILL) assert board.is_row_solved(0) board.set_cell_state(Location(0, 0), CellState.UNSET) assert not board.is_row_solved(0) board.set_cell_state(Location(0, 0), CellState.NO_FILL) assert not board.is_row_solved(0)
def test_assert_location(supply_board_from_file): board = supply_board_from_file[BOARD] rows, columns = board.get_size() bad_location1 = Location(rows, columns) bad_location2 = Location(-1, -1) good_location = Location(0, 0) with pytest.raises(AssertionError): board.get_cell_state(bad_location1) with pytest.raises(AssertionError): board.set_cell_state(bad_location1, CellState.FILL) with pytest.raises(AssertionError): board.get_cell_state(bad_location2) with pytest.raises(AssertionError): board.set_cell_state(bad_location2, CellState.FILL) try: board.get_cell_state(good_location) board.set_cell_state(good_location, CellState.FILL) except AssertionError: raise pytest.fail('Good location was wrongly found out of bound.')
def get_next_guess(self, last_guess: Guess): best_guess = None best_info_added_count = -1 for row_num, row in enumerate(self.board.game_table.table): for col_num, cell in enumerate(row): if cell.get_state() == CellState.UNSET: for state in [CellState.FILL, CellState.NO_FILL]: cell.set_state(state) info_added = self.info_adder.add_info() if len(info_added) > best_info_added_count: best_guess = Guess(Location(row_num, col_num), state) best_info_added_count = len(info_added) for cell in info_added: cell.set_state(CellState.UNSET) cell.set_state(CellState.UNSET) return best_guess
def solve(self): self.start_count_time_() self.solve_(Guess(Location(0, 0), CellState.FILL)) self.stop_count_time_() self.print_score_()
def test_get_row_column(supply_board_from_file): board = supply_board_from_file[BOARD] rows, columns = board.get_size() assert cell_row_to_states(board.get_row(0)) == [ CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET ] board.set_cell_state(Location(0, columns - 1), CellState.FILL) assert cell_row_to_states(board.get_row(0)) == [ CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.FILL ] board.set_cell_state(Location(rows - 1, 0), CellState.NO_FILL) assert cell_row_to_states(board.get_column(0)) == [ CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.NO_FILL ] board.set_cell_state(Location(0, 0), CellState.FILL) assert cell_row_to_states(board.get_row(0)) == [ CellState.FILL, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.FILL ] assert cell_row_to_states(board.get_column(0)) == [ CellState.FILL, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.UNSET, CellState.NO_FILL ]
def get_cell(state: CellState): return Cell(Location(0, 0), state)