diff --git a/src/ch/zhaw/ads/LabyrinthServer.java b/src/ch/zhaw/ads/LabyrinthServer.java index a272fe8..87983b8 100644 --- a/src/ch/zhaw/ads/LabyrinthServer.java +++ b/src/ch/zhaw/ads/LabyrinthServer.java @@ -1,5 +1,6 @@ package ch.zhaw.ads; +import java.awt.*; import java.util.Arrays; import java.util.Vector; @@ -8,10 +9,9 @@ public class LabyrinthServer implements CommandExecutor { public Graph createGraph(String s) { Graph graph = new AdjListGraph<>(DijkstraNode.class, Edge.class); - String[] lines = s.split("\n"); - Arrays.asList(lines).forEach(s1 -> { - String[] nodes = s1.split(" "); + Arrays.asList(s.split("\n")).forEach(s1 -> { try { + String[] nodes = s1.split(" "); int x1 = Integer.parseInt(nodes[0].split("-")[0]); int x2 = Integer.parseInt(nodes[1].split("-")[0]); int y1 = Integer.parseInt(nodes[0].split("-")[1]); @@ -24,21 +24,38 @@ public class LabyrinthServer implements CommandExecutor { } }); return graph; - // TODO implement 8.2 } public void drawLabyrinth(Graph graph) { // TODO implement 8.3 + g.setColor(Color.gray); + g.fillRect(0, 0, 9, 9); + g.setColor(Color.WHITE); + graph.getNodes().forEach(node -> node.getEdges().forEach(edge -> g.drawPath(node.getName(), edge.dest.getName(), false))); } private boolean search(DijkstraNode current, DijkstraNode ziel) { - // TODO implement 8.4 + current.setMark(true); + for(Edge edge : current.getEdges()){ + DijkstraNode node = (DijkstraNode) edge.getDest(); + if(node == ziel || (!node.getMark() && search(node, ziel))){ + node.setPrev(current); + return true; + } + } + current.setMark(false); return false; } // search and draw result public void drawRoute(Graph graph, String startNode, String zielNode) { - // TODO implement 8.4 + search(graph.findNode(startNode), graph.findNode(zielNode)); + DijkstraNode currentNode = graph.findNode(zielNode); + g.setColor(Color.red); + while(currentNode.getPrev() != null){ + g.drawPath(currentNode.getName(), currentNode.getPrev().getName(), true); + currentNode = currentNode.getPrev(); + } } public String execute(String s) {