refactoring

This commit is contained in:
2018-05-23 17:22:15 +02:00
parent 9e0d87ccbc
commit 21c9aea4fe
7 changed files with 59 additions and 134 deletions

View File

@@ -108,53 +108,12 @@ public class Facade implements IFacade {
@Override
public double getJumpTime(Worm worm, double deltaT) throws ModelException {
try {
return worm.jumpTime();
return worm.jumpTime(deltaT);
} catch(IllegalStateException e){
throw new ModelException(e);
}
}
// /**
// * Makes the given worm jump.
// *
// * @param worm
// * the worm who is going to move
// * @post the worm has jumped
// * @throws ModelException
// * the worm throws an IllegalStateException
// * |catch(IllegalStateException e)
// */
// @Override
// public void jump(Worm worm) throws ModelException {
// try {
// worm.jump();
// }
// catch(IllegalStateException e) {
// throw new ModelException(e);
// }
// }
// /**
// * Returns the total amount of time (in seconds) that a jump of the given worm
// * would take.
// *
// * @param worm
// * the worm who is going to move
// * @throws ModelException
// * the worm throws an IllegalStateException
// * |catch(IllegalStateException e)
// */
// @Override
// public double getJumpTime(Worm worm) throws ModelException {
//
// try {
// return worm.jumpTime();
// }
// catch(IllegalStateException e) {
// throw new ModelException(e);
// }
// }
/**
* Returns the location on the jump trajectory of the given worm after a time t.
*
@@ -192,7 +151,7 @@ public class Facade implements IFacade {
@Override
public void jump(Worm worm, double timeStep) throws ModelException {
try {
worm.jump();
worm.jump(timeStep);
} catch (IllegalStateException e) {
throw new ModelException(e);
}
@@ -358,7 +317,7 @@ public class Facade implements IFacade {
*/
@Override
public double[] getJumpStep(Projectile projectile, double elapsedTime) {
return projectile.getJumpStep(elapsedTime).toArray();
return projectile.jumpStep(elapsedTime).toArray();
}
/**
@@ -371,7 +330,7 @@ public class Facade implements IFacade {
*/
@Override
public double getJumpTime(Projectile projectile, double jumpTimeStep) throws ModelException {
return projectile.getJumpTime(jumpTimeStep);
return projectile.jumpTime(jumpTimeStep);
}
/**
@@ -1032,7 +991,7 @@ public class Facade implements IFacade {
*/
@Override
public Program getWormProgram(Worm worm) throws ModelException {
return null;
return worm.getProgram();
}
/**

View File

@@ -276,4 +276,19 @@ public abstract class GameObject {
return Math.abs(Math.atan(Math.abs(x1 - x2) / Math.abs(y1 - y2)) - Math.PI / 2.0);
}
/**
* Returns the distance between two coordinates.
*
* @param start The start coordinate of the equation.
* @param end The end coordinate of the equation.
* @return the distance between the two given coordinates. The distance is equal to
* the square root of the square of the displacement of the x coordinate plus the
* square of the displacement of the y coordinate.
* |result == sqrt(pow(start.getX()-end.getX())+pow(start.getY()-end.getY()))
*/
public static double getDistance(Coordinate start, Coordinate end) {
return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) +
Math.pow(Math.abs(start.getY() - end.getY()), 2));
}
}

View File

@@ -0,0 +1,12 @@
package worms.model;
import worms.util.Coordinate;
public interface IJumpable {
void jump(double timeStep);
Coordinate jumpStep(double elapsed);
double jumpTime(double timeStep);
}

View File

@@ -6,7 +6,7 @@ import worms.util.Coordinate;
import java.util.List;
public abstract class Projectile extends GameObject {
public abstract class Projectile extends GameObject implements IJumpable {
private static final int rho = 7800;
@@ -71,7 +71,7 @@ public abstract class Projectile extends GameObject {
public static final double FORCE_TIME = 0.5;
public Coordinate getJumpStep(double elapsedTime) {
public Coordinate jumpStep(double elapsedTime) {
if (Double.isNaN(elapsedTime) || elapsedTime < 0)
throw new IllegalArgumentException();
@@ -85,7 +85,7 @@ public abstract class Projectile extends GameObject {
return (getForce() / (getMass() / 1000)) * FORCE_TIME;
}
public double getJumpTime(double timeStep) {
public double jumpTime(double timeStep) {
World world = getWorld();
if (world == null) {
@@ -98,8 +98,7 @@ public abstract class Projectile extends GameObject {
double radius = getRadius();
List<Worm> worms = world.getGameObjectsByClass(Worm.class);
if (!world.isPassable(this.location) ||
worms.stream().anyMatch(w -> w.getDistance(this) < 0)) {
if (!world.isPassable(this.location) || worms.stream().anyMatch(w -> w.getDistance(this) < 0)) {
return 0.0;
}
@@ -116,11 +115,6 @@ public abstract class Projectile extends GameObject {
}
}
public double getDistance(Coordinate start, Coordinate end) {
return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) +
Math.pow(Math.abs(start.getY() - end.getY()), 2));
}
private boolean canJump() {
return getOrientation() < PI;
@@ -131,7 +125,7 @@ public abstract class Projectile extends GameObject {
throw new IllegalStateException();
double v = jumpVelocity();
double t = getJumpTime(jumpTimeStep);
double t = jumpTime(jumpTimeStep);
double a = getOrientation();
Coordinate newLocation = this.location;
@@ -139,7 +133,8 @@ public abstract class Projectile extends GameObject {
List<Worm> worms = getWorld().getGameObjectsByClass(Worm.class);
if (!getWorld().isAdjacent(getLocation(),getRadius())) {
newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a), getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0);
newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a),
getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0);
}
if (!isValidLocation(newLocation)) {
terminate();

View File

@@ -31,8 +31,4 @@ public class Rifle extends Projectile {
super.hitPoints = value;
}
public static double calcRadius() {
return calcRadius(10);
}
}

View File

@@ -51,7 +51,7 @@ public class Team {
public boolean equals(Object obj) {
if (!(obj instanceof Team)) return false;
return this.getName().equals(((Team) obj).getName());
return this.name.equals(((Team) obj).getName());
}
@@ -73,10 +73,9 @@ public class Team {
if (worms == null) throw new IllegalArgumentException();
if (!canHaveAsWorm(worms)) throw new IllegalArgumentException();
Collection<Worm> wormCollection = getAllWormsOfTeam();
Arrays.stream(worms).forEach(w -> {
w.setTeam(this);
wormCollection.add(w);
this.wormCollection.add(w);
});
}
@@ -96,12 +95,11 @@ public class Team {
private boolean canHaveAsWorm(Worm... worm) {
HashSet<String> names = new HashSet<>();
Collection<Worm> worms = getAllWormsOfTeam();
for (Worm w : worm) {
if (w == null || w.getTeam() != null || !names.add(w.getName())) return false;
if (worms.contains(w)) return false;
if (worms.size() == 0 && !w.isTerminated()) continue;
if (this.wormCollection.contains(w)) return false;
if (this.wormCollection.size() == 0 && !w.isTerminated()) continue;
if (w.getMass() < getMinMassTeam() / 2 || w.getMass() > 2 * getMinMassTeam()
|| w.isTerminated()) {
return false;
@@ -126,9 +124,9 @@ public class Team {
public void removeWormsFromTeam(Worm... worm) throws IllegalArgumentException {
if (worm == null || Arrays.stream(worm).anyMatch(w -> Objects.isNull(w) || !getAllWormsOfTeam().contains(w))) throw new IllegalArgumentException();
if (worm == null || Arrays.stream(worm).anyMatch(w -> Objects.isNull(w) || !this.wormCollection.contains(w))) throw new IllegalArgumentException();
for (Worm w: worm) {
getAllWormsOfTeam().remove(w);
this.wormCollection.remove(w);
w.setTeam(null);
}
}
@@ -146,16 +144,7 @@ public class Team {
*/
public boolean containsWorm(Worm worm) throws IllegalArgumentException {
if (worm == null) throw new IllegalArgumentException();
return getAllWormsOfTeam().contains(worm);
}
/**
*
* @return ...
* |wormCollection
*/
public Collection<Worm> getAllWormsOfTeam() {
return wormCollection;
return this.wormCollection.contains(worm);
}
public List<Worm> getAllWormsList() {
@@ -168,7 +157,7 @@ public class Team {
* |getAllWormsOfTeam().size()
*/
public int getNbWorms() {
return getAllWormsOfTeam().size();
return this.wormCollection.size();
}
/**
@@ -186,11 +175,11 @@ public class Team {
public static void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException {
if (receivingTeam == null || supplyingTeam == null || receivingTeam.equals(supplyingTeam) ||
supplyingTeam.getAllWormsOfTeam().stream().anyMatch(s -> receivingTeam.getAllWormsOfTeam().contains(s))) {
supplyingTeam.getAllWormsList().stream().anyMatch(receivingTeam::containsWorm)) {
throw new IllegalArgumentException();
}
Worm[] supWorms = supplyingTeam.getAllWormsOfTeam().toArray(new Worm[0]);
Worm[] supWorms = supplyingTeam.getAllWormsList().toArray(new Worm[0]);
supplyingTeam.removeWormsFromTeam(supWorms);
receivingTeam.addWorm(supWorms);
}
@@ -216,7 +205,7 @@ public class Team {
* |minMass == null
*/
private double getMinMassTeam() throws IllegalStateException {
Worm minMass = getAllWormsOfTeam().stream().min(Comparator.comparingDouble(Worm::getMass)).orElse(null);
Worm minMass = this.wormCollection.stream().min(Comparator.comparingDouble(Worm::getMass)).orElse(null);
if (minMass == null) {
throw new IllegalStateException();
}
@@ -275,7 +264,7 @@ public class Team {
*/
public void terminate() {
removeWormsFromTeam(getAllWormsOfTeam().toArray(new Worm[0]));
removeWormsFromTeam(this.wormCollection.toArray(new Worm[0]));
this.terminated = true;
}

View File

@@ -8,7 +8,6 @@ import worms.util.IllegalNameException;
import static java.lang.Math.*;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
@@ -36,7 +35,7 @@ import java.util.concurrent.ThreadLocalRandom;
* isValidValueHitPoints(getHitPoints())
* @version 3.0
*/
public class Worm extends GameObject {
public class Worm extends GameObject implements IJumpable{
// region constructor
//===================================================================================
@@ -220,17 +219,6 @@ public class Worm extends GameObject {
setMaxActionPoints(this.mass);
}
/**
* Return the minimum radius the worm can have.
* The minimum radius of the worm expresses the minimum length
* of half of the width of the worm.
*/
@Basic
@Raw
public double getMinRadius() {
return this.minRadius;
}
/**
* This variable contains the minimum value of the radius.
*/
@@ -567,21 +555,6 @@ public class Worm extends GameObject {
return maxLocDirection;
}
/**
* Returns the distance between two coordinates.
*
* @param start The start coordinate of the equation.
* @param end The end coordinate of the equation.
* @return the distance between the two given coordinates. The distance is equal to
* the square root of the square of the displacement of the x coordinate plus the
* square of the displacement of the y coordinate.
* |result == sqrt(pow(start.getX()-end.getX())+pow(start.getY()-end.getY()))
*/
public double getDistance(Coordinate start, Coordinate end) {
return Math.sqrt(Math.pow(Math.abs(start.getX() - end.getX()), 2) +
Math.pow(Math.abs(start.getY() - end.getY()), 2));
}
/**
* TODO update documentatie
* The clashing worms their hit points are reduced.
@@ -700,13 +673,13 @@ public class Worm extends GameObject {
* @post Let the worm eat if necessary.
* |checkEat()
*/
public void jump() throws IllegalStateException {
public void jump(double timeStep) throws IllegalStateException {
if (!canJump())
throw new IllegalStateException();
double v = jumpVelocity();
double t = calcJumpTime();
double t = jumpTime(timeStep);
double a = this.orientation;
Coordinate location = this.location;
@@ -726,20 +699,6 @@ public class Worm extends GameObject {
}
/**
* Return the time the worm will jump.
*
* @return The time the worm will jump. The distance divided by the velocity multiplied
* with the cosinus of the orientation.
* |jumpDistance(this.jumpVelocity) / (this.jumpVelocity * Math.cos(this.orientation))
* @throws IllegalStateException Orientation is bigger then pi or action points is equal to 0.
* |getOrientation() >= PI || getActionPoints() == 0
*/
public double jumpTime() {
return calcJumpTime();
}
/**
* Gives the location after a jump.
*
@@ -751,10 +710,10 @@ public class Worm extends GameObject {
* |Coordinate.create(getLocation().getX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
* |getLocation().getY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2))
* @throws IllegalArgumentException() If the deltaTime is not a number or bigger then jumpTime or smaller then 0.
* |if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
* |if (Double.isNaN(deltaTime) || deltaTime < 0)
*/
public Coordinate jumpStep(double deltaTime) {
if (Double.isNaN(deltaTime) || deltaTime > jumpTime() || deltaTime < 0 || this.actionPoints == 0)
if (Double.isNaN(deltaTime) || deltaTime < 0 || !canJump())
throw new IllegalArgumentException();
double velocity = jumpVelocity();
@@ -779,7 +738,7 @@ public class Worm extends GameObject {
* | break
* |result == t
*/
private double calcJumpTime() {
public double jumpTime(double timeStep) {
World world = this.world;
if (world == null) throw new IllegalStateException("World cannot be null");
@@ -793,7 +752,7 @@ public class Worm extends GameObject {
while (true) {
t += 0.05;
t += timeStep;
double x = loc.getX() + v * t * cos(a);
double y = loc.getY() + v * t * sin(a) - (G * t * t) / 2.0;
newLoc = Coordinate.create(x, y);
@@ -826,7 +785,7 @@ public class Worm extends GameObject {
* |result == getActionPoints() > 0 && getOrientation() < PI
*/
private boolean canJump() {
return this.actionPoints > 0 && this.orientation < PI;
return this.actionPoints > 0 && this.orientation <= PI;
}
/**