mass update

This commit is contained in:
2018-04-11 21:05:45 +02:00
parent 016a2e2bbb
commit 17ce8566a3
8 changed files with 270 additions and 224 deletions

View File

@@ -13,7 +13,7 @@ public class Main {
Worm worm3 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Gamma", 1.0); Worm worm3 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Gamma", 1.0);
Worm worm4 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Delta", 1.0); Worm worm4 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Delta", 1.0);
Team team1 = new Team(null, "TestTeam"); Team team1 = new Team("TestTeam");
team1.addWorm(worm1); team1.addWorm(worm1);
team1.addWorm(worm2); team1.addWorm(worm2);

View File

@@ -102,7 +102,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double[] getFurthestLocationInDirection(Worm worm, double direction, double maxDistance) throws ModelException { public double[] getFurthestLocationInDirection(Worm worm, double direction, double maxDistance) throws ModelException {
return new double[0]; return worm.getFurthestLocationInDirection(direction, maxDistance).toArray();
} }
/** /**
@@ -112,7 +112,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void move(Worm worm) throws ModelException { public void move(Worm worm) throws ModelException {
worm.move();
} }
/** /**
@@ -129,7 +129,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void fall(Worm worm) throws ModelException, MustNotImplementException { public void fall(Worm worm) throws ModelException, MustNotImplementException {
worm.fall();
} }
/** /**
@@ -142,7 +142,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getJumpTime(Worm worm, double deltaT) throws ModelException { public double getJumpTime(Worm worm, double deltaT) throws ModelException {
return 0; return worm.jumpTime();
} }
// /** // /**
@@ -222,7 +222,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void jump(Worm worm, double timeStep) throws ModelException { public void jump(Worm worm, double timeStep) throws ModelException {
worm.jump();
} }
/** /**
@@ -236,7 +236,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public Food createFood(World world, double[] location) throws ModelException { public Food createFood(World world, double[] location) throws ModelException {
return null; return new Food(world, location);
} }
/** /**
@@ -246,7 +246,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void terminate(Food food) throws ModelException { public void terminate(Food food) throws ModelException {
food.terminate();
} }
/** /**
@@ -256,7 +256,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean isTerminated(Food food) throws ModelException { public boolean isTerminated(Food food) throws ModelException {
return false; return food.isTerminated();
} }
/** /**
@@ -268,7 +268,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double[] getLocation(Food food) throws ModelException { public double[] getLocation(Food food) throws ModelException {
return new double[0]; return food.getLocationArray();
} }
/** /**
@@ -278,7 +278,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getRadius(Food food) throws ModelException { public double getRadius(Food food) throws ModelException {
return 0; return food.getRadius();
} }
/** /**
@@ -288,7 +288,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getMass(Food food) throws ModelException { public double getMass(Food food) throws ModelException {
return 0; return food.getMass();
} }
/** /**
@@ -298,7 +298,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public World getWorld(Food food) throws ModelException { public World getWorld(Food food) throws ModelException {
return null; return food.getWorld();
} }
/** /**
@@ -308,7 +308,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void eat(Worm worm) { public void eat(Worm worm) {
worm.eat();
} }
// /** // /**
@@ -355,7 +355,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public World createWorld(double width, double height, boolean[][] passableMap) throws ModelException { public World createWorld(double width, double height, boolean[][] passableMap) throws ModelException {
return null; return new World(width, height, passableMap);
} }
/** /**
@@ -365,7 +365,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void terminate(World world) throws ModelException { public void terminate(World world) throws ModelException {
world.terminate();
} }
/** /**
@@ -375,7 +375,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean isTerminated(World world) throws ModelException { public boolean isTerminated(World world) throws ModelException {
return false; return world.isTerminated();
} }
/** /**
@@ -385,7 +385,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getWorldWidth(World world) throws ModelException { public double getWorldWidth(World world) throws ModelException {
return 0; return world.getWidth();
} }
/** /**
@@ -395,7 +395,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double getWorldHeight(World world) throws ModelException { public double getWorldHeight(World world) throws ModelException {
return 0; return world.getHeight();
} }
/** /**
@@ -409,7 +409,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean isPassable(World world, double[] location) throws ModelException { public boolean isPassable(World world, double[] location) throws ModelException {
return false; return world.isPassable(location);
} }
/** /**
@@ -422,7 +422,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean isPassable(World world, double[] center, double radius) { public boolean isPassable(World world, double[] center, double radius) {
return false; return world.isPassable(center, radius);
} }
/** /**
@@ -436,7 +436,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean isAdjacent(World world, double[] center, double radius) { public boolean isAdjacent(World world, double[] center, double radius) {
return false; return world.isAdjacent(center, radius);
} }
/** /**
@@ -447,7 +447,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean hasAsWorm(World world, Worm worm) throws ModelException { public boolean hasAsWorm(World world, Worm worm) throws ModelException {
return false; return world.hasAsWorm(worm);
} }
/** /**
@@ -458,7 +458,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void addWorm(World world, Worm worm) throws ModelException { public void addWorm(World world, Worm worm) throws ModelException {
world.addWorm(worm);
} }
/** /**
@@ -469,7 +469,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void removeWorm(World world, Worm worm) throws ModelException { public void removeWorm(World world, Worm worm) throws ModelException {
world.removeWorm(worm);
} }
/** /**
@@ -479,7 +479,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public List<Worm> getAllWorms(World world) throws ModelException { public List<Worm> getAllWorms(World world) throws ModelException {
return null; return world.getWormList();
} }
/** /**
@@ -490,7 +490,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean hasAsFood(World world, Food food) throws ModelException { public boolean hasAsFood(World world, Food food) throws ModelException {
return false; return world.hasAsFood(food);
} }
/** /**
@@ -501,7 +501,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void addFood(World world, Food food) throws ModelException { public void addFood(World world, Food food) throws ModelException {
world.addFood(food);
} }
/** /**
@@ -512,7 +512,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void removeFood(World world, Food food) throws ModelException { public void removeFood(World world, Food food) throws ModelException {
world.removeFood(food);
} }
/** /**
@@ -523,7 +523,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public Collection<Object> getAllItems(World world) throws ModelException { public Collection<Object> getAllItems(World world) throws ModelException {
return null; return world.getAllItems();
} }
/** /**
@@ -533,7 +533,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public Set<Team> getAllTeams(World world) throws ModelException { public Set<Team> getAllTeams(World world) throws ModelException {
return null; return world.getAllTeams();
} }
/** /**
@@ -543,7 +543,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean hasActiveGame(World world) throws ModelException { public boolean hasActiveGame(World world) throws ModelException {
return false; return world.hasActiveGame();
} }
/** /**
@@ -555,7 +555,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public Worm getActiveWorm(World world) throws ModelException { public Worm getActiveWorm(World world) throws ModelException {
return null; return world.getActiveWorm();
} }
/** /**
@@ -565,7 +565,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void startGame(World world) throws ModelException { public void startGame(World world) throws ModelException {
world.startGame();
} }
/** /**
@@ -575,7 +575,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void finishGame(World world) throws ModelException { public void finishGame(World world) throws ModelException {
world.finishGame();
} }
/** /**
@@ -585,7 +585,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void activateNextWorm(World world) throws ModelException { public void activateNextWorm(World world) throws ModelException {
world.activateNextWorm();
} }
/** /**
@@ -596,7 +596,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public String getWinner(World world) { public String getWinner(World world) {
return null; return world.getWinner();
} }
/** /**
@@ -628,7 +628,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void terminate(Worm worm) throws ModelException { public void terminate(Worm worm) throws ModelException {
worm.terminate();
} }
/** /**
@@ -638,7 +638,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean isTerminated(Worm worm) throws ModelException { public boolean isTerminated(Worm worm) throws ModelException {
return false; return worm.isTerminated();
} }
/** /**
@@ -650,7 +650,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public double[] getLocation(Worm worm) throws ModelException { public double[] getLocation(Worm worm) throws ModelException {
return new double[0]; return worm.getLocationArray();
} }
/** /**
@@ -732,7 +732,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public BigInteger getNbHitPoints(Worm worm) throws ModelException { public BigInteger getNbHitPoints(Worm worm) throws ModelException {
return null; return BigInteger.valueOf(worm.getHitPoints());
} }
/** /**
@@ -745,7 +745,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void incrementNbHitPoints(Worm worm, long delta) throws ModelException { public void incrementNbHitPoints(Worm worm, long delta) throws ModelException {
worm.incrementHitPoints(delta);
} }
/** /**
@@ -800,7 +800,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public World getWorld(Worm worm) throws ModelException { public World getWorld(Worm worm) throws ModelException {
return null; return worm.getWorld();
} }
/** /**
@@ -819,7 +819,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public Team createTeam(World world, String name) throws ModelException, MustNotImplementException { public Team createTeam(World world, String name) throws ModelException, MustNotImplementException {
return null; return new Team(name);
} }
/** /**
@@ -827,7 +827,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void terminate(Team team) throws ModelException, MustNotImplementException { public void terminate(Team team) throws ModelException, MustNotImplementException {
team.terminate();
} }
/** /**
@@ -835,7 +835,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public boolean isTerminated(Team team) throws ModelException, MustNotImplementException { public boolean isTerminated(Team team) throws ModelException, MustNotImplementException {
return false; return team.isTerminated();
} }
/** /**
@@ -843,7 +843,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public String getName(Team team) throws ModelException, MustNotImplementException { public String getName(Team team) throws ModelException, MustNotImplementException {
return null; return team.getName();
} }
/** /**
@@ -851,7 +851,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public Team getTeam(Worm worm) throws ModelException { public Team getTeam(Worm worm) throws ModelException {
return null; return worm.getTeam();
} }
/** /**
@@ -859,7 +859,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public int getNbWormsOfTeam(Team team) throws ModelException, MustNotImplementException { public int getNbWormsOfTeam(Team team) throws ModelException, MustNotImplementException {
return 0; return team.getNbWorms();
} }
/** /**
@@ -868,7 +868,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public List<Worm> getAllWormsOfTeam(Team team) throws ModelException, MustNotImplementException { public List<Worm> getAllWormsOfTeam(Team team) throws ModelException, MustNotImplementException {
return null; return (List<Worm>) team.getAllWormsOfTeam();
} }
/** /**
@@ -876,7 +876,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void addWormsToTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException { public void addWormsToTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException {
team.addWorm(worms);
} }
/** /**
@@ -884,7 +884,7 @@ public class Facade implements IFacade {
*/ */
@Override @Override
public void removeWormsFromTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException { public void removeWormsFromTeam(Team team, Worm... worms) throws ModelException, MustNotImplementException {
team.removeWormsFromTeam(worms);
} }
/** /**
@@ -892,7 +892,7 @@ public class Facade implements IFacade {
* - All the worms of the supplying team are transferred to the receiving team. * - All the worms of the supplying team are transferred to the receiving team.
*/ */
@Override @Override
public void mergeTeams(Team recevingTeam, Team supplyingTeam) throws ModelException, MustNotImplementException { public void mergeTeams(Team receivingTeam, Team supplyingTeam) throws ModelException, MustNotImplementException {
Team.mergeTeams(receivingTeam, supplyingTeam);
} }
} }

View File

@@ -1,37 +1,13 @@
package worms.model; package worms.model;
import static java.lang.Math.*;
public class Food extends GameObject { public class Food extends GameObject {
public Food(World world, double[] location) { public Food(World world, double[] location) {
setWorld(world); setWorld(world);
setLocation(location); setLocation(location);
}
// region shape setRadius(0.2);
//===================================================================================
private final double radius = 0.20;
@Override
public double getRadius() {
return this.radius;
}
@Override
public void setMass(double radius) {
final double rho = 150; final double rho = 150;
this.mass = rho * (4/3 * PI * pow(radius, 3)); setMass(getRadius(), rho);
} }
//===================================================================================
// endregion
// region eat
//===================================================================================
//===================================================================================
// endregion
} }

View File

@@ -1,13 +1,22 @@
package worms.model; package worms.model;
import be.kuleuven.cs.som.annotate.Raw;
import worms.util.Coordinate; import worms.util.Coordinate;
import static java.lang.Math.PI;
import static java.lang.Math.pow;
import static java.lang.Math.round;
public abstract class GameObject { public abstract class GameObject {
private World world; /**
*
* @param world ...
* @return ...
*/
public static boolean isValidWorld(World world) {
return !world.hasActiveGame() && !world.isTerminated();
}
/** /**
* *
* @return World * @return World
@@ -16,13 +25,21 @@ public abstract class GameObject {
return this.world; return this.world;
} }
public void setWorld(World world) { public void setWorld(World world) {
if (world == null) {
this.world = null;
return;
}
if (!isValidWorld(world)) throw new IllegalArgumentException();
this.world = world; this.world = world;
} }
private World world;
/** /**
* *
* @return * @return ...
*/ */
public boolean isTerminated() { public boolean isTerminated() {
return this.terminated; return this.terminated;
@@ -38,7 +55,6 @@ public abstract class GameObject {
/** /**
* this variable contains the location of the worm (a Coordinate) * this variable contains the location of the worm (a Coordinate)
*/ */
@@ -95,10 +111,17 @@ public abstract class GameObject {
public double getRadius(){ public double getRadius(){
return this.radius; return this.radius;
} }
//abstract void setRadius(double radius); void setRadius(double radius) {
this.radius = radius;
}
boolean isValidLocation(Coordinate location) { boolean isValidLocation(Coordinate location) {
double radius = getRadius(); double radius = getRadius();
if (world == null) {
return !Double.isNaN(location.getCoordinateX()) &&
!Double.isNaN(location.getCoordinateY());
}
return !Double.isNaN(location.getCoordinateX()) && return !Double.isNaN(location.getCoordinateX()) &&
!Double.isNaN(location.getCoordinateY()) && !Double.isNaN(location.getCoordinateY()) &&
!(location.getCoordinateX() - radius < 0) && !(location.getCoordinateX() - radius < 0) &&
@@ -114,6 +137,16 @@ public abstract class GameObject {
return this.mass; return this.mass;
} }
@Raw /**
abstract void setMass(double radius); * set the mass of the worm to the given mass (dependent on the radius)
*
* @param radius
* part of the formula to calculate the mass
* @post the new mass of the worm is equal to
* rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
* |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
*/
void setMass(double radius, double rho) {
this.mass = (double) round(rho * (4.0 / 3.0 * PI * pow(radius, 3)));
}
} }

View File

@@ -33,6 +33,7 @@ public class Team {
for (Worm w: worm) { for (Worm w: worm) {
if (w != null && canHaveAsWorm(w)) { if (w != null && canHaveAsWorm(w)) {
getAllWormsOfTeam().add(w); getAllWormsOfTeam().add(w);
w.setTeam(this);
} }
else { else {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@@ -77,6 +78,7 @@ public class Team {
for (Worm w: worm) { for (Worm w: worm) {
if (w == null) throw new IllegalArgumentException(); if (w == null) throw new IllegalArgumentException();
getAllWormsOfTeam().remove(w); getAllWormsOfTeam().remove(w);
w.setTeam(null);
} }
} }
@@ -95,6 +97,10 @@ public class Team {
return wormCollection; return wormCollection;
} }
public int getNbWorms() {
return getAllWormsOfTeam().size();
}
/** /**
* @param receivingTeam ... * @param receivingTeam ...
* @param supplyingTeam ... * @param supplyingTeam ...
@@ -104,7 +110,7 @@ public class Team {
* @post ... * @post ...
* |supplyingTeam.isTerminated() == true * |supplyingTeam.isTerminated() == true
*/ */
public void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException { public static void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException {
if (receivingTeam == null || supplyingTeam == null || receivingTeam.equals(supplyingTeam) ) { if (receivingTeam == null || supplyingTeam == null || receivingTeam.equals(supplyingTeam) ) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();

View File

@@ -216,6 +216,8 @@ public class World {
// region objects // region objects
//=================================================================================== //===================================================================================
public Collection<Object> getAllItems() { public Collection<Object> getAllItems() {
Set<Object> all = new HashSet<Object>(); Set<Object> all = new HashSet<Object>();
all.add(getWormList()); all.add(getWormList());
@@ -249,7 +251,7 @@ public class World {
if (!getWormList().remove(worm)) throw new IllegalArgumentException(); if (!getWormList().remove(worm)) throw new IllegalArgumentException();
} }
private List<Worm> getWormList() { public List<Worm> getWormList() {
return this.wormList; return this.wormList;
} }

View File

@@ -3,6 +3,7 @@ package worms.model;
import be.kuleuven.cs.som.annotate.*; import be.kuleuven.cs.som.annotate.*;
import worms.util.Coordinate; import worms.util.Coordinate;
import worms.util.IllegalNameException; import worms.util.IllegalNameException;
import worms.util.MustNotImplementException;
import static java.lang.Math.*; import static java.lang.Math.*;
@@ -104,7 +105,7 @@ public class Worm extends GameObject {
public Worm(Coordinate 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))
throw new IllegalArgumentException("Illegal value for location"); throw new IllegalArgumentException("Illegal value for location");
setLocation(location); setLocation(location);
@@ -116,7 +117,6 @@ public class Worm extends GameObject {
if (!canHaveAsRadius(radius)) if (!canHaveAsRadius(radius))
throw new IllegalArgumentException("Invalid radius"); throw new IllegalArgumentException("Invalid radius");
*/
setRadius(radius); setRadius(radius);
setActionPoints(getMaxActionPoints()); setActionPoints(getMaxActionPoints());
@@ -126,10 +126,8 @@ public class Worm extends GameObject {
throw new IllegalNameException(validName, name); throw new IllegalNameException(validName, name);
this.name = name; this.name = name;
this.minRadius= 0.25; long startHitPoints = 1000 + (new Random().nextLong() * (2000 - 1000));
setHitPoints(startHitPoints);
/*long startHitPoints = 1000 + (new Random().nextLong() * (2000 - 1000));
setHitPoints(startHitPoints);*/
} }
//=================================================================================== //===================================================================================
@@ -220,7 +218,8 @@ public class Worm extends GameObject {
throw new IllegalArgumentException("Invalid radius"); throw new IllegalArgumentException("Invalid radius");
this.radius = radius; this.radius = radius;
setMass(radius); final double rho = 1062;
setMass(radius, rho);
} }
/** /**
@@ -261,28 +260,6 @@ public class Worm extends GameObject {
return this.mass; return this.mass;
} }
/**
* this variable contains the mass of the worm
*/
private double mass;
/**
* set the mass of the worm to the given mass (dependent on the radius)
*
* @param radius
* part of the formula to calculate the mass
* @post the new mass of the worm is equal to
* rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
* |new.getMass() == rho * (4 / 3 * Math.PI * Math.pow(radius, 3))
*/
@Raw @Override
protected void setMass(double radius) {
final double rho = 1062.0;
double mass = round(rho * (4.0 / 3.0 * PI * pow(radius, 3)));
this.mass = mass;
setMaxActionPoints(mass);
}
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -508,19 +485,21 @@ public class Worm extends GameObject {
* then the current value of action point * then the current value of action point
* |NumberSteps < 0 || cost > this.actionPoints * |NumberSteps < 0 || cost > this.actionPoints
*/ */
public void move(int numberSteps) throws IllegalArgumentException { public void move() throws IllegalArgumentException {
if (numberSteps < 0)
throw new IllegalArgumentException();
long cost = (long) ceil(abs(cos(getOrientation())) + abs(4 * sin(getOrientation()))) * numberSteps;
double newDirection = getFurthestLocationDirection();
Coordinate newLocation = getFurthestLocationInDirection(newDirection, this.getRadius());
double distance = getDistance(this.getLocation(), newLocation);
long cost = (long) ceil(abs(distance * cos(newDirection)) + abs(4 * distance * sin(newDirection)));
if (cost > getActionPoints()) if (cost > getActionPoints())
throw new IllegalArgumentException(); throw new IllegalArgumentException();
double distanceX = getRadius() * cos(getOrientation()); double distanceX = getRadius() * cos(getOrientation());
double distanceY = getRadius() * sin(getOrientation()); double distanceY = getRadius() * sin(getOrientation());
setLocation(Coordinate.create(getLocation().getCoordinateX() + numberSteps * distanceX, setLocation(newLocation);
getLocation().getCoordinateY() + numberSteps * distanceY));
subtractActionPoints(cost); subtractActionPoints(cost);
} }
@@ -528,8 +507,51 @@ public class Worm extends GameObject {
* *
* @return * @return
*/ */
public Coordinate getFurthestLocationInDirection() { public Coordinate getFurthestLocationInDirection(double direction, double maxDistance) {
return null; Coordinate currentLocation = getLocation();
double radius = getRadius();
World world = getWorld();
double step = Math.sqrt(Math.pow(world.getWidth(), 2) + Math.pow(world.getHeight(), 2));
Coordinate nextLoc, prevLoc;
nextLoc = prevLoc = currentLocation;
while(getDistance(currentLocation, nextLoc) < maxDistance) {
prevLoc = nextLoc;
nextLoc = Coordinate.create(nextLoc.getCoordinateX() + step * Math.cos(direction),
nextLoc.getCoordinateY() + step * Math.sin(direction));
if (!world.isPassable(nextLoc.toArray(), radius)) {
while (!world.isPassable(nextLoc.toArray(), radius)) {
nextLoc = Coordinate.create(nextLoc.getCoordinateX() - 0.1 * Math.cos(direction),
nextLoc.getCoordinateY() - 0.1 * Math.sin(direction));
}
break;
}
}
if (getDistance(currentLocation, prevLoc) > getDistance(currentLocation, nextLoc)) return prevLoc;
return nextLoc;
}
public double getFurthestLocationDirection() {
Coordinate location = getLocation();
double direction = getOrientation();
double minDirection = direction - 0.7875;
double maxDirection = direction + 0.7875;
double maxLocDirection = maxDirection;
Coordinate maxLoc = getFurthestLocationInDirection(maxDirection, this.getRadius());
for (; minDirection < maxDirection; minDirection += 0.0175) {
Coordinate tempLoc = getFurthestLocationInDirection(minDirection, this.getRadius());
if (getDistance(location, tempLoc) > getDistance(location, maxLoc)) {
maxLoc = tempLoc;
maxLocDirection = minDirection;
}
}
return maxLocDirection;
}
public double getDistance(Coordinate start, Coordinate end) {
return Math.sqrt(Math.pow(Math.abs(start.getCoordinateX() - end.getCoordinateX()), 2) +
Math.pow(Math.abs(start.getCoordinateY() - end.getCoordinateY()), 2));
} }
public void collision(Worm basicWorm, Worm... worm) { public void collision(Worm basicWorm, Worm... worm) {
@@ -721,7 +743,7 @@ public class Worm extends GameObject {
Worm attackedWorm; Worm attackedWorm;
Worm attacker; Worm attacker;
Random rand = new Random(); Random rand = new Random();
int coin = rand.nextInt((1 - 0) + 1) + 0; int coin = rand.nextInt(2);
if (coin == 1) { if (coin == 1) {
attackedWorm = worm1; attackedWorm = worm1;
attacker = wormB; attacker = wormB;
@@ -818,4 +840,11 @@ public class Worm extends GameObject {
// =================================================================================== // ===================================================================================
// endregion // endregion
public void eat() {
throw new IllegalStateException();
}
} }

View File

@@ -1,94 +1,94 @@
package worms.model; //package worms.model;
//
import org.junit.jupiter.api.Test; //import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll; //import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest; //import org.junit.jupiter.api.RepeatedTest;
import static org.junit.jupiter.api.Assertions.*; //import static org.junit.jupiter.api.Assertions.*;
import worms.util.Coordinate; //import worms.util.Coordinate;
//
import java.util.Random; //import java.util.Random;
//
class WormTest { //class WormTest {
//
private static Worm worm; // private static Worm worm;
private static Random r = new Random(); // private static Random r = new Random();
//
@BeforeAll // @BeforeAll
static void setUp() { // static void setUp() {
worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); // worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1);
} // }
//
@RepeatedTest(5000) // @RepeatedTest(5000)
void move() { // void move() {
worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); // worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1);
int steps = r.nextInt(5101) - 100; // int steps = r.nextInt(5101) - 100;
double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation()); // double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation());
double minAngle = worm.getOrientation(); // double minAngle = worm.getOrientation();
double combAngle = maxAngle + minAngle; // double combAngle = maxAngle + minAngle;
double angle = r.nextDouble() * combAngle - minAngle; // double angle = r.nextDouble() * combAngle - minAngle;
worm.turn(angle); // worm.turn(angle);
long cost = (long) Math.ceil(Math.abs(Math.cos(worm.getOrientation())) + Math.abs(4 * Math.sin(worm.getOrientation()))) * steps; // long cost = (long) Math.ceil(Math.abs(Math.cos(worm.getOrientation())) + Math.abs(4 * Math.sin(worm.getOrientation()))) * steps;
if (steps < 0 || cost > worm.getActionPoints()) { // if (steps < 0 || cost > worm.getActionPoints()) {
assertThrows(IllegalArgumentException.class, () -> worm.move(steps)); // assertThrows(IllegalArgumentException.class, () -> worm.move(steps));
} // }
else { // else {
//
double newLocX = worm.getLocation().item1 + steps * worm.getRadius() * Math.cos(worm.getOrientation()); // double newLocX = worm.getLocation().item1 + steps * worm.getRadius() * Math.cos(worm.getOrientation());
double newLocY = worm.getLocation().item2 + steps * worm.getRadius() * Math.sin(worm.getOrientation()); // double newLocY = worm.getLocation().item2 + steps * worm.getRadius() * Math.sin(worm.getOrientation());
worm.move(steps); // worm.move(steps);
assertEquals(Coordinate.create(newLocX, newLocY), worm.getLocation()); // assertEquals(Coordinate.create(newLocX, newLocY), worm.getLocation());
} // }
} // }
//
@RepeatedTest(5000) // @RepeatedTest(5000)
void turn() { // void turn() {
double angle = r.nextDouble() * 4 * Math.PI - 2 * Math.PI; // double angle = r.nextDouble() * 4 * Math.PI - 2 * Math.PI;
double oldAngle = worm.getOrientation(); // double oldAngle = worm.getOrientation();
//
if (!Worm.isValidOrientation(oldAngle + angle) || worm.getActionPoints() - (long) Math.ceil(Math.toDegrees(angle) / 6) < 0) { // if (!Worm.isValidOrientation(oldAngle + angle) || worm.getActionPoints() - (long) Math.ceil(Math.toDegrees(angle) / 6) < 0) {
assertThrows(AssertionError.class, () -> worm.turn(angle)); // assertThrows(AssertionError.class, () -> worm.turn(angle));
worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); // worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1);
} // }
else { // else {
worm.turn(angle); // worm.turn(angle);
assertEquals(oldAngle + angle, worm.getOrientation()); // assertEquals(oldAngle + angle, worm.getOrientation());
} // }
//
} // }
//
@Test // @Test
void turnInvalidValue() { // void turnInvalidValue() {
assertThrows(AssertionError.class, () -> worm.turn(Double.NaN)); // assertThrows(AssertionError.class, () -> worm.turn(Double.NaN));
} // }
//
@RepeatedTest(5000) // @RepeatedTest(5000)
void jump() { // void jump() {
//
worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); // worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1);
double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation()); // double maxAngle = Math.abs(2 * Math.PI - worm.getOrientation());
double minAngle = worm.getOrientation(); // double minAngle = worm.getOrientation();
double combAngle = maxAngle + minAngle; // double combAngle = maxAngle + minAngle;
double angle = r.nextDouble() * combAngle - minAngle; // double angle = r.nextDouble() * combAngle - minAngle;
worm.turn(angle); // worm.turn(angle);
int steps = r.nextInt(100); // int steps = r.nextInt(100);
worm.move(steps); // worm.move(steps);
//
if (worm.getOrientation() >= Math.PI || worm.getActionPoints() == 0){ // if (worm.getOrientation() >= Math.PI || worm.getActionPoints() == 0){
assertThrows(IllegalStateException.class, () -> worm.jump()); // assertThrows(IllegalStateException.class, () -> worm.jump());
} // }
else { // else {
double orient = worm.getOrientation(); // double orient = worm.getOrientation();
double force = 5 * worm.getActionPoints() + worm.getMass() * Worm.G; // double force = 5 * worm.getActionPoints() + worm.getMass() * Worm.G;
double v = force / worm.getMass() * Worm.FORCE_TIME; // double v = force / worm.getMass() * Worm.FORCE_TIME;
double newLocX = worm.getLocation().item1 + Math.pow(v, 2) * Math.sin(2 * orient) / Worm.G; // double newLocX = worm.getLocation().item1 + Math.pow(v, 2) * Math.sin(2 * orient) / Worm.G;
worm.jump(); // worm.jump();
assertEquals(Coordinate.create(newLocX, worm.getLocation().getCoordinateY()), worm.getLocation()); // assertEquals(Coordinate.create(newLocX, worm.getLocation().getCoordinateY()), worm.getLocation());
} // }
} // }
@Test // @Test
void doubleJump() { // void doubleJump() {
worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1); // worm = new Worm(Coordinate.create(0.0, 0.0), 0, "Test", 1);
worm.jump(); // worm.jump();
assertThrows(IllegalStateException.class, () -> worm.jump()); // assertThrows(IllegalStateException.class, () -> worm.jump());
} // }
} //}