From 7ffe8803c2b5781d339020434e64caa0333b0e51 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Tue, 3 Apr 2018 17:05:02 +0200 Subject: [PATCH] improved world class --- OGP1718-Worms/src/worms/facade/Facade.java | 2 +- OGP1718-Worms/src/worms/model/World.java | 224 +++++++++++++++++---- OGP1718-Worms/src/worms/model/Worm.java | 3 +- 3 files changed, 193 insertions(+), 36 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index 300ed6c..b2a9f8e 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -369,7 +369,7 @@ public class Facade implements IFacade { } /** - * Check whether the given worls is terminated. + * Check whether the given world is terminated. * * @param world */ diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index d2261a6..575df5a 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -1,10 +1,9 @@ package worms.model; +import java.util.*; + public class World { - private boolean terminated = false; - - public World(double width, double height, boolean[][] map) { if (!isValidDimension(width) || !isValidDimension(height)) throw new IllegalArgumentException(); @@ -12,12 +11,67 @@ public class World { this.width = width; this.height = height; this.map = map; + this.wormList = new ArrayList<>(); + this.foodSet = new HashSet<>(); - lengthX = width / map[0].length; - lengthY = height / map.length; + this.lengthX = width / map[0].length; + this.lengthY = height / map.length; + this.game = false; } + // region game + //=================================================================================== + + public boolean hasActiveGame() { + return this.game; + } + + public void startGame() { + this.game = true; + this.activeWorm = 0; + } + + public void finishGame() { + this.game = false; + } + + public void activateNextWorm() { + + this.activeWorm++; + if (this.activeWorm == getWormList().size()) { + this.activeWorm = 0; + } + } + + public Worm getActiveWorm() { + return getWormList().get(this.activeWorm); + } + + public String getWinner() { + if (getWormList().size() == 1) { + return getWormList().get(0).getName(); + } else if (getWormList().size() > 1) { + + Team lastTeam = getWormList().get(0).getTeam(); + for(Worm worm: getWormList()) { + if (!lastTeam.equals(worm.getTeam())) { + return null; + } + } + return lastTeam.getName(); + + } else { + return null; + } + } + + private boolean game; + private int activeWorm; + + //=================================================================================== + // endregion + // region Dimensions //=================================================================================== @@ -38,15 +92,37 @@ public class World { // =================================================================================== // endregion + + // region map //=================================================================================== - - private final double lengthX; - private final double lengthY; public boolean[][] getMap() { return map; } + + public boolean isTerminated() { + return this.terminated; + } + + public void terminate() { + this.terminated = true; + } + + private final boolean[][] map; + private boolean terminated = false; + + // =================================================================================== + // endregion + + // region passable + //=================================================================================== + + /** + * + * @param location ... + * @return ... + */ public boolean isPassable(double[] location) { if (location[0] / lengthX >= getMap()[0].length || location[0] < 0.0 || @@ -56,23 +132,12 @@ public class World { return this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)]; } - private boolean isPassable(double x, double y) { - int xCoord = (int) Math.floor(x / lengthX); - int yCoord = (int) Math.floor(y / lengthY); - return this.map[yCoord][xCoord]; - } - - private boolean canHaveAsPartialCircle(double x, double y, double[] center) { - - if (!isPassable(x + center[0], y + center[1])) return false; - x *= -1; - if (!isPassable(x + center[0], y + center[1])) return false; - y *= -1; - if (!isPassable(x + center[0], y + center[1])) return false; - x *= -1; - return isPassable(x + center[0], y + center[1]); - } - + /** + * + * @param center ... + * @param radius ... + * @return ... + */ public boolean isPassable(double[] center, double radius) { int radX = (int) Math.floor(radius / lengthX); @@ -90,6 +155,12 @@ public class World { return true; } + /** + * + * @param center ... + * @param radius ... + * @return ... + */ public boolean isAdjacent(double[] center, double radius) { double maxDistance = radius * 0.1; @@ -99,20 +170,105 @@ public class World { return isPassable(center, radius) && !isPassable(center, radius + maxDistance + length); } - public boolean isTerminated() { - return terminated; + private final double lengthX; + private final double lengthY; + + /** + * + * @param x ... + * @param y ... + * @return ... + */ + private boolean isPassable(double x, double y) { + int xCoord = (int) Math.floor(x / lengthX); + int yCoord = (int) Math.floor(y / lengthY); + return getMap()[yCoord][xCoord]; } - public void terminate() { - setTerminated(true); - } + /** + * + * @param x ... + * @param y ... + * @param center ... + * @return + */ + private boolean canHaveAsPartialCircle(double x, double y, double[] center) { - private final boolean[][] map; - - private void setTerminated(boolean terminated) { - this.terminated = terminated; + if (!isPassable(x + center[0], y + center[1])) return false; + x *= -1; + if (!isPassable(x + center[0], y + center[1])) return false; + y *= -1; + if (!isPassable(x + center[0], y + center[1])) return false; + x *= -1; + return isPassable(x + center[0], y + center[1]); } // =================================================================================== // endregion + + + // region objects + //=================================================================================== + + public Collection getAllItems() { + Set all = new HashSet(); + all.add(getWormList()); + all.add(getFoodSet()); + return all; + } + +// public Set getAllTeams() { +// +// Set teams = new HashSet(); +// for(Worm worm: getWormList()) { +// teams.add(worm.getTeam()); +// } +// } + + + + public boolean hasAsWorm(Worm worm) throws NullPointerException { + return getWormList().contains(worm); + } + + public void addWorm(Worm worm) throws IllegalArgumentException, NullPointerException, IllegalStateException { + + if (hasActiveGame()) throw new IllegalStateException(); + if (hasAsWorm(worm)) throw new IllegalArgumentException(); + getWormList().add(worm); + } + + public void removeWorm(Worm worm) throws IllegalArgumentException, NullPointerException { + if (!getWormList().remove(worm)) throw new IllegalArgumentException(); + } + + private List getWormList() { + return this.wormList; + } + + + public boolean hasAsFood(Food food) throws NullPointerException { + return getFoodSet().contains(food); + } + public void addFood(Food food) throws IllegalArgumentException, NullPointerException, IllegalStateException { + + if (hasActiveGame()) throw new IllegalStateException(); + if (!getFoodSet().add(food)) throw new IllegalArgumentException(); + } + + public void removeFood(Food food) throws IllegalArgumentException, NullPointerException { + if (!getFoodSet().remove(food)) throw new IllegalArgumentException(); + } + + private Set getFoodSet() { + return this.foodSet; + } + + private List wormList; + private Set foodSet; + + + // =================================================================================== + // endregion + } diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index f075881..561d2be 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -1,6 +1,7 @@ package worms.model; import be.kuleuven.cs.som.annotate.*; +import jdk.jshell.spi.ExecutionControl; import worms.util.Coordinate; import worms.util.IllegalNameException; @@ -593,7 +594,7 @@ public class Worm { } public Coordinate getFurthestLocationInDirection() { - + return null; } public void collision(Worm basicWorm, Worm... worm) {