From 785b359d37d2ccfb844e193667a48f2296bf9c4e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 8 Nov 2022 22:18:33 +0100 Subject: [PATCH] Add code for determining and drawing a route --- .../ch/nuth/zhaw/exbox/LabyrinthServer.java | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/LabyrinthServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/LabyrinthServer.java index a717b3a..bf92073 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/LabyrinthServer.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/LabyrinthServer.java @@ -51,14 +51,43 @@ public class LabyrinthServer implements CommandExecutor { } } - private boolean search(DijkstraNode current, DijkstraNode ziel) { - // TODO implement 8.4 - return false; + private boolean search(DijkstraNode current, DijkstraNode destination) { + current.setMark(true); + + if (current == destination) { + return true; + } else { + for (Edge edge : current.getEdges()) { + if (edge.getDest() instanceof DijkstraNode node && + !node.getMark()) { + node.setPrev(current); + + if (search(node, destination)) { + return true; + } else { + node.setPrev(null); + } + } + } + + return false; + } } // search and draw result - public void drawRoute(Graph graph, String startNode, String zielNode) { + public void drawRoute(Graph graph, String startNode, String destinationNode) { // TODO implement 8.4 + ServerGraphics graphics = getGraphics(); + DijkstraNode currentNode = graph.findNode(destinationNode); + graphics.setColor(Color.RED); + + if (search(graph.findNode(startNode), currentNode)) { + while (currentNode.getPrev() != null) { + DijkstraNode previousNode = currentNode.getPrev(); + graphics.drawPath(previousNode.getName(), currentNode.getName(), true); + currentNode = previousNode; + } + } } public String execute(String s) {