Ich hab kein Bock mehr
This commit is contained in:
parent
c331208620
commit
6838a34e0d
3 changed files with 42 additions and 11 deletions
|
|
@ -54,7 +54,10 @@ public class Graph<T> {
|
||||||
if(edge != null) {
|
if(edge != null) {
|
||||||
return edge;
|
return edge;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (DEBUG)
|
||||||
|
System.out.println("??" + nodeA + " " + nodeB);
|
||||||
|
|
||||||
edge = new Edge<>(nodeA, nodeB);
|
edge = new Edge<>(nodeA, nodeB);
|
||||||
this.edges.add(edge);
|
this.edges.add(edge);
|
||||||
return edge;
|
return edge;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import java.util.*;
|
||||||
public abstract class GraphAlgorithm<T> {
|
public abstract class GraphAlgorithm<T> {
|
||||||
|
|
||||||
final static boolean DEBUG = true;
|
final static boolean DEBUG = true;
|
||||||
final static boolean DEBUG_2 = false;
|
final static boolean DEBUG_2 = true;
|
||||||
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
|
|
@ -80,13 +80,17 @@ public abstract class GraphAlgorithm<T> {
|
||||||
|
|
||||||
Iterator<Node<T>> iter = availableNodes.iterator();
|
Iterator<Node<T>> iter = availableNodes.iterator();
|
||||||
Iterator<Node<T>> minElemIter = availableNodes.iterator();
|
Iterator<Node<T>> minElemIter = availableNodes.iterator();
|
||||||
AlgorithmNode<T> minElem;
|
|
||||||
AlgorithmNode<T> tempElem;
|
Node<T> minNode;
|
||||||
|
AlgorithmNode<T> minAlgoNode;
|
||||||
|
Node<T> tempNode;
|
||||||
|
AlgorithmNode<T> tempAlgoNode;
|
||||||
|
|
||||||
if (iter.hasNext()) {
|
if (iter.hasNext()) {
|
||||||
|
|
||||||
// Set minimum to first element
|
// Set minimum to first element
|
||||||
minElem = algorithmNodes.get(iter.next());
|
minNode = iter.next();
|
||||||
|
minAlgoNode = algorithmNodes.get(minNode);
|
||||||
minElemIter.next();
|
minElemIter.next();
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
|
|
@ -99,20 +103,24 @@ public abstract class GraphAlgorithm<T> {
|
||||||
System.out.flush();
|
System.out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
tempElem = algorithmNodes.get(iter.next());
|
tempNode = iter.next();
|
||||||
if (tempElem.value > 0) {
|
tempAlgoNode = algorithmNodes.get(tempNode);
|
||||||
if (tempElem.value < minElem.value) {
|
if (tempAlgoNode.value > 0) {
|
||||||
|
if (tempAlgoNode.value < minAlgoNode.value) {
|
||||||
|
|
||||||
// New minimum
|
// New minimum
|
||||||
minElem = tempElem;
|
minNode = tempNode;
|
||||||
minElemIter = iter;
|
minAlgoNode = tempAlgoNode;
|
||||||
|
|
||||||
|
// Move minElemIter to the minimum
|
||||||
|
while (!minElemIter.next().equals(minNode)) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
minElemIter.remove();
|
minElemIter.remove();
|
||||||
|
|
||||||
return minElem;
|
return minAlgoNode;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -168,6 +176,8 @@ public abstract class GraphAlgorithm<T> {
|
||||||
|
|
||||||
n.value = a;
|
n.value = a;
|
||||||
n.previous = v;
|
n.previous = v;
|
||||||
|
|
||||||
|
System.out.println("--" + n + " " + n.previous);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -175,6 +185,9 @@ public abstract class GraphAlgorithm<T> {
|
||||||
}
|
}
|
||||||
v = getSmallestNode();
|
v = getSmallestNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (DEBUG_2)
|
||||||
|
System.out.println("Finished run");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -190,6 +203,9 @@ public abstract class GraphAlgorithm<T> {
|
||||||
*/
|
*/
|
||||||
public List<Edge<T>> getPath(Node<T> destination) {
|
public List<Edge<T>> getPath(Node<T> destination) {
|
||||||
|
|
||||||
|
if (DEBUG_2)
|
||||||
|
System.out.println("Enter getPath");
|
||||||
|
|
||||||
// Stage 1
|
// Stage 1
|
||||||
ArrayList<Edge<T>> reversePath = new ArrayList<Edge<T>>();
|
ArrayList<Edge<T>> reversePath = new ArrayList<Edge<T>>();
|
||||||
|
|
||||||
|
|
@ -199,8 +215,13 @@ public abstract class GraphAlgorithm<T> {
|
||||||
|
|
||||||
while (!(prevNode.value == 0)) {
|
while (!(prevNode.value == 0)) {
|
||||||
|
|
||||||
|
if (DEBUG_2)
|
||||||
|
System.out.println("Iteration");
|
||||||
|
|
||||||
AlgorithmNode<T> prevPrevNode = prevNode.previous;
|
AlgorithmNode<T> prevPrevNode = prevNode.previous;
|
||||||
|
|
||||||
|
if (prevNode.equals(prevPrevNode)) throw new IndexOutOfBoundsException();
|
||||||
|
|
||||||
if (prevPrevNode == null)
|
if (prevPrevNode == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
|
@ -211,6 +232,12 @@ public abstract class GraphAlgorithm<T> {
|
||||||
|
|
||||||
// Stage 2
|
// Stage 2
|
||||||
Collections.reverse(reversePath);
|
Collections.reverse(reversePath);
|
||||||
|
|
||||||
|
if (DEBUG_2) {
|
||||||
|
System.out.println(reversePath);
|
||||||
|
System.out.println("Exit getPath");
|
||||||
|
}
|
||||||
|
|
||||||
return reversePath;
|
return reversePath;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -249,6 +249,7 @@ public class GameMap {
|
||||||
private List<Node<Castle>> allCastlesInRadius(Node<Castle> castle, List<Node<Castle>> allCastles, double r) {
|
private List<Node<Castle>> allCastlesInRadius(Node<Castle> castle, List<Node<Castle>> allCastles, double r) {
|
||||||
|
|
||||||
return allCastles.stream()
|
return allCastles.stream()
|
||||||
|
.filter(x -> !x.equals(castle))
|
||||||
.filter(x -> x.getValue().distance(castle.getValue()) <= r)
|
.filter(x -> x.getValue().distance(castle.getValue()) <= r)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue