From c2c2f058d423de3681ed14d5c197de7e9cc97507 Mon Sep 17 00:00:00 2001 From: "navid.sassan" Date: Mon, 16 Nov 2020 17:01:48 +0100 Subject: [PATCH] solved s08 ex4 --- .../main/java/ch/zhaw/ads/LabyrinthNode.java | 20 ++++++- .../java/ch/zhaw/ads/LabyrinthServer.java | 53 ++++++++++++++++++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/08/src/main/java/ch/zhaw/ads/LabyrinthNode.java b/08/src/main/java/ch/zhaw/ads/LabyrinthNode.java index 5308c69..54ef043 100644 --- a/08/src/main/java/ch/zhaw/ads/LabyrinthNode.java +++ b/08/src/main/java/ch/zhaw/ads/LabyrinthNode.java @@ -1,6 +1,22 @@ package ch.zhaw.ads; -import java.util.*; +class LabyrinthNode extends Node { + boolean mark; + LabyrinthNode prev; -public class LabyrinthNode extends Node { + public void setMark(boolean m) { + mark = m; + } + + public boolean getMark() { + return mark; + } + + public void setPrev(LabyrinthNode prev) { + this.prev = prev; + } + + public LabyrinthNode getPrev() { + return prev; + } } diff --git a/08/src/main/java/ch/zhaw/ads/LabyrinthServer.java b/08/src/main/java/ch/zhaw/ads/LabyrinthServer.java index c327306..7613423 100644 --- a/08/src/main/java/ch/zhaw/ads/LabyrinthServer.java +++ b/08/src/main/java/ch/zhaw/ads/LabyrinthServer.java @@ -6,7 +6,7 @@ public class LabyrinthServer implements CommandExecutor { final double SCALE = 10; private ServerGraphics serverGraphics; - private Graph graph; + Graph graph; @Override public String execute(String command) { @@ -17,19 +17,68 @@ public class LabyrinthServer implements CommandExecutor { serverGraphics.setColor(Color.BLACK); serverGraphics.fillRect(0, 0, 1, 1); + // read in data for (String record : command.split("\\r?\\n")) { String[] values = record.split(" "); try { graph.addEdge(values[0], values[1], 0.0); graph.addEdge(values[1], values[0], 0.0); - } catch (Throwable e) {} + } catch (Throwable e) { + e.printStackTrace(); + } this.drawPath(serverGraphics, values[0], values[1], false); } + + + // find path + // start and goal are taken from the picture in the exercise + LabyrinthNode start = graph.findNode("0-6"); + LabyrinthNode goal = graph.findNode("3-0"); + this.unmarkAll(); + if (this.search(start, goal)) { + LabyrinthNode current = goal; + while (current.getPrev() != null) { + LabyrinthNode prev = current.getPrev(); + this.drawPath(serverGraphics, current.getName(), prev.getName(), true); + current = prev; + } + } else { + System.out.println("Failed to find path."); + } + return serverGraphics.getTrace(); } + + private void unmarkAll() { + for (LabyrinthNode node : graph.getNodes()) { + node.setMark(false); + node.setPrev(null); + } + } + + + private boolean search(LabyrinthNode currentNode, LabyrinthNode goal) { + currentNode.setMark(true); + + if (currentNode.equals(goal)) return true; + + for (Edge edge : currentNode.getEdges()) { + LabyrinthNode sucessor = edge.getDest(); + if (!sucessor.getMark()) { + if (this.search(sucessor, goal)) { + sucessor.setPrev(currentNode); + return true; + } + } + } + currentNode.setMark(false); + return false; + } + + private void drawPath(ServerGraphics g, String from, String to, boolean mouse) { if (mouse) serverGraphics.setColor(Color.RED); else serverGraphics.setColor(Color.WHITE);