-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathMovement.java
More file actions
75 lines (63 loc) · 1.84 KB
/
Movement.java
File metadata and controls
75 lines (63 loc) · 1.84 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
package chess;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public enum Movement {
UP(0, 1),
UP_UP(UP.x * 2, UP.y * 2),
DOWN(0, -1),
DOWN_DOWN(DOWN.x * 2, DOWN.y * 2),
LEFT(-1, 0),
RIGHT(1, 0),
LEFT_UP(LEFT.x, UP.y),
RIGHT_UP(RIGHT.x, UP.y),
LEFT_DOWN(LEFT.x, DOWN.y),
RIGHT_DOWN(RIGHT.x, DOWN.y),
UP_UP_LEFT(LEFT_DOWN.x, UP_UP.y),
UP_UP_RIGHT(RIGHT_DOWN.x, UP_UP.y),
LEFT_LEFT_UP(LEFT.x * 2, UP.y),
LEFT_LEFT_DOWN(LEFT.x * 2, DOWN.y),
RIGHT_RIGHT_UP(RIGHT.x * 2, UP.y),
RIGHT_RIGHT_DOWN(RIGHT.x * 2, DOWN.y),
DOWN_DOWN_LEFT(LEFT_DOWN.x, DOWN_DOWN.y),
DOWN_DOWN_RIGHT(RIGHT_DOWN.x, DOWN_DOWN.y),
;
private final int x;
private final int y;
Movement(final int x, final int y) {
this.x = x;
this.y = y;
}
public static Map<Movement, Integer> calculate(final int rowDiff, final int columnDiff) {
int row = rowDiff;
int column = columnDiff;
System.out.println(row + ", " + column);
Map<Movement, Integer> movements = new HashMap<>();
while (!(row == 0 && column == 0)) {
for (Movement value : values()) {
if (value.y == row && value.x == column) {
movements.put(value, movements.getOrDefault(value, 0) + 1);
System.out.println(value.name());
row -= row;
column -= column;
}
}
}
return movements;
}
public int x() {
return x;
}
public int y() {
return y;
}
public boolean isVertical() {
return x == 0 && y != 0;
}
public boolean isHorizontal() {
return x != 0 && y == 0;
}
public boolean isDiagonal() {
return x != 0 && y != 0 && Math.abs(x) == Math.abs(y);
}
}