From f11d73b61282ca6a2f2574e7ad3d7d46f9ca1f52 Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 14 Jan 2018 00:51:13 +0100 Subject: Implement path finder Signed-off-by: pacien --- .../fr/umlv/java/wallj/board/PathFinderTest.java | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java (limited to 'src/test/java') diff --git a/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java b/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java new file mode 100644 index 0000000..3af4679 --- /dev/null +++ b/src/test/java/fr/umlv/java/wallj/board/PathFinderTest.java @@ -0,0 +1,60 @@ +package fr.umlv.java.wallj.board; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; +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()); + } + + private boolean isPathConnected(List path) { + for (int i = 1; i < path.size(); ++i) { + TileVec2 predecessor = path.get(i - 1), current = path.get(i); + + if (Math.abs(predecessor.getCol() - current.getCol()) > 1 || + Math.abs(predecessor.getRow() - current.getRow()) > 1) return false; + + if (Math.abs(predecessor.getCol() - current.getCol()) == 1 && + Math.abs(predecessor.getRow() - current.getRow()) == 1) return false; + } + + return true; + } + + @Test + void testFailImpossibleFindPath() throws URISyntaxException, IOException { + Board board = BoardParser.parse(getResourcePath("/maps/island.txt")); + PathFinder pathFinder = new PathFinder(board); + + Assertions.assertThrows(IllegalArgumentException.class, () -> { + pathFinder.findPath(TileVec2.of(7, 3), TileVec2.of(16, 5)); // into a wall + }); + } + + @Test + void testFindPath() 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())); + } + +} -- cgit v1.2.3