diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index e7ac7a8..e466795 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -22,15 +22,34 @@ public class Team { // region changesTeam //=================================================================================== - public void addWormsToTeam(Worm... worm) { + /** + * @param worm + * + * @post ... + * |new.team = old.team + Worm... worm + * + * @throws IllegalArgumentException + */ + public void addWormsToTeam(Worm... worm) throws IllegalArgumentException { for (Worm w: worm) { if (canHaveAsWorm(w)) { team.add(w); sortList(team); } + else { + throw new IllegalArgumentException(); + } } } + /** + * @param worm + * + * @return ... + * |for each i in [0,team.size()[ + * | result == (worm.getMass() > getMinMassTeam()/2 && worm.getMass() < 2 * getMaxMassTeam() + * | && worm.isAlive() && (team.get(i)).getName() != worm.getName()) + */ public boolean canHaveAsWorm(Worm worm) { if (worm.getMass() > getMinMassTeam()/2 && worm.getMass() < 2 * getMaxMassTeam() && worm.isAlive()) { @@ -43,17 +62,35 @@ public class Team { return false; } - public void sortList(ArrayList list) { - Collections.sort(list, + /** + * @param team + * + * @post ... + * | + */ + public void sortList(ArrayList team) { + Collections.sort(team, (el1, el2) -> el1.getName().compareToIgnoreCase(el2.getName())); } + /** + * @param worm + * + * @post ... + * |(new.team).contains(Worm... worm) == false + */ public void removeWormsFromTeam(Worm... worm) { for (Worm w: worm) { team.remove(w); } } + /** + * @param worm + * + * @return ... + * |result == team.contains(worm) + */ public boolean containsWorm(Worm worm) { if (team.contains(worm)) { return true; @@ -65,6 +102,15 @@ public class Team { return team; } + /** + * @param recevingTeam + * @param supplyingTeam + * + * @post ... (NOG NIET ZEKER VAN) + * |recevingTeam == receivingTeam + supplyingTeam + * @post ... + * |supplyingTeam.isTerminated() == true + */ public void mergeTeams(Team recevingTeam, Team supplyingTeam) { for (int i = 0; i < (supplyingTeam.team).size(); i++) { addWormsToTeam((supplyingTeam.team).get(i)); @@ -80,32 +126,52 @@ public class Team { // region mass //=================================================================================== + /** + * @return ... + * |result == this.minMassTeam + */ public double getMinMassTeam() { return this.minMassTeam; } + /** + * @post ... + * |for each worm in team + * | if (minMassTeam > worm.getMass()) + * | new.getMinMassTeam() == worm.getMass() + */ public void setMinMassTeam() { - for (int i = 0; i < team.size() - 1; i++) { - Worm wormA = team.get(i); - Worm wormB = team.get(i+1); - if (wormA.getMass() < wormB.getMass()) { - minMassTeam = wormA.getMass(); + minMassTeam = (team.get(0)).getMass(); + for (int i = 1; i < team.size() - 1; i++) { + Worm worm = team.get(i); + if (worm.getMass() < minMassTeam) { + minMassTeam = worm.getMass(); } } } private double minMassTeam; + /** + * @return ... + * |result == this.maxMassTeam + */ public double getMaxMassTeam() { return this.maxMassTeam; } + /** + * @post ... + * |for each worm in team + * | if (maxMassTeam < worm.getMass()) + * | new.getMaxMassTeam() == worm.getMass() + */ public void setMaxMassTeam() { - for (int i = 0; i < team.size() - 1; i++) { - Worm wormA = team.get(i); - Worm wormB = team.get(i+1); - if (wormA.getMass() > wormB.getMass()) { - maxMassTeam = wormA.getMass(); + maxMassTeam = (team.get(0)).getMass(); + for (int i = 1; i < team.size() - 1; i++) { + Worm worm = team.get(i); + if (worm.getMass() > maxMassTeam) { + minMassTeam = worm.getMass(); } } } @@ -118,12 +184,24 @@ public class Team { // region name //=================================================================================== + /** + * @return ... + * |result == this.name + */ public String getName() { return this.name; } + /** + * @param name + * + * @return ... + * |for (i = 0; i < name.length(); i++) + * | result == (name.length() > 2 && Character.isUpperCase(name.charAt(0) + * | && Character.isLetter(name.charAt(i)) && + * | allowedCharacters.indexOf(name.charAt(i))) + */ public static int isValidName (String name) { - if (name.length() < 2 || !Character.isUpperCase(name.charAt(0))) { return 0; } @@ -138,7 +216,15 @@ public class Team { } return -1; } - + + /** + * @param name + * + * @post |new.getName() == name + * + * @throws IllegalNameException + * |! isValidName(name) + */ public void setName(String name) throws IllegalNameException { int validTeamName = isValidName(name); @@ -156,16 +242,34 @@ public class Team { // region terminate //=================================================================================== - //TODO region terminate - + /** + * @param team + * + * @post ... + * |new.terminate == true + */ public void terminate(Team team) { - + terminate = true; } + /** + * @param team + * + * @return ... + * |if terminate == true + * | result == true + * |else + * | result == false + */ public boolean isTerminated(Team team) { + if (terminate == true) { + return true; + } return false; } + private boolean terminate = false; + //=================================================================================== // endregion } diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 8e99cc6..f075881 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -345,7 +345,6 @@ public class Worm { */ @Raw private void setMass(double radius) { - final double rho = 1062.0; double mass = round(rho * (4.0 / 3.0 * PI * pow(radius, 3))); this.mass = mass; @@ -522,7 +521,7 @@ public class Worm { * @param name * the new name for the worm * @post the new name of the worm is equal to the given name - * |new.GetName() == name + * |new.getName() == name * @throws IllegalNameException * the given name is not a valid name for any worm * |! isValidName(name) @@ -831,16 +830,22 @@ public class Worm { * terminate the worm */ public void terminate() { - + terminate = true; + setLocation(Coordinate.create(Double.NaN, Double.NaN)); } + public boolean isAlive() { + if (terminate == false) { + return true; + } + return false; + } + + private boolean terminate = false; + //=================================================================================== // endregion - public boolean isAlive() { - return false; - } - // region falling //===================================================================================