-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirection.java
More file actions
45 lines (40 loc) · 1.22 KB
/
Direction.java
File metadata and controls
45 lines (40 loc) · 1.22 KB
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
package model;
public enum Direction {
UP(0, 1), RIGHT(1, 0), DOWN(0, -1), LEFT(-1, 0);
public int dx, dy;
public static Direction[] directions = {UP, RIGHT, DOWN, LEFT};
private static Direction[] fromUp = {UP, RIGHT, LEFT};
private static Direction[] fromRight = {UP, RIGHT, DOWN};
private static Direction[] fromDown = {RIGHT, DOWN, LEFT};
private static Direction[] fromLeft = {UP, DOWN, LEFT};
Direction(int x, int y) {
dx = x;
dy = y;
}
public static Direction getDirection(String d) {
return Direction.valueOf(d.toUpperCase());
}
public static Direction[] nextDirections(Direction direction) {
if (direction == UP) {
return fromUp;
} else if (direction == RIGHT) {
return fromRight;
} else if (direction == DOWN) {
return fromDown;
} else if (direction == LEFT) {
return fromLeft;
}
return directions;
}
public Direction opposite() {
if (this == UP) {
return DOWN;
} else if (this == RIGHT) {
return LEFT;
} else if (this == DOWN) {
return UP;
} else {
return LEFT;
}
}
}