small fixes

This commit is contained in:
2018-05-22 17:48:33 +02:00
parent 9f2e91836c
commit 36bf17b689
6 changed files with 54 additions and 35 deletions

View File

@@ -413,7 +413,7 @@ public class Facade implements IFacade {
public Projectile fire(Worm worm) throws ModelException { public Projectile fire(Worm worm) throws ModelException {
try { try {
return worm.fire(); return worm.fire();
} catch (IllegalStateException e) { } catch (IllegalStateException | IllegalArgumentException e) {
throw new ModelException(e); throw new ModelException(e);
} }
} }

View File

@@ -46,9 +46,4 @@ public class Food extends GameObject {
if (getWorld() != null) return super.isValidLocation(location) && getWorld().isAdjacent(location, getRadius()); if (getWorld() != null) return super.isValidLocation(location) && getWorld().isAdjacent(location, getRadius());
return super.isValidLocation(location); 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,11 +42,7 @@ public abstract class GameObject {
* |setRadius(radius) * |setRadius(radius)
*/ */
protected GameObject(World world, double[] location, double radius) { protected GameObject(World world, double[] location, double radius) {
if (!isValidLocation(Coordinate.create(location))) throw new IllegalArgumentException("Illegal location"); this(world, Coordinate.create(location), radius);
setLocation(location);
setRadius(radius);
if (isValidWorld(world)) world.add(this);
setWorld(world);
} }
@@ -64,10 +60,11 @@ public abstract class GameObject {
* |setRadius(radius) * |setRadius(radius)
*/ */
protected GameObject(World world, Coordinate location, double radius) { protected GameObject(World world, Coordinate location, double radius) {
world.add(this); if (!isValidLocation(location)) throw new IllegalArgumentException("Illegal location");
setWorld(world);
setLocation(location); setLocation(location);
setRadius(radius); setRadius(radius);
if (isValidWorld(world)) world.add(this);
setWorld(world);
} }
/** /**

View File

@@ -62,8 +62,7 @@ public abstract class Projectile extends GameObject {
!(location.getX() - radius < 0) && !(location.getX() - radius < 0) &&
!(location.getX() + radius > getWorld().getWidth()) && !(location.getX() + radius > getWorld().getWidth()) &&
!(location.getY() + radius > getWorld().getHeight()) && !(location.getY() + radius > getWorld().getHeight()) &&
!(location.getY() - radius < 0 && !(location.getY() - radius < 0);
!getWorld().isAdjacent(location, radius));
} }
public static final double G = 5.0; public static final double G = 5.0;

View File

@@ -447,11 +447,11 @@ public class World {
*/ */
public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException { public void add(GameObject obj) throws IllegalStateException, IllegalArgumentException {
if (hasActiveGame()) throw new IllegalStateException(); if (hasActiveGame()) throw new IllegalStateException();
if (obj == null || !getGameObjects().add(obj) || obj.isTerminated() || obj.getWorld() != null) throw new IllegalArgumentException(); if (obj == null || getGameObjects().contains(obj) || obj.isTerminated() || obj.getWorld() != null) throw new IllegalArgumentException();
obj.setWorld(this); obj.setWorld(this);
if (obj.getClass().equals(Food.class) && !((Food) obj).isValidLocation(obj.getLocation(), this)) { if (!obj.isValidLocation(obj.getLocation())) {
obj.setWorld(null); obj.setWorld(null);
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
@@ -460,6 +460,7 @@ public class World {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
obj.setWorld(this); obj.setWorld(this);
getGameObjects().add(obj);
} }
/** /**

View File

@@ -522,8 +522,8 @@ public class Worm extends GameObject {
if (step > radius) step = radius; if (step > radius) step = radius;
Coordinate nextLoc = currentLocation; Coordinate nextLoc = currentLocation;
while (getDistance(currentLocation, Coordinate.create(nextLoc.getX() + step * cos(direction), while (Math.round(getDistance(currentLocation, Coordinate.create(nextLoc.getX() + step * cos(direction),
nextLoc.getY() + step * sin(direction))) <= maxDistance) { nextLoc.getY() + step * sin(direction))) * 100.0) / 100.0 <= maxDistance) {
nextLoc = Coordinate.create(nextLoc.getX() + step * cos(direction), nextLoc = Coordinate.create(nextLoc.getX() + step * cos(direction),
nextLoc.getY() + step * sin(direction)); nextLoc.getY() + step * sin(direction));
@@ -531,8 +531,8 @@ public class Worm extends GameObject {
double max = radius; double max = radius;
while (!world.isAdjacent(nextLoc.toArray(), radius)) { while (!world.isAdjacent(nextLoc.toArray(), radius)) {
max -= 0.01; max -= 0.01;
nextLoc = Coordinate.create(nextLoc.getX() - 0.01 * cos(direction), nextLoc = Coordinate.create(nextLoc.getX() - step / 100.0 * cos(direction),
nextLoc.getY() - 0.01 * sin(direction)); nextLoc.getY() - step / 100.0 * sin(direction));
if (max < 0) { if (max < 0) {
return currentLocation; return currentLocation;
} }
@@ -720,8 +720,13 @@ public class Worm extends GameObject {
terminate(); terminate();
return; return;
} }
if (!newLocation.equals(getLocation())) {
setLocation(newLocation); setLocation(newLocation);
fight();
}
setActionPoints(0); setActionPoints(0);
} }
/** /**
@@ -798,14 +803,12 @@ public class Worm extends GameObject {
if (!world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) { if (!world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) {
while (true) { while (true) {
t -= 0.01; t -= 0.01;
newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0); newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0);
if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() || if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() ||
newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) { newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) {
if (getDistance(getLocation(), newLoc) < getRadius()) throw new IllegalStateException(); if (getDistance(getLocation(), newLoc) < getRadius()) throw new IllegalStateException();
break; break;
} }
} }
@@ -903,25 +906,49 @@ public class Worm extends GameObject {
* with the rate of the radius of the attacker and the radius of the attacked worm. * with the rate of the radius of the attacker and the radius of the attacked worm.
* |new.attackedWorm.getHitPoints() == old.attackedWorm.getHitPoints() - 10 * ((attacker.getRadius())/(attackedWorm.getRadius())) * |new.attackedWorm.getHitPoints() == old.attackedWorm.getHitPoints() - 10 * ((attacker.getRadius())/(attackedWorm.getRadius()))
*/ */
public void fight(Coordinate newLocation, Coordinate oldLocation, Worm worm1, Worm... worm2) { public void fight() {
if (!worm1.isTerminated() && newLocation != oldLocation) {
for (Worm wormB : worm2) { World world = getWorld();
for (Worm w : world.getGameObjectsByClass(Worm.class)) {
if (w.equals(this)) continue;
if (w.getDistance(this) < 0) {
Worm attackedWorm; Worm attackedWorm;
Worm attacker; Worm attacker;
Random rand = new Random(); int coin = ThreadLocalRandom.current().nextInt(2);
int coin = rand.nextInt(2);
if (coin == 1) { if (coin == 1) {
attackedWorm = worm1; attackedWorm = w;
attacker = wormB; attacker = this;
} else { } else {
attackedWorm = wormB; attackedWorm = this;
attacker = worm1; attacker = w;
} }
double N = 10 * ceil((attacker.getRadius()) / (attackedWorm.getRadius())); long N = (long) ceil(10.0 * (attacker.getRadius() / attackedWorm.getRadius()));
long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; long loseAttackedWorm = ThreadLocalRandom.current().nextLong(1, 10 * N);
System.out.println(loseAttackedWorm);
System.out.println(attackedWorm.getName());
attackedWorm.decreaseHitPoints(loseAttackedWorm); attackedWorm.decreaseHitPoints(loseAttackedWorm);
} }
} }
// if (!worm1.isTerminated() && newLocation != oldLocation) {
// for (Worm wormB : worm2) {
// Worm attackedWorm;
// Worm attacker;
// Random rand = new Random();
// int coin = rand.nextInt(2);
// if (coin == 1) {
// attackedWorm = worm1;
// attacker = wormB;
// } else {
// attackedWorm = wormB;
// attacker = worm1;
// }
// double N = 10 * ceil((attacker.getRadius()) / (attackedWorm.getRadius()));
// long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1;
// attackedWorm.decreaseHitPoints(loseAttackedWorm);
// }
// }
} }