lots of changes

This commit is contained in:
2018-03-09 23:46:57 +01:00
parent d1617e9cf3
commit d8981def95

View File

@@ -53,7 +53,7 @@ public class Worm {
if (!isValidMinRadius(minRadius)) if (!isValidMinRadius(minRadius))
throw new IllegalArgumentException("Invalid min radius"); // TODO add decent exception msg throw new IllegalArgumentException("Invalid min radius"); // TODO add decent exception msg
if (!isValidRadius(radius)) if (!canHaveAsMinRadius(radius))
throw new IllegalArgumentException("Invalid radius"); throw new IllegalArgumentException("Invalid radius");
setRadius(radius); setRadius(radius);
@@ -199,10 +199,10 @@ public class Worm {
* |new.getRadius() == radius * |new.getRadius() == radius
* @throws IllegalArgumentException * @throws IllegalArgumentException
* the given radius is not a valid radius for any worm * the given radius is not a valid radius for any worm
* |! isValidRadius(radius) * |! canHaveAsMinRadius(radius)
*/ */
public void setRadius(double radius) throws IllegalArgumentException { public void setRadius(double radius) throws IllegalArgumentException {
if (!isValidRadius(radius)) if (!canHaveAsMinRadius(radius))
throw new IllegalArgumentException("Invalid radius"); throw new IllegalArgumentException("Invalid radius");
this.radius = radius; this.radius = radius;
@@ -240,8 +240,8 @@ public class Worm {
* (or equal) and the radius is a number * (or equal) and the radius is a number
* |result == (radius >= this.minRadius && !Double.isNaN(radius)) * |result == (radius >= this.minRadius && !Double.isNaN(radius))
*/ */
private boolean isValidRadius(double radius) { private boolean canHaveAsMinRadius(double radius) {
return radius >= this.minRadius && !Double.isNaN(radius); return radius >= getMinRadius() && !Double.isNaN(radius);
} }
@@ -316,16 +316,21 @@ public class Worm {
* |this.actionPoints = actionPoints * |this.actionPoints = actionPoints
*/ */
public void setActionPoints(long actionPoints) { public void setActionPoints(long actionPoints) {
this.actionPoints = toValidActionPoints(actionPoints); if (actionPoints > getMaxActionPoints())
actionPoints = getMaxActionPoints();
else if (actionPoints < 0)
actionPoints = 0;
this.actionPoints = actionPoints;
} }
/** /**
* *
* @param delta * @param delta ...
*/ */
public void decreaseActionPoints (long delta) { public void decreaseActionPoints (long delta) {
this.actionPoints = toValidActionPoints(this.actionPoints - delta); setActionPoints(getActionPoints() - delta);
} }
/** /**
@@ -338,38 +343,19 @@ public class Worm {
*/ */
private long maxActionPoints; private long maxActionPoints;
private void setMaxActionPoints(double maxActionPoints) {
this.maxActionPoints = round(maxActionPoints);
setActionPoints(this.actionPoints);
}
/**
*
* @param actionPoints
* @return
*/
private long toValidActionPoints(long actionPoints) {
if (actionPoints > this.maxActionPoints)
actionPoints = this.maxActionPoints;
else if (actionPoints < 0)
actionPoints = 0;
return actionPoints;
}
/** /**
* set the maximum of points to the given maximum of points * set the maximum of points to the given maximum of points
* *
* @param maxPoints * @param maxActionPoints
* the new maximum of points for the worm * the new maximum of points for the worm
* @post the new maximum points is set to the given maximum points (as an integer) * @post the new maximum points is set to the given maximum points (as an integer)
* |this.maxPoints = (int) ceil(maxPoints) * |this.maxPoints = (int) ceil(maxPoints)
* @post when the maximum points change, the current points should change too * @post when the maximum points change, the current points should change too
* |setPoints(this.points) * |setPoints(this.points)
*/ */
private void setMaxPoints(double maxPoints) { private void setMaxActionPoints(double maxActionPoints) {
this.maxActionPoints = (int) round(maxPoints); this.maxActionPoints = round(maxActionPoints);
setActionPoints(this.actionPoints); setActionPoints(getActionPoints());
} }
/** /**
@@ -382,7 +368,7 @@ public class Worm {
*/ */
private void subtractActionPoints (long value) { private void subtractActionPoints (long value) {
setActionPoints(this.actionPoints - value); setActionPoints(getActionPoints() - value);
} }
/** /**
@@ -396,7 +382,7 @@ public class Worm {
*/ */
private void subtractActionPoints (double angle) { private void subtractActionPoints (double angle) {
setActionPoints(this.actionPoints - (long) ceil(toDegrees(angle) / 6)); setActionPoints(getActionPoints() - (long) ceil(toDegrees(angle) / 6));
} }
//=================================================================================== //===================================================================================
@@ -510,15 +496,15 @@ 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
long cost = (long) ceil(abs(cos(this.orientation)) + abs(4 * sin(this.orientation))); long cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation())));
if (cost > this.actionPoints) if (cost > getActionPoints())
throw new IllegalArgumentException(); // TODO add decent exception msg throw new IllegalArgumentException(); // TODO add decent exception msg
double distanceX = this.radius * cos(this.orientation); double distanceX = this.radius * cos(getOrientation());
double distanceY = this.radius * sin(this.orientation); double distanceY = this.radius * sin(getOrientation());
this.location = Tuple.create(this.location.item1 + numberSteps * distanceX, setLocation(Tuple.create(getLocation().item1 + numberSteps * distanceX,
this.location.item2 + numberSteps * distanceY); getLocation().item2 + numberSteps * distanceY));
subtractActionPoints(cost); subtractActionPoints(cost);
} }
@@ -558,7 +544,7 @@ public class Worm {
private boolean canTurn(double angle) { private boolean canTurn(double angle) {
return 0 <= angle && angle < (2 * PI) && return 0 <= angle && angle < (2 * PI) &&
!Double.isNaN(angle) && !Double.isNaN(angle) &&
this.actionPoints - (long) ceil(toDegrees(angle) / 6) >= 0; getActionPoints() - (long) ceil(toDegrees(angle) / 6) >= 0;
} }
//=================================================================================== //===================================================================================
@@ -594,8 +580,8 @@ public class Worm {
if (!canJump()) if (!canJump())
throw new IllegalStateException(); throw new IllegalStateException();
this.location = Tuple.create(location.item1 + jumpDistance(this.jumpVelocity()), location.item2); setLocation(Tuple.create(getLocation().item1 + jumpDistance(this.jumpVelocity()), getLocation().item2));
this.actionPoints = 0; setActionPoints(0);
} }
/** /**
@@ -607,10 +593,10 @@ public class Worm {
*/ */
public double jumpTime() { public double jumpTime() {
if (this.orientation >= PI || this.actionPoints == 0) if (getOrientation() >= PI || getActionPoints() == 0)
throw new IllegalStateException(); throw new IllegalStateException();
return (jumpDistance(jumpVelocity()) / (jumpVelocity() * cos(this.orientation))); return (jumpDistance(jumpVelocity()) / (jumpVelocity() * cos(getOrientation())));
} }
/** /**
@@ -622,8 +608,8 @@ public class Worm {
if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
return Tuple.create(this.location.item1 + this.jumpVelocity() * cos(this.orientation) * deltaTime, return Tuple.create(getLocation().item1 + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
this.location.item2 + this.jumpVelocity() * sin(this.orientation) * deltaTime - 0.5 * G * pow(deltaTime, 2)); getLocation().item2 + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2));
} }
/** /**
@@ -634,7 +620,7 @@ public class Worm {
* |result == this.actionPoints > 0 && this.orientation < Math.PI * |result == this.actionPoints > 0 && this.orientation < Math.PI
*/ */
private boolean canJump() { private boolean canJump() {
return this.actionPoints > 0 && this.orientation < PI; return getActionPoints() > 0 && getOrientation() < PI;
} }
/** /**
@@ -647,7 +633,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 (pow(v, 2) * sin(2 * this.orientation)) / G; return (pow(v, 2) * sin(2 * getOrientation())) / G;
} }
/** /**
@@ -661,13 +647,11 @@ public class Worm {
*/ */
private double jumpVelocity() { private double jumpVelocity() {
double force = 5 * this.actionPoints + this.mass * G; double force = 5 * getActionPoints() + getMass() * G;
return force / this.mass * FORCE_TIME; return force / getMass() * FORCE_TIME;
} }
//=================================================================================== //===================================================================================
// endregion // endregion
} }