aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/board/PathFinder.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/fr/umlv/java/wallj/board/PathFinder.java')
-rw-r--r--src/main/java/fr/umlv/java/wallj/board/PathFinder.java61
1 files changed, 30 insertions, 31 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java
index 098b4a2..6bf12e9 100644
--- a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java
+++ b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java
@@ -11,26 +11,16 @@ import java.util.stream.Collectors;
11 */ 11 */
12public class PathFinder { 12public class PathFinder {
13 private static final int LEAP_COST = 1; 13 private static final int LEAP_COST = 1;
14 private final Map<TileVec2, Node<TileVec2>> graph;
14 15
15 private static class Node<T> { 16 /**
16 final T val; 17 * Builds a new path finder for the supplied board.
17 final Map<Node<T>, Integer> neighbors; 18 * A well-build (validated) board should be fully connected.
18 19 *
19 Node(T val) { 20 * @param board the board
20 this.val = val; 21 */
21 this.neighbors = new HashMap<>(); 22 public PathFinder(Board board) {
22 } 23 graph = buildGraph(Objects.requireNonNull(board));
23 }
24
25 private static class NodeSearchData<T> {
26 final Node<T> predecessor;
27 final double actualCost, estimatedCost;
28
29 NodeSearchData(Node<T> predecessor, double actualCost, double estimatedCost) {
30 this.predecessor = predecessor;
31 this.actualCost = actualCost;
32 this.estimatedCost = estimatedCost;
33 }
34 } 24 }
35 25
36 private static double euclideanDistance(TileVec2 a, TileVec2 b) { 26 private static double euclideanDistance(TileVec2 a, TileVec2 b) {
@@ -101,18 +91,6 @@ public class PathFinder {
101 return map; 91 return map;
102 } 92 }
103 93
104 private final Map<TileVec2, Node<TileVec2>> graph;
105
106 /**
107 * Builds a new path finder for the supplied board.
108 * A well-build (validated) board should be fully connected.
109 *
110 * @param board the board
111 */
112 public PathFinder(Board board) {
113 graph = buildGraph(Objects.requireNonNull(board));
114 }
115
116 /** 94 /**
117 * Returns a path from a starting point to a target if it exists, 95 * Returns a path from a starting point to a target if it exists,
118 * or throw an IllegalArgumentException if any of the given coordinates are invalid. 96 * or throw an IllegalArgumentException if any of the given coordinates are invalid.
@@ -128,4 +106,25 @@ public class PathFinder {
128 if (startNode == null) throw new IllegalArgumentException("Invalid starting point."); 106 if (startNode == null) throw new IllegalArgumentException("Invalid starting point.");
129 return findPath(startNode, target, PathFinder::euclideanDistance); 107 return findPath(startNode, target, PathFinder::euclideanDistance);
130 } 108 }
109
110 private static class Node<T> {
111 final T val;
112 final Map<Node<T>, Integer> neighbors;
113
114 Node(T val) {
115 this.val = val;
116 this.neighbors = new HashMap<>();
117 }
118 }
119
120 private static class NodeSearchData<T> {
121 final Node<T> predecessor;
122 final double actualCost, estimatedCost;
123
124 NodeSearchData(Node<T> predecessor, double actualCost, double estimatedCost) {
125 this.predecessor = predecessor;
126 this.actualCost = actualCost;
127 this.estimatedCost = estimatedCost;
128 }
129 }
131} 130}