summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/util/Trail.java
blob: 33e097515db4466ed19e97fb5c3ff1c991974313 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package ch.epfl.maze.util;

import java.util.HashMap;
import java.util.Map;

/**
 * A trail keeping track on positional markings.
 *
 * @author Pacien TRAN-GIRARD
 */
public class Trail {

    public enum Marking {
        NO_MARKING,
        AVOID_MARKING,
        NO_GO_MARKING;

        public static final Marking DEFAULT = NO_MARKING;
        public static final Marking[] ALL = new Marking[]{NO_MARKING, AVOID_MARKING, NO_GO_MARKING};

        /**
         * Returns the next Marking.
         *
         * @return The next Marking
         */
        public Marking getNext() {
            switch (this) {
                case NO_MARKING:
                    return AVOID_MARKING;
                case AVOID_MARKING:
                    return NO_GO_MARKING;
                case NO_GO_MARKING:
                    return NO_GO_MARKING;
                default:
                    return AVOID_MARKING;
            }
        }
    }

    private final Map<Vector2D, Marking> trail;

    /**
     * Constructs a new blank Trail.
     */
    public Trail() {
        this.trail = new HashMap<>();
    }

    /**
     * Get the marking at the given position.
     *
     * @param position The positional Vector
     * @return The Marking
     */
    public Marking getMarking(Vector2D position) {
        Marking marking = this.trail.get(position);
        return marking != null ? marking : Marking.NO_MARKING;
    }

    /**
     * Marks the given position with the given Marking.
     *
     * @param position The positional vector
     * @param marking  The Marking
     */
    public void markPosition(Vector2D position, Marking marking) {
        this.trail.put(position, marking);
    }

    /**
     * Marks the given position with the following Marking.
     *
     * @param position The positional vector
     */
    public void markPosition(Vector2D position) {
        this.markPosition(position, this.getMarking(position).getNext());
    }

}