solved Task 8.3 and 8.4

This commit is contained in:
schrom01 2022-11-08 13:47:52 +01:00
parent 46e1516a5a
commit 72d99228e8
1 changed files with 23 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package ch.zhaw.ads; package ch.zhaw.ads;
import java.awt.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Vector; import java.util.Vector;
@ -8,10 +9,9 @@ public class LabyrinthServer implements CommandExecutor {
public Graph<DijkstraNode, Edge> createGraph(String s) { public Graph<DijkstraNode, Edge> createGraph(String s) {
Graph<DijkstraNode, Edge> graph = new AdjListGraph<>(DijkstraNode.class, Edge.class); Graph<DijkstraNode, Edge> graph = new AdjListGraph<>(DijkstraNode.class, Edge.class);
String[] lines = s.split("\n"); Arrays.asList(s.split("\n")).forEach(s1 -> {
Arrays.asList(lines).forEach(s1 -> {
String[] nodes = s1.split(" ");
try { try {
String[] nodes = s1.split(" ");
int x1 = Integer.parseInt(nodes[0].split("-")[0]); int x1 = Integer.parseInt(nodes[0].split("-")[0]);
int x2 = Integer.parseInt(nodes[1].split("-")[0]); int x2 = Integer.parseInt(nodes[1].split("-")[0]);
int y1 = Integer.parseInt(nodes[0].split("-")[1]); int y1 = Integer.parseInt(nodes[0].split("-")[1]);
@ -24,21 +24,38 @@ public class LabyrinthServer implements CommandExecutor {
} }
}); });
return graph; return graph;
// TODO implement 8.2
} }
public void drawLabyrinth(Graph<DijkstraNode, Edge> graph) { public void drawLabyrinth(Graph<DijkstraNode, Edge> graph) {
// TODO implement 8.3 // 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) { 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; return false;
} }
// search and draw result // search and draw result
public void drawRoute(Graph<DijkstraNode, Edge> graph, String startNode, String zielNode) { public void drawRoute(Graph<DijkstraNode, Edge> 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) { public String execute(String s) {