Team class
This commit is contained in:
@@ -2,93 +2,247 @@ package worms.model;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import worms.util.IllegalNameException;
|
||||||
|
import worms.util.ModelException;
|
||||||
|
import worms.util.MustNotImplementException;
|
||||||
|
|
||||||
public class Team {
|
public class Team {
|
||||||
public void addWorm(Worm worm) {
|
// region constructor
|
||||||
if (worm.getMass() > minMassTeam/2 && worm.getMass() < 2 * maxMassTeam && worm.isAlive()) {
|
//===================================================================================
|
||||||
listWorms.add(worm);
|
|
||||||
sortList(listWorms);
|
/**
|
||||||
|
* 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<Worm> list) {
|
public boolean canHaveAsWorm(Worm worm) {
|
||||||
Collections.sort(list,
|
if (worm.getMass() > getMinMassTeam()/2 && worm.getMass() < 2 * getMaxMassTeam()
|
||||||
(el1, el2) -> el1.getName().compareToIgnoreCase(el2.getName()));
|
&& worm.isAlive()) {
|
||||||
}
|
for (int i = 0; i < team.size(); i++) {
|
||||||
|
if ((team.get(i)).getName() == worm.getName())
|
||||||
public void removeWorm(Worm worm) {
|
return false;
|
||||||
listWorms.remove(worm);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public boolean containsWorm(Worm worm) {
|
|
||||||
if (listWorms.contains(worm)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Worm> returnTeam() {
|
/**
|
||||||
return listWorms;
|
*
|
||||||
|
* @param list
|
||||||
|
*/
|
||||||
|
public void sortList(ArrayList<Worm> list) {
|
||||||
|
Collections.sort(list,
|
||||||
|
(el1, el2) -> el1.getName().compareToIgnoreCase(el2.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<Worm> 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<Worm> 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<Worm> team;
|
||||||
|
|
||||||
|
//===================================================================================
|
||||||
|
// endregion
|
||||||
|
|
||||||
|
// region mass
|
||||||
|
//===================================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public double getMinMassTeam() {
|
public double getMinMassTeam() {
|
||||||
return this.minMassTeam;
|
return this.minMassTeam;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public void setMinMassTeam() {
|
public void setMinMassTeam() {
|
||||||
for (int i = 0; i < listWorms.size() - 2; i++) {
|
for (int i = 0; i < team.size() - 1; i++) {
|
||||||
Worm wormA = listWorms.get(i);
|
Worm wormA = team.get(i);
|
||||||
Worm wormB = listWorms.get(i+1);
|
Worm wormB = team.get(i+1);
|
||||||
if (wormA.getMass() < wormB.getMass()) {
|
if (wormA.getMass() < wormB.getMass()) {
|
||||||
minMassTeam = wormA.getMass();
|
minMassTeam = wormA.getMass();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
private double minMassTeam;
|
private double minMassTeam;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public double getMaxMassTeam() {
|
public double getMaxMassTeam() {
|
||||||
return this.maxMassTeam;
|
return this.maxMassTeam;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
public void setMaxMassTeam() {
|
public void setMaxMassTeam() {
|
||||||
for (int i = 0; i < listWorms.size() - 2; i++) {
|
for (int i = 0; i < team.size() - 1; i++) {
|
||||||
Worm wormA = listWorms.get(i);
|
Worm wormA = team.get(i);
|
||||||
Worm wormB = listWorms.get(i+1);
|
Worm wormB = team.get(i+1);
|
||||||
if (wormA.getMass() > wormB.getMass()) {
|
if (wormA.getMass() > wormB.getMass()) {
|
||||||
maxMassTeam = wormA.getMass();
|
maxMassTeam = wormA.getMass();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
private double maxMassTeam;
|
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() {
|
// region terminate
|
||||||
if (listWorms.size() == 1) {
|
//===================================================================================
|
||||||
winner = (listWorms.get(0)).getName();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
winner = this.getTeamName();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user