Documentatie

This commit is contained in:
Leen Dereu
2018-04-16 23:51:23 +02:00
parent 5899928b34
commit 4eacc246c5
4 changed files with 403 additions and 47 deletions

View File

@@ -3,10 +3,20 @@ package worms.model;
import worms.util.Coordinate; import worms.util.Coordinate;
public class Food extends GameObject { public class Food extends GameObject {
/** /**
* *
* @param world * @param world
* @param location * @param location
*
* @post ...
* |setWorld(world)
* @post ...
* |setLocation(location)
* @post ...
* |setOrientation(0.2)
* @post ...
* |setMass(getRadius(), 150)
*/ */
public Food(World world, double[] location) { public Food(World world, double[] location) {
super(world, location, 0.2); super(world, location, 0.2);

View File

@@ -9,6 +9,21 @@ import static java.lang.Math.round;
public abstract class GameObject { public abstract class GameObject {
/**
*
* @param world
* @param location
* @param radius
*
* @post ...
* |setWorld(world)
* @post ...
* |world.add(this)
* @post ...
* |setLocation(location)
* @post ...
* |setRadius(radius)
*/
GameObject(World world, double[] location, double radius) { GameObject(World world, double[] location, double radius) {
setWorld(world); setWorld(world);
world.add(this); world.add(this);
@@ -18,19 +33,33 @@ public abstract class GameObject {
/** /**
* *
* @param world ... * @param world
*
* @return ... * @return ...
* |result == !world.hasActiveGame() && !world.isTerminated()
*/ */
public static boolean isValidWorld(World world) { public static boolean isValidWorld(World world) {
return !world.hasActiveGame() && !world.isTerminated(); return !world.hasActiveGame() && !world.isTerminated();
} }
/** /**
* *
* @return World * @return ...
* |result == this.world
*/ */
public World getWorld() { public World getWorld() {
return this.world; return this.world;
} }
/**
*
* @param world
*
* @post ...
* |if world == null
* | this.world = null
* |else:
* | this.world = world
*/
public void setWorld(World world) { public void setWorld(World world) {
if (world == null) { if (world == null) {
this.world = null; this.world = null;
@@ -40,23 +69,33 @@ public abstract class GameObject {
this.world = world; this.world = world;
} }
/**
*
*/
private World world; private World world;
/** /**
* *
* @return ... * @return ...
* |result == this.terminated
*/ */
public boolean isTerminated() { public boolean isTerminated() {
return this.terminated; return this.terminated;
} }
/** /**
* Terminate the object * @post ...
* |this.terminated = true
* |getWorld().remove(this)
*/ */
public void terminate() { public void terminate() {
this.terminated = true; this.terminated = true;
getWorld().remove(this); getWorld().remove(this);
} }
/**
*
*/
private boolean terminated = false; private boolean terminated = false;
@@ -74,6 +113,12 @@ public abstract class GameObject {
Coordinate getLocation() { Coordinate getLocation() {
return this.location; return this.location;
} }
/**
*
* @return ...
* |result == this.location.toArray()
*/
public double[] getLocationArray() { public double[] getLocationArray() {
return this.location.toArray(); return this.location.toArray();
} }
@@ -97,6 +142,17 @@ public abstract class GameObject {
this.location = locationCoordinate; this.location = locationCoordinate;
} }
/**
*
* @param location
*
* @post ...
* this.location = location
*
* @throws IllegalArgumentException
* ...
* |!isValidLocation(location)
*/
void setLocation(Coordinate location) throws IllegalArgumentException { void setLocation(Coordinate location) throws IllegalArgumentException {
if (!isValidLocation(location)) throw new IllegalArgumentException(); if (!isValidLocation(location)) throw new IllegalArgumentException();
@@ -117,6 +173,14 @@ public abstract class GameObject {
public double getRadius(){ public double getRadius(){
return this.radius; return this.radius;
} }
/**
*
* @param radius
*
* @post ...
* |this.radius = radius
*/
void setRadius(double radius) { void setRadius(double radius) {
if (!canHaveAsRadius(radius)) throw new IllegalArgumentException(); if (!canHaveAsRadius(radius)) throw new IllegalArgumentException();
this.radius = radius; this.radius = radius;
@@ -136,6 +200,21 @@ public abstract class GameObject {
return !Double.isNaN(radius) && radius > 0; return !Double.isNaN(radius) && radius > 0;
} }
/**
*
* @param location
*
* @return ...
* |if (world == null)
* | result == !Double.isNaN(location.getX()) && !Double.isNaN(location.getY())
* |result == !Double.isNaN(location.getX()) &&
* | !Double.isNaN(location.getY()) &&
* | !(location.getX() - radius < 0) &&
* | !(location.getX() + radius > getWorld().getWidth()) &&
* | !(location.getY() + radius > getWorld().getHeight()) &&
* | !(location.getY() - radius < 0 &&
* | !getWorld().isPassable(location))
*/
boolean isValidLocation(Coordinate location) { boolean isValidLocation(Coordinate location) {
double radius = getRadius(); double radius = getRadius();
@@ -153,7 +232,16 @@ public abstract class GameObject {
} }
/**
*
*/
double mass; double mass;
/**
*
* @return ...
* |result == this.mass
*/
public double getMass() { public double getMass() {
return this.mass; return this.mass;
} }

View File

@@ -9,6 +9,15 @@ public class Team {
// region constructor // region constructor
//=================================================================================== //===================================================================================
/**
*
* @param name
*
* @post ...
* |setName(name)
* @post ...
* |wormCollection = new Treeset<>(new TeamComparator())
*/
public Team (String name) { public Team (String name) {
setName(name); setName(name);
this.wormCollection = new TreeSet<>(new TeamComparator()); this.wormCollection = new TreeSet<>(new TeamComparator());
@@ -24,9 +33,11 @@ public class Team {
* @param worm * @param worm
* *
* @post ... * @post ...
* |new.team = old.team + Worm... worm * |new.team = old.team + worm
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* ...
* |worm == null || ! canHaveAsWorm(worm)
*/ */
public void addWorm(Worm... worm) throws IllegalArgumentException { public void addWorm(Worm... worm) throws IllegalArgumentException {
if (worm == null) throw new IllegalArgumentException(); if (worm == null) throw new IllegalArgumentException();
@@ -42,12 +53,17 @@ public class Team {
} }
/** /**
* @param worm ..
* *
* @return * @param worm
* |for each i in [0,team.size()[ *
* | result == (worm.getMass() > getMinMassTeam()/2 && worm.getMass() < 2 * getMaxMassTeam() * @return ...
* | && worm.isAlive() && (team.get(i)).getName() != worm.getName()) * |if (worm.getMass() >= getMinMassTeam() / 2 && worm.getMass() <= 2 * getMinMassTeam() && !worm.isTerminated())
* | for (Worm elWorm : worms)
* | if (! elWorm.getName().equals(worm.getName()))
* | result == true
* @throws IllegalArgumentException
* ...
* |worm == null
*/ */
private boolean canHaveAsWorm(Worm worm) throws IllegalArgumentException { private boolean canHaveAsWorm(Worm worm) throws IllegalArgumentException {
@@ -67,10 +83,17 @@ public class Team {
} }
/** /**
*
* @param worm * @param worm
* *
* @post ... * @post ...
* |(new.team).contains(Worm... worm) == false * |getAllwormsOfTeam().remove(worm)
* @post ...
* |worm.setTeam(null)
*
* @throws IllegalArgumentException
* ...
* |worm == null
*/ */
public void removeWormsFromTeam(Worm... worm) throws IllegalArgumentException { public void removeWormsFromTeam(Worm... worm) throws IllegalArgumentException {
@@ -83,32 +106,50 @@ public class Team {
} }
/** /**
* @param worm ... *
* @param worm
* *
* @return ... * @return ...
* |result == team.contains(worm) * |result == getAllWormsOfTeam().contains(worm)
*
* @throws IllegalArgumentException
* ...
* |worm == null
*/ */
public boolean containsWorm(Worm worm) throws IllegalArgumentException { public boolean containsWorm(Worm worm) throws IllegalArgumentException {
if (worm == null) throw new IllegalArgumentException(); if (worm == null) throw new IllegalArgumentException();
return getAllWormsOfTeam().contains(worm); return getAllWormsOfTeam().contains(worm);
} }
/**
*
* @return ...
* |wormCollection
*/
public Collection<Worm> getAllWormsOfTeam() { public Collection<Worm> getAllWormsOfTeam() {
return wormCollection; return wormCollection;
} }
/**
*
* @return ...
* |getAllWormsOfTeam().size()
*/
public int getNbWorms() { public int getNbWorms() {
return getAllWormsOfTeam().size(); return getAllWormsOfTeam().size();
} }
/** /**
* @param receivingTeam ...
* @param supplyingTeam ...
* *
* @post ... (NOG NIET ZEKER VAN) * @param receivingTeam
* |recevingTeam == receivingTeam + supplyingTeam * @param supplyingTeam
*
* @post ... * @post ...
* |supplyingTeam.isTerminated() == true * |receivingTeam.addWorm(supplyingTeam)
*
* @throws IllegalArgumentException
* ...
* |receivingTeam == null || supplyingTeam == null || receivingTeam.equals(supplyingTeam)
*/ */
public static void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException { public static void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException {
@@ -121,6 +162,9 @@ public class Team {
supplyingTeam.removeWormsFromTeam(supWorms); supplyingTeam.removeWormsFromTeam(supWorms);
} }
/**
*
*/
private Collection<Worm> wormCollection; private Collection<Worm> wormCollection;
//=================================================================================== //===================================================================================
@@ -130,7 +174,13 @@ public class Team {
//=================================================================================== //===================================================================================
/** /**
*
* @return ... * @return ...
* |(getAllWormsOfTeam().stream().min(Comparator.comparingDouble(Worm::getMass)).orElse(null)).getMass()
*
* @throws IllegalStateException
* ...
* |minMass == null
*/ */
private double getMinMassTeam() throws IllegalStateException { private double getMinMassTeam() throws IllegalStateException {
Worm minMass = getAllWormsOfTeam().stream().min(Comparator.comparingDouble(Worm::getMass)).orElse(null); Worm minMass = getAllWormsOfTeam().stream().min(Comparator.comparingDouble(Worm::getMass)).orElse(null);
@@ -147,6 +197,7 @@ public class Team {
//=================================================================================== //===================================================================================
/** /**
*
* @return ... * @return ...
* |result == this.name * |result == this.name
*/ */
@@ -154,14 +205,16 @@ public class Team {
return this.name; return this.name;
} }
/** /**
* @param name ...
* *
* @post |new.getName() == name * @param name
*
* @post ...
* |new.getName() == name
* *
* @throws IllegalNameException * @throws IllegalNameException
* |! isValidName(name) * ...
* |Worm.isValidName(name) != -1
*/ */
public void setName(String name) throws IllegalNameException { public void setName(String name) throws IllegalNameException {
@@ -172,6 +225,9 @@ public class Team {
this.name = name; this.name = name;
} }
/**
*
*/
private String name; private String name;
//=================================================================================== //===================================================================================
@@ -181,9 +237,8 @@ public class Team {
//=================================================================================== //===================================================================================
/** /**
* * @post ...
* @post * |new.terminatd = true
* |new.terminate == true
*/ */
public void terminate() { public void terminate() {
this.terminated = true; this.terminated = true;
@@ -192,15 +247,15 @@ public class Team {
/** /**
* *
* @return ... * @return ...
* |if terminate == true * |result == this.terminated
* | result == true
* |else
* | result == false
*/ */
public boolean isTerminated() { public boolean isTerminated() {
return this.terminated; return this.terminated;
} }
/**
*
*/
private boolean terminated = false; private boolean terminated = false;
//=================================================================================== //===================================================================================

View File

@@ -6,6 +6,27 @@ import java.util.*;
public class World { public class World {
/**
*
* @param width
* @param height
* @param map
*
* @post ...
* new.width = width
* @post ...
* new.height = height
* @post ...
* new.map = map
* @post ...
* new.legthX = width / map[0].length
* @post ...
* new.lengthY = height / map.length
* @post ...
* new.game = false
* @post ...
* new.gameObjets = new HashSet<>()
*/
public World(double width, double height, boolean[][] map) { public World(double width, double height, boolean[][] map) {
if (!isValidDimension(width) || !isValidDimension(height)) throw new IllegalArgumentException(); if (!isValidDimension(width) || !isValidDimension(height)) throw new IllegalArgumentException();
@@ -84,17 +105,43 @@ public class World {
// region Dimensions // region Dimensions
//=================================================================================== //===================================================================================
/**
*
* @param dimension
*
* @return ...
* |result == dimension >= 0.0 && dimension <= Double.MAX_VALUE
*/
public static boolean isValidDimension(double dimension) { public static boolean isValidDimension(double dimension) {
return dimension >= 0.0 && dimension <= Double.MAX_VALUE; return dimension >= 0.0 && dimension <= Double.MAX_VALUE;
} }
/**
*
* @return ...
* |result == width
*/
public double getWidth() { public double getWidth() {
return width; return width;
} }
/**
*
* @return ...
* |result == height
*/
public double getHeight() { public double getHeight() {
return height; return height;
} }
/**
*
*/
private final double width; private final double width;
/**
*
*/
private final double height; private final double height;
// =================================================================================== // ===================================================================================
@@ -104,20 +151,41 @@ public class World {
// region map // region map
//=================================================================================== //===================================================================================
/**
*
* @return ...
* |result == map
*/
public boolean[][] getMap() { public boolean[][] getMap() {
return map; return map;
} }
/**
*
* @return ...
* result = this.terminated
*/
public boolean isTerminated() { public boolean isTerminated() {
return this.terminated; return this.terminated;
} }
/**
* @post ...
* |this.terminated = true
*/
public void terminate() { public void terminate() {
this.terminated = true; this.terminated = true;
} }
/**
*
*/
private final boolean[][] map; private final boolean[][] map;
/**
*
*/
private boolean terminated = false; private boolean terminated = false;
// =================================================================================== // ===================================================================================
@@ -128,8 +196,14 @@ public class World {
/** /**
* *
* @param location ... * @param location
*
* @return ... * @return ...
* |false if:
* | Math.floor(location[0] / lengthX) >= getMap()[0].length ||
* | location[0] < 0.0 || location[1] / lengthY >= getMap().length || location[1] < 0.0
* |various:
* | result == this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)]
*/ */
public boolean isPassable(double[] location) { public boolean isPassable(double[] location) {
@@ -140,14 +214,44 @@ public class World {
return this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)]; return this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)];
} }
/**
*
* @param location
*
* @return ...
* |result == this.isPassable(location.toArray())
*/
public boolean isPassable(Coordinate location) { public boolean isPassable(Coordinate location) {
return this.isPassable(location.toArray()); return this.isPassable(location.toArray());
} }
/**
*
* @param location
* @param radius
*
* @return ...
* |result == this.isPassable(location.toArray(), radius)
*/
public boolean isPassable(Coordinate location, double radius) { public boolean isPassable(Coordinate location, double radius) {
return this.isPassable(location.toArray(), radius); return this.isPassable(location.toArray(), radius);
} }
/**
*
* @param center
* @param radius
*
* @return ...
* |for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180)
* | double lenX = center[0] + radius * Math.cos(i)
* | double lenY = center[1] + radius * Math.sin(i)
* | if (i < 1.58 && i > 1.57)
* | lenY -= 0.0000000001
* | else if (i < 0.79 && i > 0.78 )
* | lenX -= 0.0000000001
* |result == isPassable(lenX, lenY)
*/
public boolean isPassable(double[] center, double radius) { public boolean isPassable(double[] center, double radius) {
for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180) { for (double i = 0; i < 2 * Math.PI; i += Math.PI / 180) {
@@ -171,6 +275,7 @@ public class World {
/** /**
* *
* @return ... * @return ...
* |result == isAdjacent(Coordinate.create(center), radius)
*/ */
public boolean isAdjacent(double[] center, double radius) { public boolean isAdjacent(double[] center, double radius) {
return isAdjacent(Coordinate.create(center), radius); return isAdjacent(Coordinate.create(center), radius);
@@ -180,7 +285,9 @@ public class World {
* *
* @param center ... * @param center ...
* @param radius ... * @param radius ...
*
* @return ... * @return ...
* |result == isPassable(center, radius) && !isPassable(center, radius + maxDistance + 0.00001)
*/ */
public boolean isAdjacent(Coordinate center, double radius) { public boolean isAdjacent(Coordinate center, double radius) {
@@ -191,20 +298,47 @@ public class World {
return isPassable(center, radius) && !isPassable(center, radius + maxDistance + 0.00001); return isPassable(center, radius) && !isPassable(center, radius + maxDistance + 0.00001);
} }
/**
*
* @return ...
* |result == this.lengtX
*/
public double getLengthX() { public double getLengthX() {
return this.lengthX; return this.lengthX;
} }
/**
*
* @return ...
* |result == this.lengthY
*/
public double getLengthY() { public double getLengthY() {
return this.lengthY; return this.lengthY;
} }
/**
*
*/
private final double lengthX; private final double lengthX;
/**
*
*/
private final double lengthY; private final double lengthY;
/** /**
* *
* @param x ... * @param x
* @param y ... * @param y
*
* @return ... * @return ...
* |xCoord = Math.floor(x / lengthX);
* |yCoord = map.length - 1 - Math.floor(y / lengthY);
* |if (yCoord < 0 || yCoord >= map.length)
* | return true
* |if (xCoord < 0 || xCoord >= map[0].length)
* | return true
* |return getMap()[yCoord][xCoord]
*/ */
private boolean isPassable(double x, double y) { private boolean isPassable(double x, double y) {
@@ -230,11 +364,21 @@ public class World {
// region objects // region objects
//=================================================================================== //===================================================================================
/**
*
* @return ...
* |result == new ArrayList<>(getGameObjects())
*/
public Collection<Object> getAllItems() { public Collection<Object> getAllItems() {
return new ArrayList<>(getGameObjects()); return new ArrayList<>(getGameObjects());
} }
/**
*
* @return ...
* |result == teams
*/
public Set<Team> getAllTeams() { public Set<Team> getAllTeams() {
Set<Team> teams = new HashSet<Team>(); Set<Team> teams = new HashSet<Team>();
@@ -244,24 +388,66 @@ public class World {
return teams; return teams;
} }
/**
*
* @return ...
* |result == this.gameObjects
*/
public Set<GameObject> getGameObjects() { public Set<GameObject> getGameObjects() {
return this.gameObjects; return this.gameObjects;
} }
/**
*
* @param obj
*
* @post obj.setWorld(this)
*
* @throws IllegalStateException
* ...
* |hasActiveGame()
* @throws IllegalArgumentException
* ...
* |!getGameObjects().add(obj)
*/
public void add(GameObject obj) throws NullPointerException { public void add(GameObject obj) throws NullPointerException {
if (hasActiveGame()) throw new IllegalStateException(); if (hasActiveGame()) throw new IllegalStateException();
if (!getGameObjects().add(obj)) throw new IllegalArgumentException(); if (!getGameObjects().add(obj)) throw new IllegalArgumentException();
obj.setWorld(this); obj.setWorld(this);
} }
/**
*
* @param obj
*
* @throws IllegalArgumentException
* ...
* |!getGameObjects().remove(obj)
*/
public void remove(GameObject obj) throws IllegalArgumentException { public void remove(GameObject obj) throws IllegalArgumentException {
if (!getGameObjects().remove(obj)) throw new IllegalArgumentException(); if (!getGameObjects().remove(obj)) throw new IllegalArgumentException();
} }
/**
*
* @param obj
*
* @return ...
* |getGameObjects().contains(obj)
*
* @throws NullPointerException
*/
public boolean hasAsGameObject(GameObject obj) throws NullPointerException { public boolean hasAsGameObject(GameObject obj) throws NullPointerException {
return getGameObjects().contains(obj); return getGameObjects().contains(obj);
} }
/**
*
* @return ...
* |lis.add(worm) for each worm
* |result == list (ArrayList<>())
*/
public List<Worm> getWormList() { public List<Worm> getWormList() {
List<Worm> list = new ArrayList<>(); List<Worm> list = new ArrayList<>();
@@ -273,6 +459,12 @@ public class World {
return list; return list;
} }
/**
*
* @return ...
* |list.add(food) for each food
* |result == list (ArrayList<>())
*/
public List<Food> getFoodList() { public List<Food> getFoodList() {
List<Food> list = new ArrayList<>(); List<Food> list = new ArrayList<>();
for (GameObject x : getGameObjects()) { for (GameObject x : getGameObjects()) {
@@ -283,6 +475,14 @@ public class World {
return list; return list;
} }
/**
*
* @param cl
*
* @return ...
* |list.add(gameObject) for each gameObject
* |result == list (ArrayList<>())
*/
public List<GameObject> getGameObjectList(Class cl) { public List<GameObject> getGameObjectList(Class cl) {
List<GameObject> list = new ArrayList<>(); List<GameObject> list = new ArrayList<>();
for (GameObject x : getGameObjects()) { for (GameObject x : getGameObjects()) {
@@ -293,6 +493,9 @@ public class World {
return list; return list;
} }
/**
*
*/
private Set<GameObject> gameObjects; private Set<GameObject> gameObjects;