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;
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);
}
}
/**

View File

@@ -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);
}
}

View File

@@ -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);
}
}
/**

View File

@@ -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<Worm> 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<String> names = new HashSet<>();
Collection<Worm> worms = getAllWormsOfTeam();
Collection<Worm> 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;
}

View File

@@ -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<Food> 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;

View File

@@ -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<Food> foodList = world.getFoodList();
for (Food food : foodList) {
if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) {
eat(food);
if (world != null) {
List<Food> foodList = world.getFoodList();
for (Food food : foodList) {
if (getDistance(food.getLocation(), this.getLocation()) < this.getRadius() + food.getRadius()) {
eat(food);
}
}
}
}