improved facade

This commit is contained in:
2018-03-08 16:06:09 +01:00
parent 1aa5892d3b
commit 0df8b044ef
2 changed files with 79 additions and 59 deletions

View File

@@ -31,6 +31,7 @@ public class Facade implements IFacade {
@Override @Override
public void move(Worm worm, int nbSteps) throws ModelException { public void move(Worm worm, int nbSteps) throws ModelException {
worm.move(nbSteps);
} }
/** /**
@@ -42,6 +43,7 @@ public class Facade implements IFacade {
@Override @Override
public void turn(Worm worm, double angle) throws ModelException { public void turn(Worm worm, double angle) throws ModelException {
worm.turn(angle);
} }
/** /**
@@ -52,6 +54,7 @@ public class Facade implements IFacade {
@Override @Override
public void jump(Worm worm) throws ModelException { public void jump(Worm worm) throws ModelException {
worm.jump();
} }
/** /**
@@ -62,7 +65,8 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getJumpTime(Worm worm) throws ModelException { public double getJumpTime(Worm worm) throws ModelException {
return 0;
return worm.jumpTime();
} }
/** /**
@@ -75,7 +79,9 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double[] getJumpStep(Worm worm, double t) throws ModelException { public double[] getJumpStep(Worm worm, double t) throws ModelException {
return new double[0];
Tuple<Double, Double> jumpStep = worm.jumpStep(t);
return new double[] {jumpStep.item1, jumpStep.item2};
} }
/** /**
@@ -85,7 +91,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getX(Worm worm) throws ModelException { public double getX(Worm worm) throws ModelException {
return 0; return worm.getLocation().item1;
} }
/** /**
@@ -95,7 +101,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getY(Worm worm) throws ModelException { public double getY(Worm worm) throws ModelException {
return 0; return worm.getLocation().item2;
} }
/** /**
@@ -105,7 +111,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getOrientation(Worm worm) throws ModelException { public double getOrientation(Worm worm) throws ModelException {
return 0; return worm.getOrientation();
} }
/** /**
@@ -115,7 +121,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getRadius(Worm worm) throws ModelException { public double getRadius(Worm worm) throws ModelException {
return 0; return worm.getRadius();
} }
/** /**
@@ -126,7 +132,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void setRadius(Worm worm, double newRadius) throws ModelException { public void setRadius(Worm worm, double newRadius) throws ModelException {
worm.setRadius(newRadius);
} }
/** /**
@@ -136,7 +142,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public long getNbActionPoints(Worm worm) throws ModelException { public long getNbActionPoints(Worm worm) throws ModelException {
return 0; return worm.getActionPoints();
} }
/** /**
@@ -148,7 +154,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void decreaseNbActionPoints(Worm worm, long delta) throws ModelException { public void decreaseNbActionPoints(Worm worm, long delta) throws ModelException {
worm.decreaseActionPoints((int) delta);
} }
/** /**
@@ -158,7 +164,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public long getMaxNbActionPoints(Worm worm) throws ModelException { public long getMaxNbActionPoints(Worm worm) throws ModelException {
return 0; return worm.getMaxActionPoints();
} }
/** /**
@@ -168,7 +174,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public String getName(Worm worm) throws ModelException { public String getName(Worm worm) throws ModelException {
return null; return worm.getName();
} }
/** /**
@@ -179,7 +185,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void rename(Worm worm, String newName) throws ModelException { public void rename(Worm worm, String newName) throws ModelException {
worm.setName(newName);
} }
/** /**
@@ -189,6 +195,6 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getMass(Worm worm) throws ModelException { public double getMass(Worm worm) throws ModelException {
return 0; return worm.getMass();
} }
} }

View File

@@ -51,14 +51,14 @@ public class Worm {
private double mass; private double mass;
/** /**
* this variable contains the current action points of the worm * this variable contains the current action actionPoints of the worm
*/ */
private int points; private long actionPoints;
/** /**
* this variable contains the maximum points a worm can have * this variable contains the maximum actionPoints a worm can have
*/ */
private int maxPoints; private long maxActionPoints;
/** /**
* this variable contains the name of the worm * this variable contains the name of the worm
@@ -109,7 +109,7 @@ public class Worm {
this.radius = radius; this.radius = radius;
this.minimumRadius = minimumRadius; this.minimumRadius = minimumRadius;
this.points = this.maxPoints; this.actionPoints = this.maxActionPoints;
int validName = isValidName(name); int validName = isValidName(name);
if (validName != -1) if (validName != -1)
@@ -262,11 +262,11 @@ public class Worm {
*/ */
private void setMass(double radius) { private void setMass(double radius) {
final int rho = 1062; final long rho = 1062;
double mass = rho * (4 / 3 * PI * pow(radius, 3)); double mass = rho * (4 / 3 * PI * pow(radius, 3));
this.mass = mass; this.mass = mass;
setMaxPoints(mass); setMaxActionPoints(mass);
} }
//=================================================================================== //===================================================================================
@@ -274,32 +274,46 @@ public class Worm {
// region Points // region ActionPoints
//=================================================================================== //===================================================================================
public int getPoints() { public long getActionPoints() {
return this.points; return this.actionPoints;
} }
public void setPoints(int points) { public long getMaxActionPoints() {
if (points > this.maxPoints) return this.maxActionPoints;
points = this.maxPoints;
else if (points < 0)
points = 0;
this.points = points;
} }
private void setMaxPoints(double maxPoints) {
this.maxPoints = (int) ceil(maxPoints);
setPoints(this.points);
}
private void subtractPoints (int value) {
setPoints(this.points - value); public void setActionPoints(long actionPoints) {
}
private void subtractPoints (double angle) {
setPoints(this.points - (int) ceil(toDegrees(angle) / 6)); this.actionPoints = toValidActionPoints(actionPoints);
}
private void setMaxActionPoints(double maxActionPoints) {
this.maxActionPoints = round(maxActionPoints);
setActionPoints(this.actionPoints);
}
public void decreaseActionPoints (long delta) {
this.actionPoints = toValidActionPoints(this.actionPoints - delta);
}
private long toValidActionPoints(long actionPoints) {
if (actionPoints > this.maxActionPoints)
actionPoints = this.maxActionPoints;
else if (actionPoints < 0)
actionPoints = 0;
return actionPoints;
}
private void subtractActionPoints (long value) {
setActionPoints(this.actionPoints - value);
}
private void subtractActionPoints (double angle) {
setActionPoints(this.actionPoints - (long) ceil(toDegrees(angle) / 6));
} }
//=================================================================================== //===================================================================================
@@ -393,28 +407,28 @@ public class Worm {
* with the sinus of the orientation) * with the sinus of the orientation)
* |distanceY = this.radius * Math.sin(this.orientation) * |distanceY = this.radius * Math.sin(this.orientation)
* |new.yCoordinate = this.location.item2 + numberSteps * distanceY * |new.yCoordinate = this.location.item2 + numberSteps * distanceY
* @post the current value of action points has changed. The current value of action points * @post the current value of action actionPoints has changed. The current value of action actionPoints
* minus the cost of moving (abs(cos(theta)) + abs(4 sin(theta))) * minus the cost of moving (abs(cos(theta)) + abs(4 sin(theta)))
* |value = (int) Math.ceil(Math.abs(Math.cos(this.orientation)) + Math.abs(4 * Math.sin(this.orientation))) * |value = (int) Math.ceil(Math.abs(Math.cos(this.orientation)) + Math.abs(4 * Math.sin(this.orientation)))
* |setPoints(this.points - value) * |setActionPoints(this.actionPoints - value)
* @throws IllegalArgumentException * @throws IllegalArgumentException
* when the total of steps is lower then 0 or when the cost of action point is more * when the total of steps is lower then 0 or when the cost of action point is more
* then the current value of action point * then the current value of action point
* |NumberSteps < 0 || cost > this.points * |NumberSteps < 0 || cost > this.actionPoints
*/ */
public void move(int numberSteps) { public void move(int numberSteps) {
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) ceil(abs(cos(this.orientation)) + abs(4 * sin(this.orientation))); int cost = (int) ceil(abs(cos(this.orientation)) + abs(4 * sin(this.orientation)));
if (cost > this.points) if (cost > this.actionPoints)
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(this.orientation);
double distanceY = this.radius * 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); subtractActionPoints(cost);
} }
//=================================================================================== //===================================================================================
@@ -435,15 +449,15 @@ public class Worm {
* |new.getOrientation() = this.getOrientation() + angle * |new.getOrientation() = this.getOrientation() + angle
* @post the resulting angle (= the new orientation) must be between 0 and 2pi (including 0) * @post the resulting angle (= the new orientation) must be between 0 and 2pi (including 0)
* |0 <= new.getOrientation() < 2pi * |0 <= new.getOrientation() < 2pi
* @post current points of action points schould be reduced, for this there is another * @post current actionPoints of action actionPoints schould be reduced, for this there is another
* method with as parameter the given angle * method with as parameter the given angle
* |substractPoints(angle) * |substractActionPoints(angle)
*/ */
public void turn(double angle) { public void turn(double angle) {
assert 0 <= angle && angle < (2 * PI) && !Double.isNaN(angle); assert 0 <= angle && angle < (2 * PI) && !Double.isNaN(angle);
setOrientation((this.orientation + angle) % (2 * PI)); setOrientation((this.orientation + angle) % (2 * PI));
subtractPoints(angle); subtractActionPoints(angle);
} }
//=================================================================================== //===================================================================================
@@ -467,12 +481,12 @@ public class Worm {
* @post the worm jumps to his new place. The new place is the x-coordinate plus the jump distance * @post the worm jumps to his new place. The new place is the x-coordinate plus the jump distance
* with the jump velocity * with the jump velocity
* |worms.util.Tuple.create(location.item1 + jumpDistance(this.jumpVelocity), location.item2) * |worms.util.Tuple.create(location.item1 + jumpDistance(this.jumpVelocity), location.item2)
* @post the current action points schould be 0 after a jump * @post the current action actionPoints schould be 0 after a jump
* |this.points = 0 * |this.actionPoints = 0
* @throws IllegalStateException() * @throws IllegalStateException()
* if the current action points is equal to 0 or the orientation is more then * if the current action actionPoints is equal to 0 or the orientation is more then
* pi the worm can not jump * pi the worm can not jump
* |this.points == 0 || this.orientation < Math.PI * |this.actionPoints == 0 || this.orientation < Math.PI
*/ */
public void jump() { public void jump() {
@@ -483,18 +497,18 @@ public class Worm {
this.jumpVelocity = jumpVelocity(); this.jumpVelocity = jumpVelocity();
this.jumpTime = jumpTime(); this.jumpTime = jumpTime();
this.location = Tuple.create(location.item1 + jumpDistance(this.jumpVelocity), location.item2); this.location = Tuple.create(location.item1 + jumpDistance(this.jumpVelocity), location.item2);
this.points = 0; this.actionPoints = 0;
} }
/** /**
* Return a boolean whether the worm can jump or not * Return a boolean whether the worm can jump or not
* *
* @return True if and only if the action points is bigger then 0 and the orientation * @return True if and only if the action actionPoints is bigger then 0 and the orientation
* is lower then pi * is lower then pi
* |result == this.points > 0 && this.orientation < Math.PI * |result == this.actionPoints > 0 && this.orientation < Math.PI
*/ */
private boolean canJump() { private boolean canJump() {
return this.points > 0 && this.orientation < PI; return this.actionPoints > 0 && this.orientation < PI;
} }
/** /**
@@ -506,7 +520,7 @@ public class Worm {
*/ */
public double jumpTime() { public double jumpTime() {
if (this.orientation >= PI || this.points == 0) if (this.orientation >= PI || this.actionPoints == 0)
throw new IllegalStateException(); throw new IllegalStateException();
return jumpDistance(jumpVelocity()) / (jumpVelocity() * cos(this.orientation)); return jumpDistance(jumpVelocity()) / (jumpVelocity() * cos(this.orientation));
} }
@@ -542,14 +556,14 @@ public class Worm {
* Return the velocity of the jump * Return the velocity of the jump
* *
* @return the velocity of the jump. The force of the jump is equal to 5 multiplied with the * @return the velocity of the jump. The force of the jump is equal to 5 multiplied with the
* current action points plus the mass multiplied with the gravity. Therefrom the velocity * current action actionPoints plus the mass multiplied with the gravity. Therefrom the velocity
* is equal to the force divided with the mass, multiplied with the time te force takes * is equal to the force divided with the mass, multiplied with the time te force takes
* |force = 5 * this.points + this.mass * G * |force = 5 * this.actionPoints + this.mass * G
* |velocity = (force / this.mass) * FORCE_TIME * |velocity = (force / this.mass) * FORCE_TIME
*/ */
private double jumpVelocity() { private double jumpVelocity() {
double force = 5 * this.points + this.mass * G; double force = 5 * this.actionPoints + this.mass * G;
return force / this.mass * FORCE_TIME; return force / this.mass * FORCE_TIME;
} }