diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 1b42e35..fb2d366 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -8,25 +8,24 @@ import java.util.stream.Collectors; public class World { /** - * * @param width * @param height * @param map * * @post ... - * new.width = width + * |new.width = width * @post ... - * new.height = height + * |new.height = height * @post ... - * new.map = map + * |new.map = map * @post ... - * new.legthX = width / map[0].length + * |new.legthX = width / map[0].length * @post ... - * new.lengthY = height / map.length + * |new.lengthY = height / map.length * @post ... - * new.game = false + * |new.game = false * @post ... - * new.gameObjets = new HashSet<>() + * |new.gameObjets = new HashSet<>() */ public World(double width, double height, boolean[][] map) { @@ -43,7 +42,7 @@ public class World { } - // region game + // region game (Total) //=================================================================================== public boolean hasActiveGame() { @@ -120,11 +119,10 @@ public class World { // endregion - // region Dimensions + // region Dimensions (Total) //=================================================================================== /** - * * @param dimension * * @return ... @@ -135,7 +133,6 @@ public class World { } /** - * * @return ... * |result == width */ @@ -144,7 +141,6 @@ public class World { } /** - * * @return ... * |result == height */ @@ -152,14 +148,8 @@ public class World { return height; } - /** - * - */ private final double width; - /** - * - */ private final double height; // =================================================================================== @@ -167,11 +157,10 @@ public class World { - // region map + // region map (Total) //=================================================================================== /** - * * @return ... * |result == map */ @@ -180,7 +169,6 @@ public class World { } /** - * * @return ... * result = this.terminated */ @@ -189,6 +177,8 @@ public class World { } /** + * @post ... + * |teams.clear() * @post ... * |this.terminated = true */ @@ -202,32 +192,25 @@ public class World { this.terminated = true; } - /** - * - */ private final boolean[][] map; - /** - * - */ private boolean terminated = false; // =================================================================================== // endregion - // region passable + // region passable (Total) //=================================================================================== /** - * * @param location * * @return ... - * |false if: + * |true if: * | Math.floor(location[0] / lengthX) >= getMap()[0].length || * | location[0] < 0.0 || location[1] / lengthY >= getMap().length || location[1] < 0.0 * |various: - * | result == this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)] + * | result == this.map[map.length - 1 - (int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)] */ public boolean isPassable(double[] location) { @@ -240,7 +223,6 @@ public class World { } /** - * * @param location * * @return ... @@ -256,26 +238,27 @@ public class World { * @param radius * * @return ... - * |result == this.isPassable(location.toArray(), radius) + * |result == this.isPassable(location.toArray(), radius) */ public boolean isPassable(Coordinate location, double radius) { return this.isPassable(location.toArray(), radius); } /** - * * @param center * @param radius * * @return ... - * |for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180) - * | double lenX = center[0] + radius * Math.cos(i) - * | double lenY = center[1] + radius * Math.sin(i) - * | if (i < 1.58 && i > 1.57) - * | lenY -= 0.0000000001 - * | else if (i < 0.79 && i > 0.78 ) - * | lenX -= 0.0000000001 - * |result == isPassable(lenX, lenY) + * |for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180) + * | double lenX = center[0] + radius * Math.cos(i) + * | double lenY = center[1] + radius * Math.sin(i) + * | if (center[0] + radius == lenX) + * | lenX -= 0.0000000001 + * | if (center[1] + radius == lenY) + * | lenY -= 0.0000000001 + * | if (!isPassable(lenX, lenY)) + * | result == false + * |result == true */ public boolean isPassable(double[] center, double radius) { @@ -304,7 +287,6 @@ public class World { } /** - * * @return ... * |result == isAdjacent(Coordinate.create(center), radius) */ @@ -313,7 +295,6 @@ public class World { } /** - * * @param center ... * @param radius ... * @@ -330,7 +311,6 @@ public class World { } /** - * * @return ... * |result == this.lengtX */ @@ -339,26 +319,18 @@ public class World { } /** - * * @return ... * |result == this.lengthY */ public double getLengthY() { return this.lengthY; } - - /** - * - */ + private final double lengthX; - /** - * - */ private final double lengthY; /** - * * @param x * @param y * @@ -373,8 +345,6 @@ public class World { */ private boolean isPassable(double x, double y) { - - int xCoord = (int) Math.floor(x / lengthX); int yCoord = map.length - 1 - (int) Math.floor(y / lengthY); @@ -392,11 +362,10 @@ public class World { // endregion - // region objects + // region objects (Defensive) //=================================================================================== /** - * * @return ... * |result == new ArrayList<>(getGameObjects()) */ @@ -405,9 +374,16 @@ public class World { return new ArrayList<>(getGameObjects()); } - private Set teams = new HashSet<>(); + /** + * @param team + + * @throws IllegalStateException ... + * |team.size() == 10 + * @throws IllegalArgumentException ... + * |! teams.add(team) + */ public void addTeam(Team team) { if (teams.size() == 10) throw new IllegalStateException("Maximum 10 teams"); @@ -416,17 +392,14 @@ public class World { } /** - * * @return ... - * |result == teams + * |result == HashSet<>(teams) */ public Set getAllTeams() { - return new HashSet<>(teams); } /** - * * @return ... * |result == this.gameObjects */ @@ -435,17 +408,30 @@ public class World { } /** - * * @param obj - * - * @post obj.setWorld(this) - * - * @throws IllegalStateException - * ... - * |hasActiveGame() - * @throws IllegalArgumentException - * ... - * |!getGameObjects().add(obj) + * + * @post ... + * |obj.setWorld(this) + * @post ... + * |if (!obj.isValidLocation(obj.getLocation())) + * |obj.setWorld(null) + * @post ... + * |if (Worm.class.isInstance(obj) && (!obj.isValidLocation(obj.getLocation()) + * | || !isAdjacent(obj.getLocation(), obj.getRadius()) )) + * |obj.setWorld(null) + * @post ... + * |getGameObjects().add(obj) + * + * @throws IllegalStateException ... + * |hasActiveGame() + * @throws IllegalArgumentException ... + * |obj == null || getGameObjects().contains(obj) || obj.isTerminated() || + * | obj.getWorld() != null + * @throws IllegalArgumentException ... + * |! obj.isValidLocation(obj.getLocation()) + * @throws IllegalArgumentException ... + * |Worm.class.isInstance(obj) && (!obj.isValidLocation(obj.getLocation()) || + * | !isAdjacent(obj.getLocation(), obj.getRadius())) */ public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException { if (hasActiveGame()) throw new IllegalStateException(); @@ -466,9 +452,11 @@ public class World { } /** - * * @param obj - * + * + * @post ... + * |obj.setWorld(null) + * * @throws IllegalArgumentException * ... * |!getGameObjects().remove(obj) @@ -480,11 +468,10 @@ public class World { } /** - * * @param obj * * @return ... - * |getGameObjects().contains(obj) + * |result == getGameObjects().contains(obj) * * @throws NullPointerException */ @@ -493,10 +480,8 @@ public class World { } /** - * * @return ... - * |lis.add(worm) for each worm - * |result == list (ArrayList<>()) + * |result == getGameObjectsByClass(Worm.class) */ @SuppressWarnings("unchecked") public List getWormList() { @@ -505,10 +490,8 @@ public class World { } /** - * * @return ... - * |list.add(food) for each food - * |result == list (ArrayList<>()) + * |result == getGameObjectsByClass(Food.class) */ @SuppressWarnings("unchecked") public List getFoodList() { @@ -516,14 +499,17 @@ public class World { return getGameObjectsByClass(Food.class); } - + /** + * @param cl + * @param + * + * @return ... + * |result == getGameObjects().stream().filter(cl::isInstance).map(cl::cast).collect(Collectors.toList()) + */ public List getGameObjectsByClass(Class cl) { return getGameObjects().stream().filter(cl::isInstance).map(cl::cast).collect(Collectors.toList()); } - /** - * - */ private Set gameObjects; @@ -531,7 +517,7 @@ public class World { // endregion - // region Wizard + // region Wizard (Defensive) //=================================================================================== public void castSpell() {