From 9f62d1bcccff80711254553bfbc900b4a02d6b63 Mon Sep 17 00:00:00 2001 From: joachimschmidt557 Date: Fri, 22 Mar 2019 18:49:27 +0100 Subject: [PATCH] WIP GraphAlgorithm --- .../src/base/GraphAlgorithm.java | 21 ++++- Projektgruppe_175/src/game/GameConstants.java | 2 +- .../src/game/goals/CaptureTheFlagGoal.java | 18 ++++- .../student/GraphAlgoComplexityTest.java | 78 +++++++++++++++++++ 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 Projektgruppe_175/src/tests/student/GraphAlgoComplexityTest.java diff --git a/Projektgruppe_175/src/base/GraphAlgorithm.java b/Projektgruppe_175/src/base/GraphAlgorithm.java index 2c189ca..1e2f552 100644 --- a/Projektgruppe_175/src/base/GraphAlgorithm.java +++ b/Projektgruppe_175/src/base/GraphAlgorithm.java @@ -10,6 +10,10 @@ import java.util.*; */ public abstract class GraphAlgorithm { + 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 { } } + if (DEBUG) + n += 0; + } iter.remove(); @@ -121,6 +128,10 @@ public abstract class GraphAlgorithm { while (v != null) { for (Edge e : graph.getEdges(v.node)) { + + if (DEBUG) + n += 1; + if (isPassable(e)) { AlgorithmNode n = algorithmNodes.get(e.getOtherNode(v.node)); @@ -160,7 +171,7 @@ public abstract class GraphAlgorithm { if (prevNode == null) return null; - while (prevNode != null) { + while (prevNode.value != 0) { AlgorithmNode prevPrevNode = prevNode.previous; @@ -212,4 +223,12 @@ public abstract class GraphAlgorithm { * @return true, wenn der Knoten passierbar ist. */ protected abstract boolean isPassable(Node node); + + public int getN() { + if (DEBUG) + return n; + else + return 0; + } + } diff --git a/Projektgruppe_175/src/game/GameConstants.java b/Projektgruppe_175/src/game/GameConstants.java index 1222da4..1162cf5 100644 --- a/Projektgruppe_175/src/game/GameConstants.java +++ b/Projektgruppe_175/src/game/GameConstants.java @@ -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[] = { diff --git a/Projektgruppe_175/src/game/goals/CaptureTheFlagGoal.java b/Projektgruppe_175/src/game/goals/CaptureTheFlagGoal.java index 63a21ed..beecaaf 100644 --- a/Projektgruppe_175/src/game/goals/CaptureTheFlagGoal.java +++ b/Projektgruppe_175/src/game/goals/CaptureTheFlagGoal.java @@ -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(); } @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()); diff --git a/Projektgruppe_175/src/tests/student/GraphAlgoComplexityTest.java b/Projektgruppe_175/src/tests/student/GraphAlgoComplexityTest.java new file mode 100644 index 0000000..87144eb --- /dev/null +++ b/Projektgruppe_175/src/tests/student/GraphAlgoComplexityTest.java @@ -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 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 generateGraph(int n) { + + Graph g = new Graph(); + + List> 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 { + + public DumbGraphAlgorithm(Graph graph, Node sourceNode) { + super(graph, sourceNode); + } + + @Override + protected double getValue(Edge edge) { + return 100.0; + } + + @Override + protected boolean isPassable(Edge edge) { + return true; + } + + @Override + protected boolean isPassable(Node node) { + return true; + } + + } + +}