This commit is contained in:
2018-05-16 17:49:35 +02:00
parent 2cd0d3af7a
commit 4761ae72c2
6 changed files with 88 additions and 42 deletions

View File

@@ -1,5 +1,6 @@
package worms.facade; package worms.facade;
import be.kuleuven.cs.som.annotate.Model;
import worms.internal.gui.game.IActionHandler; import worms.internal.gui.game.IActionHandler;
import worms.model.*; import worms.model.*;
import worms.model.Food; import worms.model.Food;
@@ -61,7 +62,7 @@ public class Facade implements IFacade {
try { try {
return worm.getFurthestLocationInDirection(direction, maxDistance).toArray(); return worm.getFurthestLocationInDirection(direction, maxDistance).toArray();
} catch(Exception e) { } catch(Exception e) {
throw new ModelException(""); throw new ModelException(e);
} }
} }
@@ -538,7 +539,11 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void addWorm(World world, Worm worm) throws ModelException { 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 @Override
public void removeWorm(World world, Worm worm) throws ModelException { 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 @Override
public void addFood(World world, Food food) throws ModelException { 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 @Override
public void removeWormsFromTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException { public void removeWormsFromTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException {
team.removeWormsFromTeam(worms); try {
team.removeWormsFromTeam(worms);
} catch (IllegalArgumentException e) {
throw new ModelException(e);
}
} }
/** /**

View File

@@ -1,5 +1,7 @@
package worms.model; package worms.model;
import worms.util.Coordinate;
public class Food extends GameObject { public class Food extends GameObject {
/** /**
@@ -37,4 +39,16 @@ public class Food extends GameObject {
} }
private boolean poisonous = false; 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);
}
} }

View File

@@ -42,10 +42,10 @@ public abstract class GameObject {
* |setRadius(radius) * |setRadius(radius)
*/ */
protected GameObject(World world, double[] location, double radius) { protected GameObject(World world, double[] location, double radius) {
setWorld(world);
if (isValidWorld(world)) world.add(this);
setLocation(location); setLocation(location);
setRadius(radius); setRadius(radius);
setWorld(world);
if (isValidWorld(world)) world.add(this);
} }
/** /**
@@ -116,7 +116,9 @@ public abstract class GameObject {
*/ */
public void terminate() { public void terminate() {
this.terminated = true; this.terminated = true;
getWorld().remove(this); if (world != null) {
getWorld().remove(this);
}
} }
/** /**

View File

@@ -52,17 +52,15 @@ public class Team {
* ... * ...
* |worm == null || ! canHaveAsWorm(worm) * |worm == null || ! canHaveAsWorm(worm)
*/ */
public void addWorm(Worm... worm) throws IllegalArgumentException { public void addWorm(Worm... worms) throws IllegalArgumentException {
if (worm == null) throw new IllegalArgumentException(); if (worms == null) throw new IllegalArgumentException();
for (Worm w: worm) { if (!canHaveAsWorm(worms)) throw new IllegalArgumentException();
if (w != null && canHaveAsWorm(w)) {
getAllWormsOfTeam().add(w); Collection<Worm> wormCollection = getAllWormsOfTeam();
w.setTeam(this);
} Arrays.stream(worms).forEach(w -> w.setTeam(this));
else {
throw new IllegalArgumentException(); getAllWormsOfTeam().addAll(Arrays.asList(worms));
}
}
} }
/** /**
@@ -78,21 +76,21 @@ public class Team {
* ... * ...
* |worm == null * |worm == null
*/ */
private boolean canHaveAsWorm(Worm worm) throws IllegalArgumentException { private boolean canHaveAsWorm(Worm... worm) {
if (worm == null) throw new IllegalArgumentException(); HashSet<String> names = new HashSet<>();
Collection<Worm> worms = getAllWormsOfTeam();
Collection<Worm> worms = getAllWormsOfTeam(); for (Worm w : worm) {
if (worms.size() == 0 && !worm.isTerminated()) return true; if (!names.add(w.getName())) return false;
if (worms.contains(w)) return false;
if (worm.getMass() >= getMinMassTeam() / 2 && worm.getMass() <= 2 * getMinMassTeam() if (worms.size() == 0 && !w.isTerminated()) continue;
&& !worm.isTerminated()) { if (w.getMass() < getMinMassTeam() / 2 || w.getMass() > 2 * getMinMassTeam()
for (Worm elWorm : worms) { || w.isTerminated()) {
if (elWorm.getName().equals(worm.getName())) return false; return false;
} }
return true; }
} return true;
return false;
} }
/** /**
@@ -258,6 +256,9 @@ public class Team {
* |new.terminatd = true * |new.terminatd = true
*/ */
public void terminate() { public void terminate() {
removeWormsFromTeam(getAllWormsOfTeam().toArray(new Worm[0]));
this.terminated = true; this.terminated = true;
} }

View File

@@ -434,9 +434,13 @@ public class World {
* ... * ...
* |!getGameObjects().add(obj) * |!getGameObjects().add(obj)
*/ */
public void add(GameObject obj) throws NullPointerException { public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException {
if (hasActiveGame()) throw new IllegalStateException(); 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); obj.setWorld(this);
} }
@@ -452,10 +456,6 @@ public class World {
if (!getGameObjects().remove(obj)) throw new IllegalArgumentException(); if (!getGameObjects().remove(obj)) throw new IllegalArgumentException();
obj.setWorld(null); 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<Food> getFoodList() { public List<Food> getFoodList() {
return getGameObjectsByClass(Food.class); return getGameObjectsByClass(Food.class);
} }
/** /**
@@ -539,6 +538,10 @@ public class World {
int size = gameObjects.size(); int size = gameObjects.size();
int nb1 = new Random().nextInt(size); int nb1 = new Random().nextInt(size);
int nb2 = new Random().nextInt(size); int nb2 = new Random().nextInt(size);
while (nb2 == nb1) {
nb2 = new Random().nextInt(size);
}
GameObject item1 = null; GameObject item1 = null;
GameObject item2 = null; GameObject item2 = null;

View File

@@ -493,9 +493,16 @@ public class Worm extends GameObject {
* |result == (0 <= new.getLocation() <= maxDistance) && (world.isPassable(new.getLocation(), radius)) * |result == (0 <= new.getLocation() <= maxDistance) && (world.isPassable(new.getLocation(), radius))
*/ */
public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) { public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) {
Coordinate currentLocation = getLocation(); Coordinate currentLocation = getLocation();
double radius = getRadius(); double radius = getRadius();
World world = getWorld(); 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)); double step = Math.sqrt(Math.pow(world.getLengthX() * cos(direction), 2) + Math.pow(world.getLengthY() * sin(direction), 2));
if (step > radius) step = radius; if (step > radius) step = radius;
Coordinate nextLoc = currentLocation; Coordinate nextLoc = currentLocation;
@@ -1170,10 +1177,12 @@ public class Worm extends GameObject {
public void checkEat() { public void checkEat() {
World world = getWorld(); World world = getWorld();
List<Food> foodList = world.getFoodList(); if (world != null) {
for (Food food : foodList) { List<Food> foodList = world.getFoodList();
if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) { for (Food food : foodList) {
eat(food); if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) {
eat(food);
}
} }
} }
} }