solved s08 ex3
This commit is contained in:
parent
2a06e452cc
commit
11cf6cae90
54
08/src/main/java/ch/zhaw/ads/AdjListGraph.java
Normal file
54
08/src/main/java/ch/zhaw/ads/AdjListGraph.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class AdjListGraph<N extends Node,E extends Edge>
|
||||||
|
implements Graph<N, E> {
|
||||||
|
private final List<N> nodes = new LinkedList<N>();
|
||||||
|
private final Class nodeClazz;
|
||||||
|
private final Class edgeClazz;
|
||||||
|
|
||||||
|
public AdjListGraph(Class nodeClazz, Class edgeClazz) {
|
||||||
|
this.nodeClazz = nodeClazz;
|
||||||
|
this.edgeClazz = edgeClazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
// füge Knoten hinzu, gebe alten zurück falls Knoten schon existiert
|
||||||
|
public N addNode(String name) throws Throwable {
|
||||||
|
N node = findNode(name);
|
||||||
|
if (node == null) {
|
||||||
|
node = (N) nodeClazz.newInstance();
|
||||||
|
node.setName(name);
|
||||||
|
nodes.add(node);
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// füge gerichtete Kante hinzu
|
||||||
|
public void addEdge(String source, String dest, double weight) throws Throwable {
|
||||||
|
N src = addNode(source);
|
||||||
|
N dst = addNode(dest);
|
||||||
|
|
||||||
|
try {
|
||||||
|
E edge = (E) edgeClazz.newInstance();
|
||||||
|
edge.setDest(dst);
|
||||||
|
edge.setWeight(weight);
|
||||||
|
src.addEdge(edge);
|
||||||
|
} catch (Exception e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// finde den Knoten anhand seines Namens
|
||||||
|
public N findNode(String name) {
|
||||||
|
for (N node : nodes) {
|
||||||
|
if (node.getName().equals(name)) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterator über alle Knoten
|
||||||
|
public Iterable<N> getNodes() {
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
}
|
20
08/src/main/java/ch/zhaw/ads/Edge.java
Normal file
20
08/src/main/java/ch/zhaw/ads/Edge.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
public class Edge<N>
|
||||||
|
{
|
||||||
|
protected N dest; // Zielknoten der Kante
|
||||||
|
protected double weight; // Kantengewicht
|
||||||
|
|
||||||
|
public Edge() {}
|
||||||
|
|
||||||
|
public Edge(N dest, double weight) {
|
||||||
|
this.dest = dest;
|
||||||
|
this.weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDest(N node) {this.dest = node;}
|
||||||
|
public N getDest() {return dest;}
|
||||||
|
|
||||||
|
public void setWeight(double w) {this.weight = w;}
|
||||||
|
double getWeight() {return weight;}
|
||||||
|
}
|
20
08/src/main/java/ch/zhaw/ads/Graph.java
Normal file
20
08/src/main/java/ch/zhaw/ads/Graph.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public interface Graph<N,E> {
|
||||||
|
|
||||||
|
// füge Knoten hinzu, tue nichts, falls Knoten schon existiert
|
||||||
|
N addNode (String name) throws Throwable;
|
||||||
|
|
||||||
|
// finde den Knoten anhand seines Namens
|
||||||
|
N findNode(String name);
|
||||||
|
|
||||||
|
// Iterator über alle Knoten des Graphen
|
||||||
|
Iterable<N> getNodes();
|
||||||
|
|
||||||
|
// füge gerichtete und gewichtete Kante hinzu
|
||||||
|
void addEdge(String source, String dest, double weight) throws Throwable ;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
6
08/src/main/java/ch/zhaw/ads/LabyrinthNode.java
Normal file
6
08/src/main/java/ch/zhaw/ads/LabyrinthNode.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class LabyrinthNode<E> extends Node<E> {
|
||||||
|
}
|
54
08/src/main/java/ch/zhaw/ads/LabyrinthServer.java
Normal file
54
08/src/main/java/ch/zhaw/ads/LabyrinthServer.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
|
public class LabyrinthServer implements CommandExecutor {
|
||||||
|
final double SCALE = 10;
|
||||||
|
|
||||||
|
private ServerGraphics serverGraphics;
|
||||||
|
private Graph<LabyrinthNode, Edge> graph;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String execute(String command) {
|
||||||
|
serverGraphics = new ServerGraphics();
|
||||||
|
graph = new AdjListGraph<LabyrinthNode, Edge>(LabyrinthNode.class, Edge.class);
|
||||||
|
|
||||||
|
serverGraphics.clear();
|
||||||
|
serverGraphics.setColor(Color.BLACK);
|
||||||
|
serverGraphics.fillRect(0, 0, 1, 1);
|
||||||
|
|
||||||
|
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) {}
|
||||||
|
|
||||||
|
this.drawPath(serverGraphics, values[0], values[1], false);
|
||||||
|
}
|
||||||
|
return serverGraphics.getTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawPath(ServerGraphics g, String from, String to, boolean mouse) {
|
||||||
|
if (mouse) serverGraphics.setColor(Color.RED);
|
||||||
|
else serverGraphics.setColor(Color.WHITE);
|
||||||
|
double scale = 10;
|
||||||
|
double xh0 = from.charAt(0) - '0';
|
||||||
|
double yh0 = from.charAt(2) - '0';
|
||||||
|
double xh1 = to.charAt(0) - '0';
|
||||||
|
double yh1 = to.charAt(2) - '0';
|
||||||
|
double x0 = Math.min(xh0,xh1)/SCALE;
|
||||||
|
double y0 = Math.min(yh0,yh1)/SCALE;
|
||||||
|
double x1 = Math.max(xh0,xh1)/SCALE;
|
||||||
|
double y1 = Math.max(yh0,yh1)/SCALE;
|
||||||
|
double w = 1/SCALE;
|
||||||
|
if (mouse) g.drawLine(x0+w/2,y0+w/2,x1+w/2,y1+w/2);
|
||||||
|
else {
|
||||||
|
if (y0 == y1)
|
||||||
|
g.fillRect(x0,y0,x1-x0+w,w);
|
||||||
|
else
|
||||||
|
g.fillRect(x0,y0,w,y1-y0+w);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
08/src/main/java/ch/zhaw/ads/Node.java
Normal file
37
08/src/main/java/ch/zhaw/ads/Node.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package ch.zhaw.ads;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Node<E> {
|
||||||
|
protected String name; // Name
|
||||||
|
protected List<E> edges; // Kanten
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
edges = new LinkedList<E>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(String name) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterable<E> getEdges() {
|
||||||
|
return edges;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addEdge(E edge) {
|
||||||
|
edges.add(edge);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Node other) {
|
||||||
|
return this.name == other.getName();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user