From dac6936895a0dbc677db0ff1febf32724a0729a0 Mon Sep 17 00:00:00 2001 From: Leen Dereu Date: Tue, 3 Apr 2018 12:19:16 +0200 Subject: [PATCH 1/4] Team class --- OGP1718-Worms/src/worms/model/Team.java | 238 +++++++++++++++++++----- 1 file changed, 196 insertions(+), 42 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index 2b725d2..1076f39 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -2,93 +2,247 @@ package worms.model; import java.util.ArrayList; import java.util.Collections; +import java.util.List; + +import worms.util.IllegalNameException; +import worms.util.ModelException; +import worms.util.MustNotImplementException; public class Team { - public void addWorm(Worm worm) { - if (worm.getMass() > minMassTeam/2 && worm.getMass() < 2 * maxMassTeam && worm.isAlive()) { - listWorms.add(worm); - sortList(listWorms); + // region constructor + //=================================================================================== + + /** + * Create a new team for the given world with given name and with no members yet. + */ + public Team (World world, String name) { + setName(name); + } + + //=================================================================================== + // endregion + + // region changeTeam + //=================================================================================== + + /** + * + * TODO Facade: addWormsToTeam(Team team, Worm... worms) + * + * @param worm + */ + public void addWormsToTeam(Worm worm) { + if (canHaveAsWorm(worm)) { + team.add(worm); + sortList(team); } } - public void sortList(ArrayList list) { - Collections.sort(list, - (el1, el2) -> el1.getName().compareToIgnoreCase(el2.getName())); - } - - public void removeWorm(Worm worm) { - listWorms.remove(worm); - } - - public boolean containsWorm(Worm worm) { - if (listWorms.contains(worm)) { + public boolean canHaveAsWorm(Worm worm) { + if (worm.getMass() > getMinMassTeam()/2 && worm.getMass() < 2 * getMaxMassTeam() + && worm.isAlive()) { + for (int i = 0; i < team.size(); i++) { + if ((team.get(i)).getName() == worm.getName()) + return false; + } return true; } return false; } - public ArrayList returnTeam() { - return listWorms; + /** + * + * @param list + */ + public void sortList(ArrayList list) { + Collections.sort(list, + (el1, el2) -> el1.getName().compareToIgnoreCase(el2.getName())); } - private ArrayList listWorms; + /** + * + * TODO Facade: removeWormsFromTeam(Team team, Worm... worms) + * + * @param worm + */ + public void removeWormsFromTeam(Worm worm) { + team.remove(worm); + } + /** + * + * @param worm + * @return + */ + public boolean containsWorm(Worm worm) { + if (team.contains(worm)) { + return true; + } + return false; + } + + /** + * Return a list of all the worms in the given team, sorted alphabetically. + * This method must run in linear time. + * + * @return listWorms + */ + public ArrayList getAllWormsOfTeam() { + return team; + } + + /** + * Merge the given teams. + * - All the worms of the supplying team are transferred to the receiving team. + */ + public void mergeTeams(Team recevingTeam, Team supplyingTeam) { + for (int i = 0; i < (supplyingTeam.team).size(); i++) { + addWormsToTeam((supplyingTeam.team).get(i)); + } + terminate(supplyingTeam); + } + + /** + * + */ + private ArrayList team; + + //=================================================================================== + // endregion + + // region mass + //=================================================================================== + + /** + * + * @return + */ public double getMinMassTeam() { return this.minMassTeam; } + /** + * + */ public void setMinMassTeam() { - for (int i = 0; i < listWorms.size() - 2; i++) { - Worm wormA = listWorms.get(i); - Worm wormB = listWorms.get(i+1); + 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(); } } } + /** + * + */ private double minMassTeam; + /** + * + * @return + */ public double getMaxMassTeam() { return this.maxMassTeam; } + /** + * + */ public void setMaxMassTeam() { - for (int i = 0; i < listWorms.size() - 2; i++) { - Worm wormA = listWorms.get(i); - Worm wormB = listWorms.get(i+1); + 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(); } } } + /** + * + */ private double maxMassTeam; - public String getTeamName() { - return this.teamName; + //=================================================================================== + // endregion + + // region name + //=================================================================================== + + /** + * + * @return + */ + public String getName() { + return this.name; } - public void setTeamName() { - - } + /** + * + * @param teamName + * @return + */ + 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; + } - private String teamName; + /** + * + * @param teamName + * @throws IllegalNameException + */ + public void setName(String name) throws IllegalNameException { + + int validTeamName = isValidName(name); + if (validTeamName != -1) + throw new IllegalNameException(validTeamName, name); + + this.name = name; + } + + /** + * + */ + private String name; - public String getWinner(){ - return this.winner; - } + //=================================================================================== + // endregion - public void setWinner() { - if (listWorms.size() == 1) { - winner = (listWorms.get(0)).getName(); - } - else { - winner = this.getTeamName(); - } - } + // region terminate + //=================================================================================== - private String winner; + //TODO region terminate + /** + * Terminate the given team. + */ + public void terminate(Team team) { + + } + + /** + * Check whether the given portion of food is terminated. + */ + public boolean isTerminated(Team team) { + return false; + } + + //=================================================================================== + // endregion } From 6cd76282c02e5fb780e45a926fed40b4aa44e7c8 Mon Sep 17 00:00:00 2001 From: Leen Dereu Date: Tue, 3 Apr 2018 12:31:22 +0200 Subject: [PATCH 2/4] Team class --- OGP1718-Worms/src/worms/model/Team.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index 1076f39..5fef8bd 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -27,14 +27,15 @@ public class Team { /** * - * TODO Facade: addWormsToTeam(Team team, Worm... worms) * * @param worm */ - public void addWormsToTeam(Worm worm) { - if (canHaveAsWorm(worm)) { - team.add(worm); - sortList(team); + public void addWormsToTeam(Worm... worm) { + for (Worm w: worm) { + if (canHaveAsWorm(w)) { + team.add(w); + sortList(team); + } } } @@ -61,12 +62,13 @@ public class Team { /** * - * TODO Facade: removeWormsFromTeam(Team team, Worm... worms) * * @param worm */ - public void removeWormsFromTeam(Worm worm) { - team.remove(worm); + public void removeWormsFromTeam(Worm... worm) { + for (Worm w: worm) { + team.remove(w); + } } /** From 8166920c94ae1fdabcd6b8f3e0651eb649aaa247 Mon Sep 17 00:00:00 2001 From: Leen Dereu Date: Tue, 3 Apr 2018 12:48:49 +0200 Subject: [PATCH 3/4] Food class --- OGP1718-Worms/src/worms/model/Food.java | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/OGP1718-Worms/src/worms/model/Food.java b/OGP1718-Worms/src/worms/model/Food.java index 2129851..379a8a1 100644 --- a/OGP1718-Worms/src/worms/model/Food.java +++ b/OGP1718-Worms/src/worms/model/Food.java @@ -1,5 +1,39 @@ package worms.model; +import static java.lang.Math.*; + public class Food { + // region location + //=================================================================================== + + //=================================================================================== + // endregion + + // region shape + //=================================================================================== + + final double radius = 0.20; + + public double getMass() { + return this.mass; + } + + public void setMass() { + final double rho = 150; + double m = rho * (4/3 * PI * pow(radius, 3)); + this.mass = m; + } + + public double mass; + + //=================================================================================== + // endregion + + // region eat + //=================================================================================== + + + //=================================================================================== + // endregion } From a18d5863886e2ee2ec9b9979c982548fcaf454fa Mon Sep 17 00:00:00 2001 From: Leen Dereu Date: Tue, 3 Apr 2018 15:12:02 +0200 Subject: [PATCH 4/4] Worm class: fight, hitPoints, fall, collision --- OGP1718-Worms/src/worms/model/Team.java | 105 +++--------------------- OGP1718-Worms/src/worms/model/Worm.java | 73 +++++++++++++--- 2 files changed, 76 insertions(+), 102 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Team.java b/OGP1718-Worms/src/worms/model/Team.java index 5fef8bd..e7ac7a8 100644 --- a/OGP1718-Worms/src/worms/model/Team.java +++ b/OGP1718-Worms/src/worms/model/Team.java @@ -12,9 +12,6 @@ public class Team { // region constructor //=================================================================================== - /** - * Create a new team for the given world with given name and with no members yet. - */ public Team (World world, String name) { setName(name); } @@ -22,14 +19,9 @@ public class Team { //=================================================================================== // endregion - // region changeTeam + // region changesTeam //=================================================================================== - - /** - * - * - * @param worm - */ + public void addWormsToTeam(Worm... worm) { for (Worm w: worm) { if (canHaveAsWorm(w)) { @@ -50,53 +42,29 @@ public class Team { } return false; } - - /** - * - * @param list - */ + public void sortList(ArrayList list) { Collections.sort(list, (el1, el2) -> el1.getName().compareToIgnoreCase(el2.getName())); } - - /** - * - * - * @param worm - */ + public void removeWormsFromTeam(Worm... worm) { for (Worm w: worm) { team.remove(w); } } - - /** - * - * @param worm - * @return - */ + public boolean containsWorm(Worm worm) { if (team.contains(worm)) { return true; } return false; } - - /** - * Return a list of all the worms in the given team, sorted alphabetically. - * This method must run in linear time. - * - * @return listWorms - */ + public ArrayList getAllWormsOfTeam() { return team; } - - /** - * Merge the given teams. - * - All the worms of the supplying team are transferred to the receiving team. - */ + public void mergeTeams(Team recevingTeam, Team supplyingTeam) { for (int i = 0; i < (supplyingTeam.team).size(); i++) { addWormsToTeam((supplyingTeam.team).get(i)); @@ -104,9 +72,6 @@ public class Team { terminate(supplyingTeam); } - /** - * - */ private ArrayList team; //=================================================================================== @@ -114,18 +79,11 @@ public class Team { // region mass //=================================================================================== - - /** - * - * @return - */ + public double getMinMassTeam() { return this.minMassTeam; } - - /** - * - */ + public void setMinMassTeam() { for (int i = 0; i < team.size() - 1; i++) { Worm wormA = team.get(i); @@ -135,23 +93,13 @@ public class Team { } } } - - /** - * - */ + private double minMassTeam; - - /** - * - * @return - */ + public double getMaxMassTeam() { return this.maxMassTeam; } - - /** - * - */ + public void setMaxMassTeam() { for (int i = 0; i < team.size() - 1; i++) { Worm wormA = team.get(i); @@ -161,10 +109,7 @@ public class Team { } } } - - /** - * - */ + private double maxMassTeam; //=================================================================================== @@ -173,19 +118,10 @@ public class Team { // region name //=================================================================================== - /** - * - * @return - */ public String getName() { return this.name; } - /** - * - * @param teamName - * @return - */ public static int isValidName (String name) { if (name.length() < 2 || !Character.isUpperCase(name.charAt(0))) { @@ -203,11 +139,6 @@ public class Team { return -1; } - /** - * - * @param teamName - * @throws IllegalNameException - */ public void setName(String name) throws IllegalNameException { int validTeamName = isValidName(name); @@ -217,10 +148,6 @@ public class Team { this.name = name; } - - /** - * - */ private String name; //=================================================================================== @@ -231,16 +158,10 @@ public class Team { //TODO region terminate - /** - * Terminate the given team. - */ public void terminate(Team team) { } - /** - * Check whether the given portion of food is terminated. - */ public boolean isTerminated(Team team) { return false; } diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 9556626..8e99cc6 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -593,14 +593,30 @@ public class Worm { subtractActionPoints(cost); } - public void collision(Worm smallestWorm, Worm largestWorm) { - long total = 1 + (new Random().nextLong() * (10 - 1)); - long loseSmallest = (long) (total / (largestWorm.getOrientation() / (smallestWorm.getOrientation() + largestWorm.getOrientation()))); - long loseLargest = total - loseSmallest; - smallestWorm.incrementHitPoints(loseSmallest); - largestWorm.incrementHitPoints(loseLargest); + public Coordinate getFurthestLocationInDirection() { + } - + + public void collision(Worm basicWorm, Worm... worm) { + Worm largestWorm; + Worm smallestWorm; + for (Worm w1: worm) { + if (w1.getMass() > basicWorm.getMass()) { + largestWorm = w1; + smallestWorm = basicWorm; + } + else { + largestWorm = basicWorm; + smallestWorm = w1; + } + long total = 1 + (new Random().nextLong() * (10 - 1)); + long loseSmallest = (long) (total / (largestWorm.getOrientation() / (smallestWorm.getOrientation() + largestWorm.getOrientation()))); + long loseLargest = total - loseSmallest; + smallestWorm.incrementHitPoints(loseSmallest); + largestWorm.incrementHitPoints(loseLargest); + } + } + //=================================================================================== // endregion @@ -764,6 +780,27 @@ public class Worm { double force = 5 * getActionPoints() + getMass() * G; return force / getMass() * FORCE_TIME; } + + public void fight(Worm worm1, Worm... worm2) { + for (Worm wormB: worm2) { + Worm attackedWorm; + Worm attacker; + Random rand = new Random(); + int coin = rand.nextInt((1 - 0) + 1) + 0; + if (coin == 1) { + attackedWorm = worm1; + attacker = wormB; + } + else { + attackedWorm = wormB; + attacker = worm1; + } + double N = 10 * ceil((attacker.getRadius())/(attackedWorm.getRadius())); + long loseAttackedWorm = (long) rand.nextInt((((int) N) - 1) + 1) + 1; + attackedWorm.incrementHitPoints(loseAttackedWorm); + } + + } //=================================================================================== @@ -780,7 +817,7 @@ public class Worm { @Raw private void setHitPoints(long hitPoints) { if (hitPoints <= 0) - // TODO worm sterft + weghalen van gamewereld + terminate(); this.hitPoints = hitPoints; } @@ -790,11 +827,27 @@ public class Worm { setHitPoints(getHitPoints() - value); } + /** + * terminate the worm + */ + public void terminate() { + + } + //=================================================================================== // endregion - //TODO nog te implementeren public boolean isAlive() { - return true; + return false; } + + // region falling + //=================================================================================== + + public void fall() { + + } + + //=================================================================================== + // endregion }