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 run(self, grid_state: PuzzleState): for step in self.steps: if grid_state.puzzle.is_solved(): return step.prepare(grid_state) if self.debug: time_start = time.time() check_for_next_step = step.check_for_next_step(grid_state) print("Running step " + str(step.__class__.__name__) + ": Changes? " + str(check_for_next_step)) if check_for_next_step: step.do_next_step(grid_state) grid_state.update() grid_state.display() print("Took " + str(time.time() - time_start) + " seconds") elif step.check_for_next_step(grid_state): step.do_next_step(grid_state)