summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/stragegies/reducer/CostReducer.java
blob: bfa5191e59c1099cd0c9bad4d261092ca2157801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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<Direction> reduce(Set<Direction> choices) {
        return this.reduce(choices, null);
    }

    @Override
    default Set<Direction> reduce(Set<Direction> choices, Daedalus daedalus) {
        Set<Direction> 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;
    }

}