From 4761ae72c2051a2fe7ad11404f278d218388c3eb Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Wed, 16 May 2018 17:49:35 +0200 Subject: [PATCH] update --- OGP1718-Worms/src/worms/facade/Facade.java | 27 +++++++++-- OGP1718-Worms/src/worms/model/Food.java | 14 ++++++ OGP1718-Worms/src/worms/model/GameObject.java | 8 ++-- OGP1718-Worms/src/worms/model/Team.java | 47 ++++++++++--------- OGP1718-Worms/src/worms/model/World.java | 17 ++++--- OGP1718-Worms/src/worms/model/Worm.java | 17 +++++-- 6 files changed, 88 insertions(+), 42 deletions(-) diff --git a/OGP1718-Worms/src/worms/facade/Facade.java b/OGP1718-Worms/src/worms/facade/Facade.java index c9ffb6e..b599179 100644 --- a/OGP1718-Worms/src/worms/facade/Facade.java +++ b/OGP1718-Worms/src/worms/facade/Facade.java @@ -1,5 +1,6 @@ package worms.facade; +import be.kuleuven.cs.som.annotate.Model; import worms.internal.gui.game.IActionHandler; import worms.model.*; import worms.model.Food; @@ -61,7 +62,7 @@ public class Facade implements IFacade { try { return worm.getFurthestLocationInDirection(direction, maxDistance).toArray(); } catch(Exception e) { - throw new ModelException(""); + throw new ModelException(e); } } @@ -538,7 +539,11 @@ public class Facade implements IFacade { */ @Override public void addWorm(World world, Worm worm) throws ModelException { - world.add(worm); + try { + world.add(worm); + } catch(IllegalStateException | IllegalArgumentException e) { + throw new ModelException(e); + } } /** @@ -549,7 +554,11 @@ public class Facade implements IFacade { */ @Override public void removeWorm(World world, Worm worm) throws ModelException { - world.remove(worm); + try { + world.remove(worm); + } catch (IllegalArgumentException e) { + throw new ModelException(e); + } } /** @@ -582,7 +591,11 @@ public class Facade implements IFacade { */ @Override public void addFood(World world, Food food) throws ModelException { - world.add(food); + try { + world.add(food); + } catch(IllegalArgumentException | IllegalStateException e) { + throw new ModelException(e); + } } /** @@ -985,7 +998,11 @@ public class Facade implements IFacade { */ @Override public void removeWormsFromTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException { - team.removeWormsFromTeam(worms); + try { + team.removeWormsFromTeam(worms); + } catch (IllegalArgumentException e) { + throw new ModelException(e); + } } /** diff --git a/OGP1718-Worms/src/worms/model/Food.java b/OGP1718-Worms/src/worms/model/Food.java index 8fb03bb..52f8774 100644 --- a/OGP1718-Worms/src/worms/model/Food.java +++ b/OGP1718-Worms/src/worms/model/Food.java @@ -1,5 +1,7 @@ package worms.model; +import worms.util.Coordinate; + public class Food extends GameObject { /** @@ -37,4 +39,16 @@ public class Food extends GameObject { } private boolean poisonous = false; + + + @Override + protected boolean isValidLocation(Coordinate location) { + if (getWorld() != null) return super.isValidLocation(location) && getWorld().isAdjacent(location, getRadius()); + return super.isValidLocation(location); + } + + protected boolean isValidLocation(Coordinate location, World world) { + if (world != null) return super.isValidLocation(location) && world.isAdjacent(location, getRadius()); + return super.isValidLocation(location); + } } diff --git a/OGP1718-Worms/src/worms/model/GameObject.java b/OGP1718-Worms/src/worms/model/GameObject.java index 267da9e..f7a8121 100644 --- a/OGP1718-Worms/src/worms/model/GameObject.java +++ b/OGP1718-Worms/src/worms/model/GameObject.java @@ -42,10 +42,10 @@ public abstract class GameObject { * |setRadius(radius) */ protected GameObject(World world, double[] location, double radius) { - setWorld(world); - if (isValidWorld(world)) world.add(this); setLocation(location); setRadius(radius); + setWorld(world); + if (isValidWorld(world)) world.add(this); } /** @@ -116,7 +116,9 @@ public abstract class GameObject { */ public void terminate() { this.terminated = true; - getWorld().remove(this); + if (world != null) { + getWorld().remove(this); + } } /** diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index ec4e2ed..11012d2 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -52,17 +52,15 @@ public class Team { * ... * |worm == null || ! canHaveAsWorm(worm) */ - public void addWorm(Worm... worm) throws IllegalArgumentException { - if (worm == null) throw new IllegalArgumentException(); - for (Worm w: worm) { - if (w != null && canHaveAsWorm(w)) { - getAllWormsOfTeam().add(w); - w.setTeam(this); - } - else { - throw new IllegalArgumentException(); - } - } + public void addWorm(Worm... worms) throws IllegalArgumentException { + if (worms == null) throw new IllegalArgumentException(); + if (!canHaveAsWorm(worms)) throw new IllegalArgumentException(); + + Collection wormCollection = getAllWormsOfTeam(); + + Arrays.stream(worms).forEach(w -> w.setTeam(this)); + + getAllWormsOfTeam().addAll(Arrays.asList(worms)); } /** @@ -78,21 +76,21 @@ public class Team { * ... * |worm == null */ - private boolean canHaveAsWorm(Worm worm) throws IllegalArgumentException { + private boolean canHaveAsWorm(Worm... worm) { - if (worm == null) throw new IllegalArgumentException(); + HashSet names = new HashSet<>(); + Collection worms = getAllWormsOfTeam(); - Collection worms = getAllWormsOfTeam(); - if (worms.size() == 0 && !worm.isTerminated()) return true; - - if (worm.getMass() >= getMinMassTeam() / 2 && worm.getMass() <= 2 * getMinMassTeam() - && !worm.isTerminated()) { - for (Worm elWorm : worms) { - if (elWorm.getName().equals(worm.getName())) return false; + for (Worm w : worm) { + if (!names.add(w.getName())) return false; + if (worms.contains(w)) return false; + if (worms.size() == 0 && !w.isTerminated()) continue; + if (w.getMass() < getMinMassTeam() / 2 || w.getMass() > 2 * getMinMassTeam() + || w.isTerminated()) { + return false; } - return true; - } - return false; + } + return true; } /** @@ -258,6 +256,9 @@ public class Team { * |new.terminatd = true */ public void terminate() { + + removeWormsFromTeam(getAllWormsOfTeam().toArray(new Worm[0])); + this.terminated = true; } diff --git a/OGP1718-Worms/src/worms/model/World.java b/OGP1718-Worms/src/worms/model/World.java index 89b71a5..08ae145 100644 --- a/OGP1718-Worms/src/worms/model/World.java +++ b/OGP1718-Worms/src/worms/model/World.java @@ -434,9 +434,13 @@ public class World { * ... * |!getGameObjects().add(obj) */ - public void add(GameObject obj) throws NullPointerException { + public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException { if (hasActiveGame()) throw new IllegalStateException(); - if (!getGameObjects().add(obj)) throw new IllegalArgumentException(); + if (obj == null || !getGameObjects().add(obj) || obj.isTerminated()) throw new IllegalArgumentException(); + + if (obj.getClass().equals(Food.class) && !((Food) obj).isValidLocation(obj.getLocation(), this)) { + throw new IllegalArgumentException(); + } obj.setWorld(this); } @@ -452,10 +456,6 @@ public class World { if (!getGameObjects().remove(obj)) throw new IllegalArgumentException(); obj.setWorld(null); - - if (obj.getClass().equals(Worm.class)) { - //System.out.println("remove " + ((Worm) obj).getName()); - } } /** @@ -493,7 +493,6 @@ public class World { public List getFoodList() { return getGameObjectsByClass(Food.class); - } /** @@ -539,6 +538,10 @@ public class World { int size = gameObjects.size(); int nb1 = new Random().nextInt(size); int nb2 = new Random().nextInt(size); + while (nb2 == nb1) { + nb2 = new Random().nextInt(size); + } + GameObject item1 = null; GameObject item2 = null; diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 4fdbb45..26b3c24 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -493,9 +493,16 @@ public class Worm extends GameObject { * |result == (0 <= new.getLocation() <= maxDistance) && (world.isPassable(new.getLocation(), radius)) */ public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) { + Coordinate currentLocation = getLocation(); double radius = getRadius(); World world = getWorld(); + + if (getWorld() == null) { + return Coordinate.create(currentLocation.getX() + maxDistance * cos(direction), currentLocation.getY() + maxDistance * sin(direction)); + } + + double step = Math.sqrt(Math.pow(world.getLengthX() * cos(direction), 2) + Math.pow(world.getLengthY() * sin(direction), 2)); if (step > radius) step = radius; Coordinate nextLoc = currentLocation; @@ -1170,10 +1177,12 @@ public class Worm extends GameObject { public void checkEat() { World world = getWorld(); - List foodList = world.getFoodList(); - for (Food food : foodList) { - if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) { - eat(food); + if (world != null) { + List foodList = world.getFoodList(); + for (Food food : foodList) { + if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) { + eat(food); + } } } }