Add files for Exercise 08

This commit is contained in:
Manuel Thalmann 2022-11-08 13:25:50 +01:00
parent 235f9857c9
commit 466bd318ab
6 changed files with 318 additions and 0 deletions

16
Labyrinth.txt Normal file
View file

@ -0,0 +1,16 @@
0-6 4-6
4-6 7-6
7-6 9-6
7-6 7-4
7-4 6-4
7-4 9-4
9-4 9-1
7-4 7-1
7-1 5-1
4-6 4-4
4-4 4-3
4-4 1-4
1-4 1-1
1-1 3-1
3-1 3-2
3-1 3-0

View file

@ -0,0 +1,30 @@
package ch.nuth.zhaw.exbox;
public class LabyrinthServer implements CommandExecutor {
ServerGraphics g = new ServerGraphics();
public Graph<DijkstraNode, Edge> createGraph(String s) {
// TODO implement 8.2
}
public void drawLabyrinth(Graph<DijkstraNode, Edge> graph) {
// TODO implement 8.3
}
private boolean search(DijkstraNode current, DijkstraNode ziel) {
// TODO implement 8.4
}
// search and draw result
public void drawRoute(Graph<DijkstraNode, Edge> graph, String startNode, String zielNode) {
// TODO implement 8.4
}
public String execute(String s) {
Graph<DijkstraNode, Edge> graph;
graph = createGraph(s);
drawLabyrinth(graph);
drawRoute(graph, "0-6", "3-0");
return g.getTrace();
}
}

View file

@ -0,0 +1,103 @@
package ch.nuth.zhaw.exbox;
import java.awt.Color;
public class ServerGraphics {
private StringBuffer b;
private static ServerGraphics theGraphics;
public static ServerGraphics instance() {
if (theGraphics == null) {
theGraphics = new ServerGraphics();
}
return theGraphics;
}
public ServerGraphics() {
clear();
theGraphics = this;
}
public void clear() {
b = new StringBuffer();
}
public String getTrace() {
return new String(b);
}
private double round(double d) {
return Math.round(d * 10000) / 10000.0;
}
public void drawLine(double x1, double y1, double x2, double y2) {
b.append("<line x1=\"");
b.append(round(x1));
b.append("\" y1=\"");
b.append(round(y1));
b.append("\" x2=\"");
b.append(round(x2));
b.append("\" y2=\"");
b.append(round(y2));
b.append("\"/>\n");
}
public void drawRect(double x, double y, double w, double h) {
b.append("<rect x=\"");
b.append(round(x));
b.append("\" y=\"");
b.append(round(y));
b.append("\" ");
b.append("width=\"");
b.append(round(w));
b.append("\" height=\"");
b.append(round(h));
b.append("\" style=\"draw\" />\n");
}
public void fillRect(double x, double y, double w, double h) {
b.append("<rect x=\"");
b.append(round(x));
b.append("\" y=\"");
b.append(round(y));
b.append("\" ");
b.append("width=\"");
b.append(round(w));
b.append("\" height=\"");
b.append(round(h));
b.append("\" style=\"fill\" />\n");
}
public void setColor(Color c) {
b.append("<color red=\"");
b.append(c.getRed());
b.append("\" green=\"");
b.append(c.getGreen());
b.append("\" blue=\"");
b.append(c.getBlue());
b.append("\"/>\n");
}
public void drawPath(String from, String to, boolean line) {
double scale = 11;
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 (line) {
drawLine(x0 + w / 2, y0 + w / 2, x1 + w / 2, y1 + w / 2);
} else {
if (Math.abs(y0 - y1) < 1E-10) {
fillRect(x0, y0, x1 - x0 + w, w);
} else {
fillRect(x0, y0, w, y1 - y0 + w);
}
}
}
}

View file

@ -0,0 +1,43 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS8_2_test {
LabyrinthServer labyrinthServer;
Graph<DijkstraNode, Edge> graph;
@BeforeEach
public void init() {
String labyrinth = "0-6 4-6\n4-6 7-6\n7-6 9-6\n7-6 7-4\n7-4 6-4\n7-4 9-4\n9-4 9-1\n" +
"7-4 7-1\n7-1 5-1\n4-6 4-4\n4-4 4-3\n4-4 1-4\n1-4 1-1\n1-1 3-1\n3-1 3-2\n3-1 3-0\n";
labyrinthServer = new LabyrinthServer();
graph = labyrinthServer.createGraph(labyrinth);
}
private void testEdge(String startName, String destName) {
DijkstraNode node = graph.findNode(startName);
for (Edge edge : node.getEdges()) {
if (edge.getDest().getName().equals(destName)) {
return;
}
}
fail(startName + " not connected to " + destName);
}
@Test
public void testCreateGraph() {
testEdge("0-6", "4-6");
testEdge("4-6", "0-6");
testEdge("1-4", "1-1");
testEdge("3-1", "3-0");
testEdge("3-0", "3-1");
}
}

View file

@ -0,0 +1,57 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS8_3_test {
LabyrinthServer labyrinthServer;
Graph<DijkstraNode, Edge> graph;
@BeforeEach
public void init() {
String labyrinth = "0-6 4-6\n4-6 7-6\n7-6 9-6\n7-6 7-4\n7-4 6-4\n7-4 9-4\n9-4 9-1\n" +
"7-4 7-1\n7-1 5-1\n4-6 4-4\n4-4 4-3\n4-4 1-4\n1-4 1-1\n1-1 3-1\n3-1 3-2\n3-1 3-0\n";
labyrinthServer = new LabyrinthServer();
graph = labyrinthServer.createGraph(labyrinth);
}
private void testEdge(String startName, String destName) {
DijkstraNode node = graph.findNode(startName);
for (Edge edge : node.getEdges()) {
if (edge.getDest().getName().equals(destName)) {
return;
}
}
fail(startName + " not connected to " + destName);
}
@Test
public void testCreateGraph() {
testEdge("0-6", "4-6");
testEdge("4-6", "0-6");
testEdge("1-4", "1-1");
testEdge("3-1", "3-0");
testEdge("3-0", "3-1");
}
private void testPath(String trace, String path) {
if (!trace.contains(path)) {
fail(path + " not found");
}
}
@Test
public void testDrawLabyrinth() {
labyrinthServer.drawLabyrinth(graph);
String trace = ServerGraphics.instance().getTrace();
testPath(trace, "<rect x=\"0.5455\" y=\"0.3636\" width=\"0.1818\" height=\"0.0909\"");
testPath(trace, "<rect x=\"0.2727\" y=\"0.0909\" width=\"0.0909\" height=\"0.1818\"");
}
}

View file

@ -0,0 +1,69 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author K Rege
* @version 1.00 2018/3/17
* @version 1.01 2021/8/1
*/
public class ADS8_4_test {
LabyrinthServer labyrinthServer;
Graph<DijkstraNode, Edge> graph;
@BeforeEach
public void init() {
String labyrinth = "0-6 4-6\n4-6 7-6\n7-6 9-6\n7-6 7-4\n7-4 6-4\n7-4 9-4\n9-4 9-1\n" +
"7-4 7-1\n7-1 5-1\n4-6 4-4\n4-4 4-3\n4-4 1-4\n1-4 1-1\n1-1 3-1\n3-1 3-2\n3-1 3-0\n";
labyrinthServer = new LabyrinthServer();
graph = labyrinthServer.createGraph(labyrinth);
}
private void testEdge(String startName, String destName) {
DijkstraNode node = graph.findNode(startName);
for (Edge edge : node.getEdges()) {
if (edge.getDest().getName().equals(destName)) {
return;
}
}
fail(startName + " not connected to " + destName);
}
@Test
public void testCreateGraph() {
testEdge("0-6", "4-6");
testEdge("4-6", "0-6");
testEdge("1-4", "1-1");
testEdge("3-1", "3-0");
testEdge("3-0", "3-1");
}
private void testPath(String trace, String path) {
if (!trace.contains(path)) {
fail(path + " not found");
}
}
@Test
public void testDrawLabyrinth() {
labyrinthServer.drawLabyrinth(graph);
String trace = ServerGraphics.instance().getTrace();
testPath(trace, "<rect x=\"0.5455\" y=\"0.3636\" width=\"0.1818\" height=\"0.0909\"");
testPath(trace, "<rect x=\"0.2727\" y=\"0.0909\" width=\"0.0909\" height=\"0.1818\"");
}
@Test
public void testDrawRoute() {
labyrinthServer.drawRoute(graph, "0-6", "3-0");
String trace = ServerGraphics.instance().getTrace();
testPath(trace, "<line x1=\"0.3182\" y1=\"0.0455\" x2=\"0.3182\" y2=\"0.1364\"/>");
testPath(trace, "<line x1=\"0.1364\" y1=\"0.1364\" x2=\"0.3182\" y2=\"0.1364\"/>");
testPath(trace, "<line x1=\"0.1364\" y1=\"0.1364\" x2=\"0.1364\" y2=\"0.4091\"/>");
testPath(trace, "<line x1=\"0.1364\" y1=\"0.4091\" x2=\"0.4091\" y2=\"0.4091\"/>");
testPath(trace, "<line x1=\"0.4091\" y1=\"0.4091\" x2=\"0.4091\" y2=\"0.5909\"/>");
testPath(trace, "<line x1=\"0.0455\" y1=\"0.5909\" x2=\"0.4091\" y2=\"0.5909\"/>");
}
}