overall improvements

This commit is contained in:
2018-03-07 22:06:59 +01:00
parent d51de2eea5
commit 3c8da5825f

View File

@@ -4,6 +4,8 @@ import be.kuleuven.cs.som.annotate.*;
import worms.util.Tuple; import worms.util.Tuple;
import worms.util.IllegalNameException; import worms.util.IllegalNameException;
import static java.lang.Math.*;
public class Worm { public class Worm {
/** /**
* a class with the specifications of the worm * a class with the specifications of the worm
@@ -94,14 +96,18 @@ public class Worm {
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minimumRadius) public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minimumRadius)
throws IllegalArgumentException { throws IllegalArgumentException {
setLocation(location); if (!isValidLocation(location))
throw new IllegalArgumentException("Illegal value for location");
this.location = location;
setOrientation(orientation); setOrientation(orientation);
if (!isValidMininmumRadius(minimumRadius)) if (!isValidMinimumRadius(minimumRadius))
throw new IllegalArgumentException("Invalid minimum radius"); // TODO add decent exception msg throw new IllegalArgumentException("Invalid minimum radius"); // TODO add decent exception msg
if (!isValidRadius(radius))
throw new IllegalArgumentException("Invalid radius");
setRadius(radius); this.radius = radius;
this.minimumRadius = minimumRadius; this.minimumRadius = minimumRadius;
this.points = this.maxPoints; this.points = this.maxPoints;
@@ -141,11 +147,15 @@ public class Worm {
*/ */
private void setLocation(Tuple<Double, Double> location) throws IllegalArgumentException { private void setLocation(Tuple<Double, Double> location) throws IllegalArgumentException {
if (location == null || Double.isNaN(location.item1) || Double.isNaN(location.item2)) if (!isValidLocation(location))
throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg
this.location = location; this.location = location;
} }
private boolean isValidLocation(Tuple<Double, Double>location) {
return location != null && !Double.isNaN(location.item1) && !Double.isNaN(location.item2);
}
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -209,13 +219,17 @@ public class Worm {
* |! isValidRadius(radius) * |! isValidRadius(radius)
*/ */
public void setRadius(double radius) throws IllegalArgumentException { public void setRadius(double radius) throws IllegalArgumentException {
if (radius < this.minimumRadius || Double.isNaN(radius)) if (!isValidRadius(radius))
throw new IllegalArgumentException("Invalid radius"); throw new IllegalArgumentException("Invalid radius");
this.radius = radius; this.radius = radius;
setMass(radius); setMass(radius);
} }
private boolean isValidRadius(double radius) {
return radius >= this.minimumRadius && !Double.isNaN(radius);
}
/** /**
* Return the minimum radius the worm can have * Return the minimum radius the worm can have
* the minimum radius of the worm expresses the minimum length * the minimum radius of the worm expresses the minimum length
@@ -225,7 +239,7 @@ public class Worm {
return this.minimumRadius; return this.minimumRadius;
} }
private boolean isValidMininmumRadius(double radius) { private boolean isValidMinimumRadius(double radius) {
return !Double.isNaN(radius) && radius > 0; return !Double.isNaN(radius) && radius > 0;
} }
@@ -249,7 +263,7 @@ public class Worm {
private void setMass(double radius) { private void setMass(double radius) {
final int rho = 1062; final int rho = 1062;
double mass = rho * (4 / 3 * Math.PI * Math.pow(radius, 3)); double mass = rho * (4 / 3 * PI * pow(radius, 3));
this.mass = mass; this.mass = mass;
setMaxPoints(mass); setMaxPoints(mass);
@@ -276,7 +290,7 @@ public class Worm {
this.points = points; this.points = points;
} }
private void setMaxPoints(double maxPoints) { private void setMaxPoints(double maxPoints) {
this.maxPoints = (int) Math.ceil(maxPoints); this.maxPoints = (int) ceil(maxPoints);
setPoints(this.points); setPoints(this.points);
} }
private void subtractPoints (int value) { private void subtractPoints (int value) {
@@ -285,7 +299,7 @@ public class Worm {
} }
private void subtractPoints (double angle) { private void subtractPoints (double angle) {
setPoints(this.points - (int) Math.ceil(Math.toDegrees(angle) / 6)); setPoints(this.points - (int) ceil(toDegrees(angle) / 6));
} }
//=================================================================================== //===================================================================================
@@ -392,12 +406,12 @@ public class Worm {
if (numberSteps < 0) if (numberSteps < 0)
throw new IllegalArgumentException(); // TODO add decent exception msg throw new IllegalArgumentException(); // TODO add decent exception msg
int cost = (int) Math.ceil(Math.abs(Math.cos(this.orientation)) + Math.abs(4 * Math.sin(this.orientation))); int cost = (int) ceil(abs(cos(this.orientation)) + abs(4 * sin(this.orientation)));
if (cost > this.points) if (cost > this.points)
throw new IllegalArgumentException(); // TODO add decent exception msg throw new IllegalArgumentException(); // TODO add decent exception msg
double distanceX = this.radius * Math.cos(this.orientation); double distanceX = this.radius * cos(this.orientation);
double distanceY = this.radius * Math.sin(this.orientation); double distanceY = this.radius * sin(this.orientation);
this.location = Tuple.create(this.location.item1 + numberSteps * distanceX, this.location = Tuple.create(this.location.item1 + numberSteps * distanceX,
this.location.item2 + numberSteps * distanceY); this.location.item2 + numberSteps * distanceY);
subtractPoints(cost); subtractPoints(cost);
@@ -427,9 +441,9 @@ public class Worm {
* |substractPoints(angle) * |substractPoints(angle)
*/ */
public void turn(double angle) { public void turn(double angle) {
assert 0 <= angle && angle < (2 * Math.PI); assert 0 <= angle && angle < (2 * PI);
setOrientation((this.orientation + angle) % (2 * Math.PI)); setOrientation((this.orientation + angle) % (2 * PI));
subtractPoints(angle); subtractPoints(angle);
} }
@@ -444,9 +458,9 @@ public class Worm {
* this constant contains the gravity * this constant contains the gravity
*/ */
private final double G = 5.0; private final double G = 5.0;
/** /**
* this constant contains the duration of the force * this constant contains the duration of the force
*/ */
private final double FORCE_TIME = 0.5; private final double FORCE_TIME = 0.5;
@@ -482,7 +496,7 @@ public class Worm {
* |result == this.points > 0 && this.orientation < Math.PI * |result == this.points > 0 && this.orientation < Math.PI
*/ */
private boolean canJump() { private boolean canJump() {
return this.points > 0 && this.orientation < Math.PI; return this.points > 0 && this.orientation < PI;
} }
/** /**
@@ -494,7 +508,7 @@ public class Worm {
*/ */
public double jumpTime() { public double jumpTime() {
return jumpDistance(this.jumpVelocity) / (this.jumpVelocity * Math.cos(this.orientation)); return jumpDistance(this.jumpVelocity) / (this.jumpVelocity * cos(this.orientation));
} }
/** /**
@@ -507,8 +521,8 @@ public class Worm {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
return Tuple.create(oldLocation.item1 + this.jumpVelocity * Math.cos(this.orientation) * deltaTime, return Tuple.create(oldLocation.item1 + this.jumpVelocity * cos(this.orientation) * deltaTime,
oldLocation.item2 + this.jumpVelocity * Math.sin(this.orientation) * deltaTime - 0.5 * G * Math.pow(deltaTime, 2)); oldLocation.item2 + this.jumpVelocity * sin(this.orientation) * deltaTime - 0.5 * G * pow(deltaTime, 2));
} }
/** /**
@@ -521,7 +535,7 @@ public class Worm {
* |(Math.pow(v, 2) * Math.sin(2 * this.orientation)) / this.G * |(Math.pow(v, 2) * Math.sin(2 * this.orientation)) / this.G
*/ */
private double jumpDistance(double v) { private double jumpDistance(double v) {
return (Math.pow(v, 2) * Math.sin(2 * this.orientation)) / this.G; return (pow(v, 2) * sin(2 * this.orientation)) / this.G;
} }
/** /**
@@ -536,7 +550,7 @@ public class Worm {
private double jumpVelocity() { private double jumpVelocity() {
double force = 5 * this.points + this.mass * G; double force = 5 * this.points + this.mass * G;
return (force / this.mass) * FORCE_TIME; return force / this.mass * FORCE_TIME;
} }