-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDirectionGenerator.java
More file actions
72 lines (57 loc) · 2.15 KB
/
DirectionGenerator.java
File metadata and controls
72 lines (57 loc) · 2.15 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
package domain.direction;
import domain.ladder.Location;
import domain.ladder.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class DirectionGenerator {
private static final int ANY = 0;
private static final int DOWN = Direction.DOWN.getCode();
private static final int RIGHT = Direction.RIGHT.getCode();
private static final Map<Integer, Direction> RIGHT_AND_DOWN;
static {
RIGHT_AND_DOWN = getInstance();
}
private static Map<Integer, Direction> getInstance() {
return IntStream.rangeClosed(RIGHT, DOWN)
.mapToObj(code -> Direction.findByCode(code))
.collect(Collectors.toMap(Direction::getCode, Function.identity()));
}
public static List<Point> createFirst(int height) {
return IntStream.range(0, height)
.mapToObj(y -> new Location(0, y))
.map(location -> new Point(location, createEach()))
.collect(Collectors.toList());
}
private static Direction createEach() {
List<Integer> codes = new ArrayList<>(RIGHT_AND_DOWN.keySet());
Collections.shuffle(codes);
return Direction.findByCode(codes.get(ANY));
}
public static List<Point> createMiddle(List<Point> before) {
return before.stream()
.map(point -> Point.createNextPoint(point, createMidDirection(point)))
.collect(Collectors.toList());
}
public static Direction createMidDirection(Point point) {
if (point.isRight()) {
return Direction.LEFT;
}
return createEach();
}
public static List<Point> createLast(List<Point> points) {
return points.stream()
.map(point -> Point.createNextPoint(point, createLastDirection(point)))
.collect(Collectors.toList());
}
public static Direction createLastDirection(Point point) {
if (point.isRight()) {
return Direction.LEFT;
}
return Direction.DOWN;
}
}