diff options
author | Pacien TRAN-GIRARD | 2015-11-24 23:43:00 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-11-24 23:43:00 +0100 |
commit | 4da9bdc4fa2f44eedba3dff29af7b0ce9180e442 (patch) | |
tree | a7bb129adc714d4c06e2fdeb0008343aa816bd97 /src/ch/epfl/maze/physical/stragegies/reducer | |
parent | 5c943f7c7b53d850d7ad3413183fef7f001c6cc4 (diff) | |
download | maze-solver-4da9bdc4fa2f44eedba3dff29af7b0ce9180e442.tar.gz |
Refactor Ghosts
Diffstat (limited to 'src/ch/epfl/maze/physical/stragegies/reducer')
-rw-r--r-- | src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java | 51 |
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 @@ | |||
1 | package ch.epfl.maze.physical.stragegies.reducer; | ||
2 | |||
3 | import ch.epfl.maze.physical.Daedalus; | ||
4 | import ch.epfl.maze.util.Direction; | ||
5 | |||
6 | import java.util.EnumSet; | ||
7 | import java.util.Set; | ||
8 | |||
9 | /** | ||
10 | * A filter keeping only the cheapest choices. | ||
11 | * | ||
12 | * @author Pacien TRAN-GIRARD | ||
13 | */ | ||
14 | public 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 | } | ||