From 68aae2be6ef41a7a9e5a8c2be9283c18b75ef944 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Fri, 13 Apr 2018 21:00:41 +0200 Subject: [PATCH] mass update --- OGP1718-Worms/src/worms/facade/Facade.java | 94 +++++------------- OGP1718-Worms/src/worms/model/Food.java | 7 +- OGP1718-Worms/src/worms/model/GameObject.java | 27 ++++- OGP1718-Worms/src/worms/model/World.java | 89 ++++++++++------- OGP1718-Worms/src/worms/model/Worm.java | 99 ++++++++++--------- 5 files changed, 156 insertions(+), 160 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index bf76a2a..76980c7 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -1,73 +1,18 @@ package worms.facade; -import worms.model.Food; -import worms.model.Team; -import worms.model.World; -import worms.model.Worm; +import worms.model.*; import worms.util.IllegalNameException; import worms.util.ModelException; import worms.util.Coordinate; import worms.util.MustNotImplementException; import java.math.BigInteger; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; public class Facade implements IFacade { -// /** -// * Create and return a new worm that is positioned at the given location, looks -// * in the given direction, has the given radius and the given name. -// * -// * @param location -// * An array containing the x-coordinate of the position of the new -// * worm followed by the y-coordinate of the position of the new worm -// * (in meter) -// * @param direction -// * The direction of the new worm (in radians) -// * @param radius -// * The radius of the new worm (in meter) -// * @param name -// * the name of the new worm -// * @post the new worm has the given location, direction, name and radius -// * |new Worm(Coordinate.create(location), direction, name, radius) -// * @throws ModelException -// * the worm throws an IllegalArgumentException -// * |catch(IllegalArgumentException e) -// */ -// @Override -// public Worm createWorm(double[] location, double direction, double radius, String name) throws ModelException { -// -// try { -// return new Worm(Coordinate.create(location), direction, name, radius); -// } -// catch(IllegalArgumentException e) { -// throw new ModelException(e); -// } -// } - -// /** -// * Moves the given worm by the given number of steps. -// * -// * @param worm -// * the worm who is going to move -// * @param nbSteps -// * the number of steps the worm is going to take -// * @post the worm has taken the given number of steps -// * |worm.move(nbSteps) -// * @throws ModelException -// * the worm throws an IllegalArgumentException -// * |catch(IllegalArgumentException e) -// */ -// @Override -// public void move(Worm worm, int nbSteps) throws ModelException { -// try { -// worm.move(nbSteps); -// } -// catch(IllegalArgumentException e) { -// throw new ModelException(e); -// } -// } /** * Turns the given worm by the given angle. @@ -82,7 +27,12 @@ public class Facade implements IFacade { @Override public void turn(Worm worm, double angle) throws ModelException { - worm.turn(angle); + try { + worm.turn(angle); + } + catch (AssertionError e) { + throw new ModelException(""); + } } /** @@ -106,7 +56,7 @@ public class Facade implements IFacade { try { return worm.getFurthestLocationInDirection(direction, maxDistance).toArray(); } catch(Exception e) { - throw new ModelException(e); + throw new ModelException(""); } } @@ -117,7 +67,12 @@ public class Facade implements IFacade { */ @Override public void move(Worm worm) throws ModelException { - worm.move(); + + try { + worm.move(); + } catch(IllegalArgumentException e) { + throw new ModelException(""); + } } /** @@ -456,7 +411,7 @@ public class Facade implements IFacade { */ @Override public boolean hasAsWorm(World world, Worm worm) throws ModelException { - return world.hasAsWorm(worm); + return world.hasAsGameObject(worm); } /** @@ -467,7 +422,7 @@ public class Facade implements IFacade { */ @Override public void addWorm(World world, Worm worm) throws ModelException { - world.addWorm(worm); + world.add(worm); } /** @@ -478,7 +433,7 @@ public class Facade implements IFacade { */ @Override public void removeWorm(World world, Worm worm) throws ModelException { - world.removeWorm(worm); + world.remove(worm); } /** @@ -488,6 +443,7 @@ public class Facade implements IFacade { */ @Override public List getAllWorms(World world) throws ModelException { + return world.getWormList(); } @@ -499,7 +455,7 @@ public class Facade implements IFacade { */ @Override public boolean hasAsFood(World world, Food food) throws ModelException { - return world.hasAsFood(food); + return world.hasAsGameObject(food); } /** @@ -510,7 +466,7 @@ public class Facade implements IFacade { */ @Override public void addFood(World world, Food food) throws ModelException { - world.addFood(food); + world.add(food); } /** @@ -521,7 +477,7 @@ public class Facade implements IFacade { */ @Override public void removeFood(World world, Food food) throws ModelException { - world.removeFood(food); + world.remove(food); } /** @@ -832,7 +788,11 @@ public class Facade implements IFacade { */ @Override public Team createTeam(World world, String name) throws ModelException, MustNotImplementException { - return new Team(name); + try { + return new Team(name); + } catch(IllegalNameException e) { + throw new ModelException(e.getMessage()); + } } /** diff --git a/OGP1718-Worms/src/worms/model/Food.java b/OGP1718-Worms/src/worms/model/Food.java index d1b5981..8b18b42 100644 --- a/OGP1718-Worms/src/worms/model/Food.java +++ b/OGP1718-Worms/src/worms/model/Food.java @@ -1,14 +1,13 @@ package worms.model; +import worms.util.Coordinate; + public class Food extends GameObject { public Food(World world, double[] location) { - setWorld(world); - setLocation(location); + super(world, location, 0.2); - setRadius(0.2); final double rho = 150; setMass(getRadius(), rho); - world.addFood(this); } } diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index fc115ec..bc9e7bf 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -1,5 +1,6 @@ package worms.model; +import be.kuleuven.cs.som.annotate.Raw; import worms.util.Coordinate; import static java.lang.Math.PI; @@ -8,6 +9,12 @@ import static java.lang.Math.round; public abstract class GameObject { + GameObject(World world, double[] location, double radius) { + setWorld(world); + world.add(this); + setLocation(location); + setRadius(radius); + } /** * @@ -35,8 +42,6 @@ public abstract class GameObject { private World world; - - /** * * @return ... @@ -46,10 +51,11 @@ public abstract class GameObject { } /** - * Terminate the worm + * Terminate the object */ public void terminate() { this.terminated = true; + getWorld().remove(this); } private boolean terminated = false; @@ -112,9 +118,24 @@ public abstract class GameObject { return this.radius; } void setRadius(double radius) { + if (!canHaveAsRadius(radius)) throw new IllegalArgumentException(); this.radius = radius; } + /** + * Check whether the given radius is a valid radius for the worm + * + * @param radius + * the radius to check + * @return True if and only if the radius is bigger then the minimum radius + * (or equal) and the radius is a number + * |result == (radius >= getMinRadius() && !Double.isNaN(radius)) + */ + @Raw + private boolean canHaveAsRadius(double radius) { + return !Double.isNaN(radius) && radius > 0; + } + boolean isValidLocation(Coordinate location) { double radius = getRadius(); diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 5294e62..43e945b 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -3,6 +3,7 @@ package worms.model; import worms.util.Coordinate; import java.util.*; +import java.util.stream.Collectors; public class World { @@ -13,12 +14,11 @@ public class World { this.width = width; this.height = height; this.map = map; - this.wormList = new ArrayList<>(); - this.foodSet = new HashSet<>(); this.lengthX = width / map[0].length; this.lengthY = height / map.length; this.game = false; + this.gameObjects = new HashSet<>(); } @@ -38,7 +38,11 @@ public class World { this.game = false; } - public void activateNextWorm() { + public void activateNextWorm() throws IllegalStateException { + + if (getWormList().size() == 0) { + throw new IllegalStateException("No worms"); + } this.activeWorm++; if (this.activeWorm == getWormList().size()) { @@ -52,6 +56,7 @@ public class World { } public String getWinner() { + if (getWormList().size() == 1) { return getWormList().get(0).getName(); } else if (getWormList().size() > 1) { @@ -139,6 +144,10 @@ public class World { return this.isPassable(location.toArray()); } + public boolean isPassable(Coordinate location, double radius) { + return this.isPassable(location.toArray(), radius); + } + public boolean isPassable(double[] center, double radius) { for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180) { @@ -158,13 +167,22 @@ public class World { } return true; } + + /** + * + * @return ... + */ + public boolean isAdjacent(double[] center, double radius) { + return isAdjacent(Coordinate.create(center), radius); + } + /** * * @param center ... * @param radius ... * @return ... */ - public boolean isAdjacent(double[] center, double radius) { + public boolean isAdjacent(Coordinate center, double radius) { double maxDistance = radius * 0.1; double length = lengthX; @@ -213,10 +231,8 @@ public class World { //=================================================================================== public Collection getAllItems() { - Set all = new HashSet(); - all.add(getWormList()); - all.add(getFoodSet()); - return all; + + return new ArrayList<>(getGameObjects()); } public Set getAllTeams() { @@ -228,47 +244,46 @@ public class World { return teams; } - - - public boolean hasAsWorm(Worm worm) throws NullPointerException { - return getWormList().contains(worm); + public Set getGameObjects() { + return this.gameObjects; } - public void addWorm(Worm worm) throws IllegalArgumentException, NullPointerException, IllegalStateException { + public void add(GameObject obj) throws NullPointerException { + if (hasActiveGame()) throw new IllegalStateException(); + if (!getGameObjects().add(obj)) throw new IllegalArgumentException(); + obj.setWorld(this); + } + public void remove(GameObject obj) throws IllegalArgumentException { - if (hasActiveGame() || hasAsWorm(worm)) throw new IllegalStateException(); - getWormList().add(worm); - worm.setWorld(this); + if (!getGameObjects().remove(obj)) throw new IllegalArgumentException(); } - public void removeWorm(Worm worm) throws IllegalArgumentException, NullPointerException { - if (!getWormList().remove(worm)) throw new IllegalArgumentException(); + public boolean hasAsGameObject(GameObject obj) throws NullPointerException { + return getGameObjects().contains(obj); } public List getWormList() { - return this.wormList; + + List list = new ArrayList<>(); + for (GameObject x : getGameObjects()) { + if (x.getClass().equals(Worm.class)) { + list.add((Worm) x); + } + } + return list; } - - 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 List getGameObjectList(Class cl) { + List list = new ArrayList<>(); + for (GameObject x : getGameObjects()) { + if (x.getClass().equals(cl)) { + list.add(x); + } + } + return list; } - 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; + private Set gameObjects; // =================================================================================== diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 704ab6d..144ef63 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -7,6 +7,7 @@ import worms.util.IllegalNameException; import static java.lang.Math.*; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; /** * A class with the specifications of the worm @@ -40,14 +41,16 @@ public class Worm extends GameObject { /** *initialize the new worm with given location, orientation, name and radius * + * @param world + * The world this worm belongs to * @param location * the location for the new worm - * @param orientation - * the orientation for the new worm * @param name * the name for the new worm * @param radius * the radius for the new worm + * @param team + * The team of the new worm * @post the new location of the worm is equal to the given location * |new.getLocation() == location * @post the new orientation of the worm is equal to the given orientation @@ -58,9 +61,10 @@ public class Worm extends GameObject { * |new.getRadius() == radius */ @Raw - public Worm(Coordinate location, double orientation, String name, double radius) { + public Worm(World world, double[] location, double direction, double radius, String name, Team team) { + + this(world, location, direction, name, radius, 0.25, team); - this(location, orientation, name, radius, 0.25); } /** @@ -101,12 +105,10 @@ public class Worm extends GameObject { * |isValidName(name) */ @Raw - public Worm(Coordinate location, double orientation, String name, double radius, double minRadius) + public Worm(World world, double[] location, double orientation, String name, double radius, double minRadius, Team team) throws IllegalArgumentException { - - if (!isValidLocation(location)) - throw new IllegalArgumentException("Illegal value for location"); - setLocation(location); + super(world, location, radius); + setTeam(team); setOrientation(orientation); @@ -114,9 +116,7 @@ public class Worm extends GameObject { throw new IllegalArgumentException("Invalid min radius"); this.minRadius = minRadius; - if (!canHaveAsRadius(radius)) - throw new IllegalArgumentException("Invalid radius"); - setRadius(radius); + if (this.radius < this.minRadius) throw new IllegalArgumentException("Invalid radius"); setActionPoints(getMaxActionPoints()); @@ -124,19 +124,11 @@ public class Worm extends GameObject { if (validName != -1) throw new IllegalNameException(validName, name); this.name = name; - - long startHitPoints = 1000 + (new Random().nextLong() * (2000 - 1000)); + + long startHitPoints = ThreadLocalRandom.current().nextLong(1001, 2000); setHitPoints(startHitPoints); } - public Worm(World world, double[] location, double direction, double radius, String name, Team team) { - - this(Coordinate.create(location), direction, name, radius, 0.25); - setWorld(world); - setTeam(team); - world.addWorm(this); - } - //=================================================================================== // endregion @@ -219,7 +211,7 @@ public class Worm extends GameObject { * the given radius is not a valid radius for any worm * |! canHaveAsMinRadius(radius) */ - @Raw + @Raw @Override public void setRadius(double radius) throws IllegalArgumentException { if (!canHaveAsRadius(radius)) throw new IllegalArgumentException("Invalid radius"); @@ -247,7 +239,7 @@ public class Worm extends GameObject { /** * Check whether the given radius is a valid radius for the worm - * + * * @param radius * the radius to check * @return True if and only if the radius is bigger then the minimum radius @@ -469,9 +461,6 @@ public class Worm extends GameObject { /** * move the worm for the given number of steps * - * @param numberSteps - * the number of steps the worm should take - * * @post the x-coordinate of the new location of the worm should be the location of * the old x-coordinate plus the number of steps multiplied with the distance * that the x-coordinate should move (distance is equal to the radius multiplied @@ -495,7 +484,6 @@ public class Worm extends GameObject { */ public void move() throws IllegalArgumentException { - double newDirection = getFurthestLocationDirection(); Coordinate newLocation = getFurthestLocationInDirection(newDirection, this.getRadius()); double distance = getDistance(this.getLocation(), newLocation); @@ -628,7 +616,7 @@ public class Worm extends GameObject { double currentAngle = getOrientation(); return 0 <= angle + currentAngle && angle + currentAngle < (2 * PI) && !Double.isNaN(angle) && - getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0; + getActionPoints() - (long) Math.abs(ceil(toDegrees(angle) / 6)) >= 0; } //=================================================================================== @@ -668,6 +656,10 @@ public class Worm extends GameObject { setLocation(Coordinate.create( getLocation().getX() + jumpDistance(this.jumpVelocity()), getLocation().getY())); setActionPoints(0); + + if (!getWorld().isAdjacent(getLocation(), getRadius())) { + fall(); + } } /** @@ -800,7 +792,7 @@ public class Worm extends GameObject { */ @Raw private void setHitPoints(long hitPoints) { - if (hitPoints <= 0) + if (hitPoints < 0) terminate(); this.hitPoints = hitPoints; } @@ -833,26 +825,35 @@ public class Worm extends GameObject { //=================================================================================== public void fall() { - double[] center = {getLocation().getX(), getLocation().getY()}; - Coordinate oldLocation = getLocation(); - double endY = getLocation().getY(); - if (canFall()) { - for (double y = oldLocation.getY(); y <= (getWorld().getHeight() + 1); y--) { - if (y >= (getWorld().getHeight() + 1)) { - terminate(); - } - double[] newLoc = {oldLocation.getX(), y}; - setLocation(newLoc); - if (! canFall()) { - endY = y; - break; - } - center[1] = y; - } - } + double heigth = getWorld().getHeight() - getRadius(); + Coordinate oldLocation = getLocation(); + if (canFall()) { + for (double y = oldLocation.getY(); y <= heigth; y = y - 0.01) { + Coordinate newLoc = Coordinate.create(oldLocation.getX(), y); - long cost = 3 * (long) Math.floor(oldLocation.getY() - endY); - decreaseHitPoints(cost); + if (y - radius < 0) { + terminate(); + break; + } + + if (!getWorld().isAdjacent(newLoc, radius)) { + setLocation(newLoc); + } else { + break; + } +// for (double i = y; i >= y - 1; i = i - 0.1) { +// double[] newLocation = {oldLocation.getX(), i}; +// if (! getWorld().isPassable(newLocation, getRadius())) { +// setLocation(newLocation); +// endY = i; +// break; +// } +// } + } + } + + long cost = 3 * (long) Math.floor(oldLocation.getY() - getLocation().getY()); + decreaseHitPoints(cost); } public boolean canFall() {