solved s08 ex3

This commit is contained in:
navid.sassan 2020-11-16 16:15:31 +01:00
parent 2a06e452cc
commit 11cf6cae90
6 changed files with 191 additions and 0 deletions

View 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;
}
}

View 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;}
}

View 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 ;
}

View File

@ -0,0 +1,6 @@
package ch.zhaw.ads;
import java.util.*;
public class LabyrinthNode<E> extends Node<E> {
}

View 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);
}
}
}

View 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();
}
}