-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirection.java
More file actions
54 lines (47 loc) · 1.14 KB
/
Direction.java
File metadata and controls
54 lines (47 loc) · 1.14 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
46
47
48
49
50
51
52
53
54
import java.util.Random;
/**
* An enum to handle directions.
*
* @author Thomas Vakili
* @version 2013.12.12
*/
public enum Direction {
NORTH("north", "n"),
SOUTH("south", "s"),
EAST("east", "e"),
WEST("west", "w"),
UP("up", "u"),
DOWN("down", "d");
private Direction opposite;
private String longDirection;
private String shortDirection;
static {
NORTH.opposite = SOUTH;
SOUTH.opposite = NORTH;
EAST.opposite = WEST;
WEST.opposite = EAST;
UP.opposite = DOWN;
DOWN.opposite = UP;
}
private Direction (final String longDirection, final String shortDirection) {
this.longDirection = longDirection;
this.shortDirection = shortDirection;
}
@Override
public String toString() {
return longDirection;
}
/**
* Return the opposite direction.
*/
public Direction getOpposite() {
return opposite;
}
/**
* Get a random direction.
*/
public static Direction getRandomDirection() {
Random random = new Random();
return values()[random.nextInt(values().length)];
}
}