WIP GraphAlgorithm

This commit is contained in:
joachimschmidt557 2019-03-22 18:49:27 +01:00
parent 07e292a5e1
commit 9f62d1bccc
4 changed files with 116 additions and 3 deletions

View file

@ -10,6 +10,10 @@ import java.util.*;
*/
public abstract class GraphAlgorithm<T> {
final static boolean DEBUG = true;
int n = 0;
/**
* Innere Klasse um {@link Node} zu erweitern, aber nicht zu verändern Sie weist
* jedem Knoten einen Wert und einen Vorgängerknoten zu.
@ -88,6 +92,9 @@ public abstract class GraphAlgorithm<T> {
}
}
if (DEBUG)
n += 0;
}
iter.remove();
@ -121,6 +128,10 @@ public abstract class GraphAlgorithm<T> {
while (v != null) {
for (Edge<T> e : graph.getEdges(v.node)) {
if (DEBUG)
n += 1;
if (isPassable(e)) {
AlgorithmNode<T> n = algorithmNodes.get(e.getOtherNode(v.node));
@ -160,7 +171,7 @@ public abstract class GraphAlgorithm<T> {
if (prevNode == null)
return null;
while (prevNode != null) {
while (prevNode.value != 0) {
AlgorithmNode<T> prevPrevNode = prevNode.previous;
@ -212,4 +223,12 @@ public abstract class GraphAlgorithm<T> {
* @return true, wenn der Knoten passierbar ist.
*/
protected abstract boolean isPassable(Node<T> node);
public int getN() {
if (DEBUG)
return n;
else
return 0;
}
}

View file

@ -31,7 +31,7 @@ public class GameConstants {
public static final Goal GAME_GOALS[] = {
new ConquerGoal(),
new TimeGoal(),
// TODO: Add more Goals
new CaptureTheFlagGoal(),
};
public static final Class<?> PLAYER_TYPES[] = {

View file

@ -12,6 +12,8 @@ import game.map.Castle;
public class CaptureTheFlagGoal extends Goal {
final static boolean DEBUG = true;
final static int ROUND_FOR_DISTRIBUTION = 2;
final static int MIN_ROUND_FOR_WIN = 2;
final static int NUM_FLAG_CASTLES = 4;
@ -26,17 +28,25 @@ public class CaptureTheFlagGoal extends Goal {
public CaptureTheFlagGoal() {
super("Capture the flag", "Derjenige Spieler gewinnt, der zuerst"
+ " alle " + NUM_FLAG_CASTLES + " Flaggen-Schlösser erobert.");
flagCastles = new ArrayList<Castle>();
}
@Override
public boolean isCompleted() {
if (DEBUG)
System.out.println("Checking is completed");
Game game = this.getGame();
// As this function is called every round,
// let's check if we can set the flag castles
if (flagCastles == null && game.getRound() > ROUND_FOR_DISTRIBUTION) {
if (flagCastles.isEmpty() && game.getRound() >= ROUND_FOR_DISTRIBUTION) {
if (DEBUG)
System.out.println("Distributing");
distributeFlagCastles();
return false;
}
return this.getWinner() != null;
@ -95,6 +105,9 @@ public class CaptureTheFlagGoal extends Goal {
for (int i = 0; i < players.size(); i++) {
if (DEBUG)
System.out.println("Distributing for " + players.get(i).getName());
int playersLeft = players.size() - i;
int numCastlesForPlayer = numCastlesToDistribute / playersLeft;
@ -136,6 +149,9 @@ public class CaptureTheFlagGoal extends Goal {
}
if (DEBUG)
System.out.println(nums);
return nums.stream().map(x -> castles.get(x)).collect(Collectors.toList());

View file

@ -0,0 +1,78 @@
package tests.student;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import base.Edge;
import base.Graph;
import base.GraphAlgorithm;
import base.Node;
public class GraphAlgoComplexityTest {
@Test
public void test() {
for (int i = 1; i < 100; i++) {
Graph<Integer> g = generateGraph(i);
DumbGraphAlgorithm ga = new DumbGraphAlgorithm(g, g.getNodes().get(0));
ga.run();
double divN = (double)ga.getN() / (double)i;
double divN2 = (double)ga.getN() / ((double)i*i);
double divN3 = (double)ga.getN() / ((double)i*i*i);
System.out.println(i + "," + ga.getN() + "," + divN + "," + divN2 + "," + divN3);
}
}
public Graph<Integer> generateGraph(int n) {
Graph<Integer> g = new Graph<Integer>();
List<Node<Integer>> nodes = new ArrayList<>();
for (int i = 0; i < n; i++)
nodes.add(g.addNode(i));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j)
g.addEdge(nodes.get(i), nodes.get(j));
}
}
return g;
}
public class DumbGraphAlgorithm extends GraphAlgorithm<Integer> {
public DumbGraphAlgorithm(Graph<Integer> graph, Node<Integer> sourceNode) {
super(graph, sourceNode);
}
@Override
protected double getValue(Edge<Integer> edge) {
return 100.0;
}
@Override
protected boolean isPassable(Edge<Integer> edge) {
return true;
}
@Override
protected boolean isPassable(Node<Integer> node) {
return true;
}
}
}