diff --git a/OGP1718-Worms/src/Main.java b/OGP1718-Worms/src/Main.java index 08d90ba..132fd9b 100644 --- a/OGP1718-Worms/src/Main.java +++ b/OGP1718-Worms/src/Main.java @@ -1,30 +1,24 @@ +import worms.model.Team; +import worms.model.Worm; +import worms.util.Coordinate; + import java.io.Console; public class Main { public static void main(String[] args) { - System.out.println(4/3); - System.out.println(4.0/3.0); - /* - System.out.println("Hello World!"); - Console console = System.console(); - double input = Double.parseDouble(console.readLine("Enter your %d (th) passport number: ", 2)); - System.out.println(input); - System.out.println((int)input);*/ - System.out.println(1/2); - System.out.println(1.0/2.0); + Worm worm1 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Alpha", 1.0); + Worm worm2 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Beta", 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); - System.out.println(8 + 11 % 2); - System.out.println((8 + 11) % 2); - } + Team team1 = new Team(null, "TestTeam"); - private static int minRadius = 50; - private static double radius; - public static void setRadius(double radius2) { - if (radius2 < minRadius) { - throw new IllegalArgumentException("Radius is smaller than " + minRadius); - } - radius = radius2; + team1.addWorm(worm1); + team1.addWorm(worm2); + team1.addWorm(worm3); + team1.addWorm(worm4); + team1.getAllWormsOfTeam(); } } diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index e82a41c..fbfb856 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -1,17 +1,17 @@ package worms.model; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; import worms.util.IllegalNameException; +import worms.util.TeamComparator; public class Team { // region constructor //=================================================================================== - public Team (World world, String name) { + public Team (String name) { setName(name); + this.wormCollection = new TreeSet<>(new TeamComparator()); } //=================================================================================== @@ -28,11 +28,11 @@ public class Team { * * @throws IllegalArgumentException */ - public void addWormsToTeam(Worm... worm) throws IllegalArgumentException { + public void addWorm(Worm... worm) throws IllegalArgumentException { + if (worm == null) throw new IllegalArgumentException(); for (Worm w: worm) { - if (canHaveAsWorm(w)) { - wormArrayList.add(w); - sortList(wormArrayList); + if (w != null && canHaveAsWorm(w)) { + getAllWormsOfTeam().add(w); } else { throw new IllegalArgumentException(); @@ -41,82 +41,81 @@ public class Team { } /** - * @param worm + * @param worm .. * - * @return ... + * @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() + private boolean canHaveAsWorm(Worm worm) throws IllegalArgumentException { + + if (worm == null) throw new IllegalArgumentException(); + + Collection worms = getAllWormsOfTeam(); + if (worms.size() == 0 && !worm.isTerminated()) return true; + + if (worm.getMass() >= getMinMassTeam() / 2 && worm.getMass() <= 2 * getMinMassTeam() && !worm.isTerminated()) { - for (int i = 0; i < wormArrayList.size(); i++) { - if ((wormArrayList.get(i)).getName() == worm.getName()) - return false; - } + for (Worm elWorm : worms) { + if (elWorm.getName().equals(worm.getName())) return false; + } return true; } return false; } - /** - * @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) { + public void removeWormsFromTeam(Worm... worm) throws IllegalArgumentException { + + if (worm == null) throw new IllegalArgumentException(); for (Worm w: worm) { - wormArrayList.remove(w); + if (w == null) throw new IllegalArgumentException(); + getAllWormsOfTeam().remove(w); } } /** - * @param worm + * @param worm ... * * @return ... * |result == team.contains(worm) */ - public boolean containsWorm(Worm worm) { - if (wormArrayList.contains(worm)) { - return true; - } - return false; + public boolean containsWorm(Worm worm) throws IllegalArgumentException { + if (worm == null) throw new IllegalArgumentException(); + return getAllWormsOfTeam().contains(worm); } - public ArrayList getAllWormsOfTeam() { - return wormArrayList; + public Collection getAllWormsOfTeam() { + return wormCollection; } /** - * @param recevingTeam - * @param supplyingTeam + * @param receivingTeam ... + * @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.wormArrayList).size(); i++) { - addWormsToTeam((supplyingTeam.wormArrayList).get(i)); - } - terminate(supplyingTeam); + public void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException { + + if (receivingTeam == null || supplyingTeam == null || receivingTeam.equals(supplyingTeam) ) { + throw new IllegalArgumentException(); + } + + Worm[] supWorms = supplyingTeam.getAllWormsOfTeam().toArray(new Worm[0]); + receivingTeam.addWorm(supWorms); + supplyingTeam.removeWormsFromTeam(supWorms); } - private ArrayList wormArrayList; + private Collection wormCollection; //=================================================================================== // endregion @@ -126,55 +125,14 @@ public class Team { /** * @return ... - * |result == this.minMassTeam */ - public double getMinMassTeam() { - return this.minMassTeam; + private double getMinMassTeam() throws IllegalStateException { + Worm minMass = getAllWormsOfTeam().stream().min(Comparator.comparingDouble(Worm::getMass)).orElse(null); + if (minMass == null) { + throw new IllegalStateException(); + } + return minMass.getMass(); } - - /** - * @post ... - * |for each worm in team - * | if (minMassTeam > worm.getMass()) - * | new.getMinMassTeam() == worm.getMass() - */ - public void setMinMassTeam() { - minMassTeam = (wormArrayList.get(0)).getMass(); - for (int i = 1; i < wormArrayList.size() - 1; i++) { - Worm worm = wormArrayList.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() { - maxMassTeam = (wormArrayList.get(0)).getMass(); - for (int i = 1; i < wormArrayList.size() - 1; i++) { - Worm worm = wormArrayList.get(i); - if (worm.getMass() > maxMassTeam) { - minMassTeam = worm.getMass(); - } - } - } - - private double maxMassTeam; //=================================================================================== // endregion @@ -189,34 +147,10 @@ public class Team { 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; - } - String allowedCharacters = "'\" "; - for (int i = 0; i < name.length(); i++) { - if (!Character.isLetter(name.charAt(i))) { - if (allowedCharacters.indexOf(name.charAt(i)) == -1) { - return i; - } - } - } - return -1; - } /** - * @param name + * @param name ... * * @post |new.getName() == name * @@ -225,7 +159,7 @@ public class Team { */ public void setName(String name) throws IllegalNameException { - int validTeamName = isValidName(name); + int validTeamName = Worm.isValidName(name); if (validTeamName != -1) throw new IllegalNameException(validTeamName, name); @@ -241,17 +175,15 @@ public class Team { //=================================================================================== /** - * @param team - * - * @post ... + * + * @post * |new.terminate == true */ - public void terminate(Team team) { + public void terminate() { this.terminated = true; } /** - * @param team * * @return ... * |if terminate == true @@ -259,7 +191,7 @@ public class Team { * |else * | result == false */ - public boolean isTerminated(Team team) { + public boolean isTerminated() { return this.terminated; } diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index a57e96a..e6241b9 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -104,7 +104,7 @@ public class Worm extends GameObject { public Worm(Coordinate location, double orientation, String name, double radius, double minRadius) throws IllegalArgumentException { - if (!isValidLocation(location)) + /*if (!isValidLocation(location)) throw new IllegalArgumentException("Illegal value for location"); setLocation(location); @@ -116,7 +116,7 @@ public class Worm extends GameObject { if (!canHaveAsRadius(radius)) throw new IllegalArgumentException("Invalid radius"); - +*/ setRadius(radius); setActionPoints(getMaxActionPoints()); @@ -125,9 +125,11 @@ public class Worm extends GameObject { if (validName != -1) throw new IllegalNameException(validName, 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);*/ } //=================================================================================== diff --git a/OGP1718-Worms/src/worms/util/TeamComparator.java b/OGP1718-Worms/src/worms/util/TeamComparator.java new file mode 100644 index 0000000..3bc3554 --- /dev/null +++ b/OGP1718-Worms/src/worms/util/TeamComparator.java @@ -0,0 +1,12 @@ +package worms.util; + +import worms.model.Worm; + +import java.util.Comparator; + +public class TeamComparator implements Comparator { + @Override + public int compare(Worm t1, Worm t2) { + return t1.getName().compareTo(t2.getName()); + } +}