-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirection.java
More file actions
78 lines (66 loc) · 1.8 KB
/
Direction.java
File metadata and controls
78 lines (66 loc) · 1.8 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright 2018 David Prentiss
*/
package sim.app.agentcity;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toMap;
import java.util.Map;
enum Direction {
NONE(0, 0, 0),
NORTH(1, 0, -1),
NORTH_EAST(2, 1, -1),
EAST(3, 1, 0),
SOUTH_EAST(4, 1, 1),
SOUTH(5, 0, 1),
SOUTH_WEST(6, -1, 1),
WEST(7, -1, 0),
NORTH_WEST(8, -1, -1),
ALL(9, 0, 0);
private final int dirNum;
private final int xOffset;
private final int yOffset;
private final static Map<Integer, Direction> map =
stream(Direction.values()).collect(toMap(dir -> dir.dirNum, dir -> dir));
private Direction(final int dir, final int x, final int y) {
this.dirNum = dir;
this.xOffset = x;
this.yOffset = y;
}
public int toInt() { return dirNum; }
public int getXOffset() { return xOffset; }
public int getYOffset() { return yOffset; }
public static Direction byInt(int dirNum) {
return map.get(dirNum);
}
public Direction byDirective(Driver.Directive directive) {
switch (directive) {
case TURN_LEFT:
return this.onLeft();
case TURN_RIGHT:
return this.onRight();
default:
return this;
}
}
public Direction onRight() {
if (dirNum > 0 && dirNum < 9) {
return this.byInt((dirNum + 2) % 8);
} else {
return this;
}
}
public Direction onLeft() {
if (dirNum > 0 && dirNum < 9) {
return this.byInt((dirNum + 6) % 8);
} else {
return this;
}
}
public Direction opposite() {
if (dirNum > 0 && dirNum < 9) {
return this.byInt((dirNum + 4) % 8);
} else {
return this;
}
}
}