From 6bbef8efd0748d7ebd71c8e17b90ac7f407e4575 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 3 Feb 2018 20:58:28 +0100 Subject: Fix path finder incorrect behaviour Signed-off-by: pacien --- .../java/fr/umlv/java/wallj/board/PathFinder.java | 4 +-- .../fr/umlv/java/wallj/board/PathFinderTest.java | 39 +++++++++++++--------- src/test/resources/maps/wall.txt | 7 ++++ 3 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 src/test/resources/maps/wall.txt (limited to 'src') 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 { private static List findPath(Node start, T target, BiFunction heuristic) { Map, NodeSearchData> searchData = new HashMap<>(); - TreeSet> discovered = new TreeSet<>(Comparator.comparingDouble(n -> searchData.get(n).estimatedCost)); + Queue> discovered = new PriorityQueue<>(Comparator.comparingDouble(n -> searchData.get(n).estimatedCost)); Set> visited = new HashSet<>(); searchData.put(start, new NodeSearchData<>(null, 0, heuristic.apply(start.val, target))); @@ -65,7 +65,7 @@ public class PathFinder { Node current; while (!discovered.isEmpty()) { - current = discovered.pollFirst(); + current = discovered.poll(); if (target.equals(current.val)) return buildPath(searchData, current); for (Map.Entry, 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; * @author Pacien TRAN-GIRARD */ final class PathFinderTest { - private Path getResourcePath(String str) throws URISyntaxException { return Paths.get(getClass().getResource(str).toURI()); } @@ -35,29 +34,37 @@ final class PathFinderTest { return true; } - @Test - void testFailImpossibleFindPath() throws URISyntaxException, IOException { - Board board = BoardParser.parse(getResourcePath("/maps/island.txt")); + private void testValidPath(Board board, TileVec2 origin, TileVec2 target) { PathFinder pathFinder = new PathFinder(board); + List path = pathFinder.findPath(origin, target); - Assertions.assertThrows(IllegalArgumentException.class, () -> { - pathFinder.findPath(TileVec2.of(7, 3), TileVec2.of(16, 5)); // into a wall - }); + Assertions.assertEquals(path.get(0), origin); + Assertions.assertEquals(path.get(path.size() - 1), target); + Assertions.assertTrue(isPathConnected(path)); + Assertions.assertTrue(path.stream().allMatch(v -> board.getBlockTypeAt(v).isTraversable())); } @Test void testFindPath() throws URISyntaxException, IOException { + testValidPath( + BoardParser.parse(getResourcePath("/maps/wall.txt")), + TileVec2.of(4, 3), + TileVec2.of(6, 3)); + + testValidPath( + BoardParser.parse(getResourcePath("/maps/island.txt")), + TileVec2.of(7, 3), + TileVec2.of(33, 4)); + } + + + @Test + void testFailImpossibleFindPath() throws URISyntaxException, IOException { Board board = BoardParser.parse(getResourcePath("/maps/island.txt")); PathFinder pathFinder = new PathFinder(board); - TileVec2 origin = TileVec2.of(7, 3); - TileVec2 target = TileVec2.of(33, 4); - List path = pathFinder.findPath(origin, target); - - Assertions.assertEquals(path.get(0), origin); - Assertions.assertEquals(path.get(path.size() - 1), target); - Assertions.assertTrue(isPathConnected(path)); - Assertions.assertTrue(path.stream().allMatch(v -> board.getBlockTypeAt(v).isTraversable())); + Assertions.assertThrows(IllegalArgumentException.class, () -> { + pathFinder.findPath(TileVec2.of(7, 3), TileVec2.of(16, 5)); // into a wall + }); } - } 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 @@ +WWWWWWWWWWW +W W +W W W +W W W +W W W +W W +WWWWWWWWWWW \ No newline at end of file -- cgit v1.2.3