summaryrefslogtreecommitdiff
path: root/src/ch/epfl/maze/util/Direction.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ch/epfl/maze/util/Direction.java')
-rw-r--r--src/ch/epfl/maze/util/Direction.java235
1 files changed, 235 insertions, 0 deletions
diff --git a/src/ch/epfl/maze/util/Direction.java b/src/ch/epfl/maze/util/Direction.java
new file mode 100644
index 0000000..a75ba7f
--- /dev/null
+++ b/src/ch/epfl/maze/util/Direction.java
@@ -0,0 +1,235 @@
1package ch.epfl.maze.util;
2
3/**
4 * Directions that an animal can take to move. They represent the four cardinal
5 * points ({@code DOWN, UP, RIGHT, LEFT}) from the frame of reference of the
6 * labyrinth, plus a default one : {@code NONE}.
7 *
8 */
9
10public enum Direction {
11 DOWN, UP, RIGHT, LEFT, NONE;
12
13 /**
14 * Returns the integer value of the direction
15 *
16 * @return Integer value of the direction
17 */
18
19 public int intValue() {
20 switch (this) {
21 case DOWN:
22 return 0;
23
24 case UP:
25 return 1;
26
27 case RIGHT:
28 return 2;
29
30 case LEFT:
31 return 3;
32
33 case NONE:
34 default:
35 return 4;
36 }
37 }
38
39 /**
40 * Converts the direction into an orthonormal vector, when possible.
41 *
42 * @return Orthonormal {@code Vector2D} that represents the direction.
43 */
44
45 public Vector2D toVector() {
46 switch (this) {
47 case DOWN:
48 return new Vector2D(0, 1);
49
50 case UP:
51 return new Vector2D(0, -1);
52
53 case RIGHT:
54 return new Vector2D(1, 0);
55
56 case LEFT:
57 return new Vector2D(-1, 0);
58
59 case NONE:
60 default:
61 return new Vector2D(0, 0);
62 }
63 }
64
65 /**
66 * Reverses the direction.
67 *
68 * @return The opposite direction.
69 */
70
71 public Direction reverse() {
72 switch (this) {
73 case DOWN:
74 return UP;
75
76 case UP:
77 return DOWN;
78
79 case RIGHT:
80 return LEFT;
81
82 case LEFT:
83 return RIGHT;
84
85 case NONE:
86 default:
87 return NONE;
88 }
89 }
90
91 /**
92 * Determines whether the argument is the opposite of another.
93 *
94 * @param d
95 * The direction to compare with
96 * @return <b>true</b> if the direction is the opposite the argument,
97 * <b>false</b> otherwise
98 */
99
100 public boolean isOpposite(Direction d) {
101 return this == d.reverse();
102 }
103
104 /**
105 * Converts the argument relative to the frame of reference given by the
106 * direction that calls the method.
107 *
108 * @param dir
109 * The direction to convert
110 * @return The direction converted to the frame of reference given by the
111 * direction called.
112 */
113
114 public Direction relativeDirection(Direction dir) {
115 switch (this) {
116 case DOWN:
117 return dir.reverse();
118
119 case UP:
120 return dir;
121
122 case RIGHT:
123 return dir.rotateLeft();
124
125 case LEFT:
126 return dir.rotateRight();
127
128 case NONE:
129 default:
130 return NONE;
131 }
132 }
133
134 /**
135 * Converts the argument back to the frame of reference of the labyrinth
136 *
137 * @param dir
138 * The direction to convert back
139 * @return The direction converted back to the frame of reference of the
140 * labyrinth
141 */
142
143 public Direction unRelativeDirection(Direction dir) {
144 switch (this) {
145 case DOWN:
146 return dir.reverse();
147
148 case UP:
149 return dir;
150
151 case RIGHT:
152 return dir.rotateRight();
153
154 case LEFT:
155 return dir.rotateLeft();
156
157 case NONE:
158 default:
159 return NONE;
160 }
161 }
162
163 /**
164 * Rotates the direction to the right.
165 *
166 * @return The rotated direction to the right
167 */
168
169 public Direction rotateRight() {
170 switch (this) {
171 case DOWN:
172 return LEFT;
173
174 case UP:
175 return RIGHT;
176
177 case RIGHT:
178 return DOWN;
179
180 case LEFT:
181 return UP;
182
183 case NONE:
184 default:
185 return NONE;
186 }
187 }
188
189 /**
190 * Rotates the direction to the left.
191 *
192 * @return The rotated direction to the left
193 */
194
195 public Direction rotateLeft() {
196 switch (this) {
197 case DOWN:
198 return RIGHT;
199
200 case UP:
201 return LEFT;
202
203 case RIGHT:
204 return UP;
205
206 case LEFT:
207 return DOWN;
208
209 case NONE:
210 default:
211 return NONE;
212 }
213 }
214
215 /**
216 * Applies the change of coordinates relative to the frame of reference
217 * of the direction that calls the method to all the directions in the
218 * argument.
219 *
220 * @param dir
221 * The array of directions to convert
222 * @return The directions converted to the frame of reference given by the
223 * direction which calls the method
224 */
225
226 public Direction[] relativeDirections(Direction[] dir) {
227 Direction[] relativeDirections = new Direction[dir.length];
228
229 for (int i = 0; i < dir.length; i++) {
230 relativeDirections[i] = this.relativeDirection(dir[i]);
231 }
232
233 return relativeDirections;
234 }