From 4da9bdc4fa2f44eedba3dff29af7b0ce9180e442 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 23:43:00 +0100 Subject: Refactor Ghosts --- .../physical/stragegies/reducer/CostReducer.java | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java (limited to 'src/ch/epfl/maze/physical/stragegies') diff --git a/src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java b/src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java new file mode 100644 index 0000000..bfa5191 --- /dev/null +++ b/src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java @@ -0,0 +1,51 @@ +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; + } + +} -- cgit v1.2.3