summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java')
-rw-r--r--src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java51
1 files changed, 51 insertions, 0 deletions
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 @@
1package ch.epfl.maze.physical.stragegies.reducer;
2
3import ch.epfl.maze.physical.Daedalus;
4import ch.epfl.maze.util.Direction;
5
6import java.util.EnumSet;
7import java.util.Set;
8
9/**
10 * A filter keeping only the cheapest choices.
11 *
12 * @author Pacien TRAN-GIRARD
13 */
14public interface CostReducer extends ChoiceReducer {
15
16 /**
17 * Returns the cost of a choice.
18 *
19 * @param choice The Direction choice
20 * @param daedalus The Daedalus
21 * @return The cost of the given choice
22 */
23 int getChoiceCost(Direction choice, Daedalus daedalus);
24
25 @Override
26 default Set<Direction> reduce(Set<Direction> choices) {
27 return this.reduce(choices, null);
28 }
29
30 @Override
31 default Set<Direction> reduce(Set<Direction> choices, Daedalus daedalus) {
32 Set<Direction> cheaperChoices = EnumSet.noneOf(Direction.class);
33 int minimalCost = Integer.MAX_VALUE;
34
35 for (Direction choice : choices) {
36 int choiceCost = this.getChoiceCost(choice, daedalus);
37
38 if (choiceCost < minimalCost) {
39 cheaperChoices.clear();
40 minimalCost = choiceCost;
41 }
42
43 if (choiceCost <= minimalCost) {
44 cheaperChoices.add(choice);
45 }
46 }
47
48 return cheaperChoices;
49 }
50
51}