summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/physical/stragegies/picker/CyclePicker.java
blob: efa3794e26ec284295a9734c424cb709574ed2b0 (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
52
53
54
55
56
57
58
59
60
61
62
package ch.epfl.maze.physical.stragegies.picker;

import ch.epfl.maze.util.Direction;

import java.util.Set;

/**
 * A decision maker cycling in each Direction starting from a given one.
 *
 * @author Pacien TRAN-GIRARD
 */
public interface CyclePicker extends BlindPicker {

    /**
     * Returns the cycle starting Direction.
     *
     * @return The starting Direction
     */
    Direction getStartingDirection();

    /**
     * Returns the Rotation Direction.
     *
     * @return The Rotation Direction
     */
    Direction getRotationDirection();

    @Override
    default Direction pick(Set<Direction> choices) {
        if (choices.isEmpty()) return FALLBACK_DIRECTION;
        if (this.getStartingDirection() == Direction.NONE) return FALLBACK_DIRECTION;
        if (this.getRotationDirection() == Direction.NONE) return FALLBACK_DIRECTION;

        Direction dir = this.getStartingDirection();
        while (!choices.contains(dir))
            dir = dir.unRelativeDirection(this.getRotationDirection());

        return dir;
    }

    /**
     * Counts the number of rotations to rotates to the given Direction
     *
     * @param direction The Direction
     * @return The number of rotations
     */
    default int countRotations(Direction direction) {
        if (direction == Direction.NONE) return 0;
        if (this.getStartingDirection() == Direction.NONE) return 0;
        if (this.getRotationDirection() == Direction.NONE) return 0;

        Direction dir = this.getStartingDirection();
        int counter = 0;
        while (dir != direction) {
            dir = dir.unRelativeDirection(this.getRotationDirection());
            counter += 1;
        }

        return counter;
    }

}