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 }