package ch.epfl.maze.physical.stragegies.reducer; import ch.epfl.maze.physical.Daedalus; import ch.epfl.maze.util.Direction; import java.util.EnumSet; import java.util.Set; /** * A filter keeping only the cheapest choices. * * @author Pacien TRAN-GIRARD */ public interface CostReducer extends ChoiceReducer { /** * Returns the cost of a choice. * * @param choice The Direction choice * @param daedalus The Daedalus * @return The cost of the given choice */ int getChoiceCost(Direction choice, Daedalus daedalus); @Override default Set reduce(Set choices) { return this.reduce(choices, null); } @Override default Set reduce(Set choices, Daedalus daedalus) { Set cheaperChoices = EnumSet.noneOf(Direction.class); int minimalCost = Integer.MAX_VALUE; for (Direction choice : choices) { int choiceCost = this.getChoiceCost(choice, daedalus); if (choiceCost < minimalCost) { cheaperChoices.clear(); minimalCost = choiceCost; } if (choiceCost <= minimalCost) { cheaperChoices.add(choice); } } return cheaperChoices; } }