blob: d7ccf6e7973cc6a1387bd81998ed0943ca2df67f (
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
package bjc.utils.misc;
import java.util.Random;
import java.util.function.Consumer;
import bjc.utils.exceptions.InvalidDirectionException;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
/**
* Represents a direction that is relative to another direction
*
* @author ben
*
*/
public enum RelativeDirection {
/**
* Same as direction
*/
BACKWARD,
/**
* Opposing direction
*/
FORWARD,
/**
* Counterclockwise from direction
*/
LEFT,
/**
* Clockwise from direction
*/
RIGHT;
private static Random RNG = new Random();
/**
* Perform a specified action for a random number of relative
* directions.
*
* @param numDirections
* The number of cardinal directions to act on. Must be
* greater than 0 and less then 5
* @param action
* The action to perform for each of the relative directions
* @param ignoreBackwards
* Whether or not to not have a chance of one of the
* directions being backwards
*/
public static void forRandomDirections(int numDirections,
Consumer<RelativeDirection> action, boolean ignoreBackwards) {
int maxNDirections;
if (ignoreBackwards) {
maxNDirections = 3;
} else {
maxNDirections = 4;
}
if (numDirections <= 0 || numDirections > maxNDirections) {
throw new IllegalArgumentException(
"Tried to do things with incorrect number of relative directions. Tried with "
+ numDirections);
}
IList<RelativeDirection> relativeDirs =
new FunctionalList<>(values());
if (ignoreBackwards) {
relativeDirs.removeMatching(BACKWARD);
}
for (int i = 0; i <= maxNDirections - numDirections; i++) {
RelativeDirection relativeDir =
relativeDirs.randItem(RNG::nextInt);
relativeDirs.removeMatching(relativeDir);
}
relativeDirs.forEach(action);
}
/**
* Properly convert a string to a relative direction
*
* @param value
* The string to convert
* @return The relative direction represented by the string
*/
public static RelativeDirection properValueOf(String value) {
return valueOf(value.toUpperCase());
}
/**
* Change another direction by turning the way this direction specifies
*
* @param dir
* The direction to change
* @return The direction after turning this way
*/
public Direction makeAbsolute(Direction dir) {
// Only cardinal directions can be truly absolutized
if (dir.isCardinal()) {
switch (this) {
case BACKWARD:
return dir;
case FORWARD:
return dir.opposing();
case LEFT:
return dir.rotateCounterClockwise();
case RIGHT:
return dir.rotateClockwise();
default:
throw new InvalidDirectionException(
"Attempted to make absolute a direction in a unknown way "
+ this);
}
}
// Since it isn't a cardinal direction, absolutize it against a
// random direction
return this.makeAbsolute(Direction.NORTH);
}
}
|