Add code for determining and drawing a route
This commit is contained in:
parent
ed5cfd5cb9
commit
785b359d37
|
@ -51,14 +51,43 @@ public class LabyrinthServer implements CommandExecutor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean search(DijkstraNode current, DijkstraNode ziel) {
|
private boolean search(DijkstraNode current, DijkstraNode destination) {
|
||||||
// TODO implement 8.4
|
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;
|
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 destinationNode) {
|
||||||
// TODO implement 8.4
|
// 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) {
|
public String execute(String s) {
|
||||||
|
|
Loading…
Reference in a new issue