This commit is contained in:
Leen Dereu
2018-03-23 13:31:51 +01:00
2 changed files with 66 additions and 25 deletions

View File

@@ -1,7 +1,7 @@
package worms.model; package worms.model;
import be.kuleuven.cs.som.annotate.*; import be.kuleuven.cs.som.annotate.*;
import worms.util.Tuple; import worms.util.Coordinate;
import worms.util.IllegalNameException; import worms.util.IllegalNameException;
import static java.lang.Math.*; import static java.lang.Math.*;
@@ -58,7 +58,7 @@ public class Worm {
* |new.getRadius() == radius * |new.getRadius() == radius
*/ */
@Raw @Raw
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius) { public Worm(Coordinate location, double orientation, String name, double radius) {
this(location, orientation, name, radius, 0.25); this(location, orientation, name, radius, 0.25);
} }
@@ -100,7 +100,7 @@ public class Worm {
* |isValidName(name) * |isValidName(name)
*/ */
@Raw @Raw
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minRadius) public Worm(Coordinate location, double orientation, String name, double radius, double minRadius)
throws IllegalArgumentException { throws IllegalArgumentException {
if (!isValidLocation(location)) if (!isValidLocation(location))
@@ -141,7 +141,7 @@ public class Worm {
* in the play area * in the play area
*/ */
@Basic @Immutable @Raw @Basic @Immutable @Raw
public Tuple<Double, Double> getLocation() { public Coordinate getLocation() {
return this.location; return this.location;
} }
@@ -152,18 +152,17 @@ public class Worm {
* the location to check * the location to check
* @return True if and only if the location is not equal to null and the coordinates of * @return True if and only if the location is not equal to null and the coordinates of
* the worm are numbers * the worm are numbers
* |result == ( (location != null) && (location.item1 != null) && (location.item2 != null) * |result == ( (location != null) && (location.getCoordinateX() != null) && (location.getCoordinateY() != null)
* |&& (!Double.isNaN(location.item1)) && (!Double.isNaN(location.item2)) ) * |&& (!Double.isNaN(location.getCoordinateX())) && (!Double.isNaN(location.getCoordinateY())) )
*/ */
public static boolean isValidLocation(Tuple<Double, Double>location) { public static boolean isValidLocation(Coordinate location) {
return location != null && location.item1 != null && location.item2 != null return location != null && !Double.isNaN(location.getCoordinateX()) && !Double.isNaN(location.getCoordinateY());
&& !Double.isNaN(location.item1) && !Double.isNaN(location.item2);
} }
/** /**
* this variable contains the location of the worm (a tuple) * this variable contains the location of the worm (a Coordinate)
*/ */
private Tuple<Double, Double> location; private Coordinate location;
/** /**
* set the location of the worm to the given location * set the location of the worm to the given location
@@ -177,7 +176,7 @@ public class Worm {
* |! isValidLocation(location) * |! isValidLocation(location)
*/ */
@Raw @Raw
private void setLocation(Tuple<Double, Double> location) throws IllegalArgumentException { private void setLocation(Coordinate location) throws IllegalArgumentException {
if (!isValidLocation(location)) if (!isValidLocation(location))
throw new IllegalArgumentException("Illegal value for location"); throw new IllegalArgumentException("Illegal value for location");
@@ -562,13 +561,13 @@ public class Worm {
* that the x-coordinate should move (distance is equal to the radius multiplied * that the x-coordinate should move (distance is equal to the radius multiplied
* with the cosinus of the orientation) * with the cosinus of the orientation)
* |distanceX = this.radius * cos(getOrientation()) * |distanceX = this.radius * cos(getOrientation())
* |new.xCoordinate = getLocation().item1 + numberSteps * distanceX * |new.xCoordinate = getLocation().getCoordinateX() + numberSteps * distanceX
* @post the y-coordinate of the new location of the worm should be the location of * @post the y-coordinate of the new location of the worm should be the location of
* the old y-coordinate plus the number of steps multiplied with the distance * the old y-coordinate plus the number of steps multiplied with the distance
* that the y-coordinate should move (distance is equal to the radius multiplied * that the y-coordinate should move (distance is equal to the radius multiplied
* with the sinus of the orientation) * with the sinus of the orientation)
* |distanceY = this.radius * sin(getOrientation()) * |distanceY = this.radius * sin(getOrientation())
* |new.yCoordinate = getLocation().item2 + numberSteps * distanceY * |new.yCoordinate = getLocation().getCoordinateY() + numberSteps * distanceY
* @post the current value of action actionPoints has changed. The current value of action actionPoints * @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)))
* |cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))) * numberSteps * |cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))) * numberSteps
@@ -589,8 +588,8 @@ public class Worm {
double distanceX = getRadius() * cos(getOrientation()); double distanceX = getRadius() * cos(getOrientation());
double distanceY = getRadius() * sin(getOrientation()); double distanceY = getRadius() * sin(getOrientation());
setLocation(Tuple.create(getLocation().item1 + numberSteps * distanceX, setLocation(Coordinate.create(getLocation().getCoordinateX() + numberSteps * distanceX,
getLocation().item2 + numberSteps * distanceY)); getLocation().getCoordinateY() + numberSteps * distanceY));
subtractActionPoints(cost); subtractActionPoints(cost);
} }
@@ -666,7 +665,7 @@ 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
* |setLocation(Tuple.create(getLocation().item1 + jumpDistance(this.jumpVelocity()), getLocation().item2)) * |setLocation(Coordinate.create(getLocation().getCoordinateX() + jumpDistance(this.jumpVelocity()), getLocation().getCoordinateY()))
* @post the current action actionPoints should be 0 after a jump * @post the current action actionPoints should be 0 after a jump
* |setActionPoints(0) * |setActionPoints(0)
* @throws IllegalStateException * @throws IllegalStateException
@@ -679,7 +678,7 @@ public class Worm {
if (!canJump()) if (!canJump())
throw new IllegalStateException(); throw new IllegalStateException();
setLocation(Tuple.create(getLocation().item1 + jumpDistance(this.jumpVelocity()), getLocation().item2)); setLocation(Coordinate.create( getLocation().getCoordinateX() + jumpDistance(this.jumpVelocity()), getLocation().getCoordinateY()));
setActionPoints(0); setActionPoints(0);
} }
@@ -705,22 +704,22 @@ public class Worm {
* *
* @param deltaTime * @param deltaTime
* the total time the worm should jump * the total time the worm should jump
* @return Tuple<Double, Double> with the new location of the worm. The new x-coordinate is the old x-coordinate plus the jump velocity * @return Coordinate with the new location of the worm. The new x-coordinate is the old x-coordinate plus the jump velocity
* multiplied with the cosinus of the orientation multiplied with delta time. The new y-coordinate is the old y-coordinate plus * multiplied with the cosinus of the orientation multiplied with delta time. The new y-coordinate is the old y-coordinate plus
* the jump velocity multiplied with the sinus of the orientation multiplied with delta time minus 0,5 times the gravity multiplied * the jump velocity multiplied with the sinus of the orientation multiplied with delta time minus 0,5 times the gravity multiplied
* with the second power of delta time * with the second power of delta time
* |Tuple.create(getLocation().item1 + this.jumpVelocity() * cos(getOrientation()) * deltaTime, * |Coordinate.create(getLocation().getCoordinateX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
|getLocation().item2 + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)) |getLocation().getCoordinateY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2))
* @throws IllegalArgumentException() * @throws IllegalArgumentException()
* if the deltaTime is not a number or bigger then jumpTime or smaller then 0 * 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 > this.jumpTime() || deltaTime < 0)
*/ */
public Tuple<Double, Double> jumpStep(double deltaTime) { public Coordinate jumpStep(double deltaTime) {
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(getLocation().item1 + this.jumpVelocity() * cos(getOrientation()) * deltaTime, return Coordinate.create(getLocation().getCoordinateX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime,
getLocation().item2 + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)); getLocation().getCoordinateY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2));
} }
/** /**

View File

@@ -0,0 +1,42 @@
package worms.util;
import be.kuleuven.cs.som.annotate.Basic;
import be.kuleuven.cs.som.annotate.Immutable;
import be.kuleuven.cs.som.annotate.Value;
@Value
public class Coordinate extends Tuple<Double, Double> {
/**
* Initialize this new Tuple as a Tuple with the given items.
*
* @param coordinateX The x-coordinate for this Coordinate
* @param coordinateY The y-coordinate for this Coordinate
*/
public Coordinate(double coordinateX, double coordinateY) throws IllegalArgumentException {
super(coordinateX, coordinateY);
}
/**
*
* @param coordinateX
* The value for the x-coordinate.
* @param coordinateY
* The value for the y-coordinate.
* @return
* The coordinate created from coordinateX and coordinateY.
* | result == new Coordinate(coordinateX, coordinateY);
*/
@Immutable
public static Coordinate create(double coordinateX, double coordinateY){
return new Coordinate(coordinateX, coordinateY);
}
@Basic @Immutable
public double getCoordinateX() {
return this.item1;
}
@Basic @Immutable
public double getCoordinateY() {
return this.item2;
}
}