aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-02-03 20:58:28 +0100
committerpacien2018-02-03 20:58:28 +0100
commit6bbef8efd0748d7ebd71c8e17b90ac7f407e4575 (patch)
tree430f50a67de2b913a9d859cf24442982a7a599ba
parente86f5e3c3e197aee8899f643d026dcf4468fbd84 (diff)
downloadwallj-6bbef8efd0748d7ebd71c8e17b90ac7f407e4575.tar.gz
Fix path finder incorrect behaviour
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r--src/main/java/fr/umlv/java/wallj/board/PathFinder.java4
-rw-r--r--src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java39
-rw-r--r--src/test/resources/maps/wall.txt7
3 files changed, 32 insertions, 18 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 c530d83..dfd1fa6 100644
--- a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java
+++ b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java
@@ -57,7 +57,7 @@ public class PathFinder {
57 57
58 private static <T> List<T> findPath(Node<T> start, T target, BiFunction<T, T, Double> heuristic) { 58 private static <T> List<T> findPath(Node<T> start, T target, BiFunction<T, T, Double> heuristic) {
59 Map<Node<T>, NodeSearchData<T>> searchData = new HashMap<>(); 59 Map<Node<T>, NodeSearchData<T>> searchData = new HashMap<>();
60 TreeSet<Node<T>> discovered = new TreeSet<>(Comparator.comparingDouble(n -> searchData.get(n).estimatedCost)); 60 Queue<Node<T>> discovered = new PriorityQueue<>(Comparator.comparingDouble(n -> searchData.get(n).estimatedCost));
61 Set<Node<T>> visited = new HashSet<>(); 61 Set<Node<T>> visited = new HashSet<>();
62 62
63 searchData.put(start, new NodeSearchData<>(null, 0, heuristic.apply(start.val, target))); 63 searchData.put(start, new NodeSearchData<>(null, 0, heuristic.apply(start.val, target)));
@@ -65,7 +65,7 @@ public class PathFinder {
65 65
66 Node<T> current; 66 Node<T> current;
67 while (!discovered.isEmpty()) { 67 while (!discovered.isEmpty()) {
68 current = discovered.pollFirst(); 68 current = discovered.poll();
69 if (target.equals(current.val)) return buildPath(searchData, current); 69 if (target.equals(current.val)) return buildPath(searchData, current);
70 70
71 for (Map.Entry<Node<T>, Integer> neighborEntry : current.neighbors.entrySet()) { 71 for (Map.Entry<Node<T>, Integer> neighborEntry : current.neighbors.entrySet()) {
diff --git a/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java b/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java
index be615b7..ddacf71 100644
--- a/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java
+++ b/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java
@@ -14,7 +14,6 @@ import java.util.List;
14 * @author Pacien TRAN-GIRARD 14 * @author Pacien TRAN-GIRARD
15 */ 15 */
16final class PathFinderTest { 16final class PathFinderTest {
17
18 private Path getResourcePath(String str) throws URISyntaxException { 17 private Path getResourcePath(String str) throws URISyntaxException {
19 return Paths.get(getClass().getResource(str).toURI()); 18 return Paths.get(getClass().getResource(str).toURI());
20 } 19 }
@@ -35,29 +34,37 @@ final class PathFinderTest {
35 return true; 34 return true;
36 } 35 }
37 36
38 @Test 37 private void testValidPath(Board board, TileVec2 origin, TileVec2 target) {
39 void testFailImpossibleFindPath() throws URISyntaxException, IOException {
40 Board board = BoardParser.parse(getResourcePath("/maps/island.txt"));
41 PathFinder pathFinder = new PathFinder(board); 38 PathFinder pathFinder = new PathFinder(board);
39 List<TileVec2> path = pathFinder.findPath(origin, target);
42 40
43 Assertions.assertThrows(IllegalArgumentException.class, () -> { 41 Assertions.assertEquals(path.get(0), origin);
44 pathFinder.findPath(TileVec2.of(7, 3), TileVec2.of(16, 5)); // into a wall 42 Assertions.assertEquals(path.get(path.size() - 1), target);
45 }); 43 Assertions.assertTrue(isPathConnected(path));
44 Assertions.assertTrue(path.stream().allMatch(v -> board.getBlockTypeAt(v).isTraversable()));
46 } 45 }
47 46
48 @Test 47 @Test
49 void testFindPath() throws URISyntaxException, IOException { 48 void testFindPath() throws URISyntaxException, IOException {
49 testValidPath(
50 BoardParser.parse(getResourcePath("/maps/wall.txt")),
51 TileVec2.of(4, 3),
52 TileVec2.of(6, 3));
53
54 testValidPath(
55 BoardParser.parse(getResourcePath("/maps/island.txt")),
56 TileVec2.of(7, 3),
57 TileVec2.of(33, 4));
58 }
59
60
61 @Test
62 void testFailImpossibleFindPath() throws URISyntaxException, IOException {
50 Board board = BoardParser.parse(getResourcePath("/maps/island.txt")); 63 Board board = BoardParser.parse(getResourcePath("/maps/island.txt"));
51 PathFinder pathFinder = new PathFinder(board); 64 PathFinder pathFinder = new PathFinder(board);
52 65
53 TileVec2 origin = TileVec2.of(7, 3); 66 Assertions.assertThrows(IllegalArgumentException.class, () -> {
54 TileVec2 target = TileVec2.of(33, 4); 67 pathFinder.findPath(TileVec2.of(7, 3), TileVec2.of(16, 5)); // into a wall
55 List<TileVec2> path = pathFinder.findPath(origin, target); 68 });
56
57 Assertions.assertEquals(path.get(0), origin);
58 Assertions.assertEquals(path.get(path.size() - 1), target);
59 Assertions.assertTrue(isPathConnected(path));
60 Assertions.assertTrue(path.stream().allMatch(v -> board.getBlockTypeAt(v).isTraversable()));
61 } 69 }
62
63} 70}
diff --git a/src/test/resources/maps/wall.txt b/src/test/resources/maps/wall.txt
new file mode 100644
index 0000000..97a5874
--- /dev/null
+++ b/src/test/resources/maps/wall.txt
@@ -0,0 +1,7 @@
1WWWWWWWWWWW
2W W
3W W W
4W W W
5W W W
6W W
7WWWWWWWWWWW \ No newline at end of file