def do_next_step(self, puzzle_state: PuzzleState): changes = False for column in range(puzzle_state.puzzle.columns): if 0 < puzzle_state.currently_missing_ships_in_columns[column] \ == FillUpShips.count_slots_in_lines(puzzle_state.currently_free_lines_in_columns[column]): if not self.are_correct_ships_missing( puzzle_state, puzzle_state.currently_free_lines_in_columns[column]): continue puzzle_state.place_ships( puzzle_state.currently_free_lines_in_columns[column]) changes = True # if ships in columns were set, update the empty lines and counts # ToDo: when specific ship parts start to count, it will be important to go from longest ship to smallest if changes: puzzle_state.update() for row in range(puzzle_state.puzzle.rows): if 0 < puzzle_state.currently_missing_ships_in_rows[row] == \ FillUpShips.count_slots_in_lines(puzzle_state.currently_free_lines_in_rows[row]): if not self.are_correct_ships_missing( puzzle_state, puzzle_state.currently_free_lines_in_rows[row]): continue puzzle_state.place_ships( puzzle_state.currently_free_lines_in_rows[row])
def do_next_step(self, grid_state: PuzzleState): max_ship_length = max(grid_state.missing_ships.keys()) max_length_free_line = max(grid_state.free_lines.keys()) if max_length_free_line == max_ship_length: ship_count = grid_state.missing_ships[max_ship_length] free_lines_count = sum([ len(free_lines[max_length_free_line] if max_length_free_line in free_lines else ()) for free_lines in self.relevant_free_lines_in_columns.values() ]) free_lines_count += sum([ len(free_lines[max_length_free_line] if max_length_free_line in free_lines else ()) for free_lines in self.relevant_free_lines_in_rows.values() ]) if free_lines_count > ship_count: pass elif free_lines_count == ship_count: for free_lines in self.relevant_free_lines_in_columns.values(): if max_length_free_line in free_lines: grid_state.place_ships({ max_ship_length: free_lines[max_length_free_line] }) for free_lines in self.relevant_free_lines_in_rows.values(): if max_length_free_line in free_lines: grid_state.place_ships({ max_ship_length: free_lines[max_length_free_line] }) else: raise ImpossiblePuzzleException( 'Not enough free lines for left ships!')