massive improvement to Team

This commit is contained in:
2018-04-11 16:39:58 +02:00
parent 14d67eb323
commit 016a2e2bbb
4 changed files with 88 additions and 148 deletions

View File

@@ -1,30 +1,24 @@
import worms.model.Team;
import worms.model.Worm;
import worms.util.Coordinate;
import java.io.Console; import java.io.Console;
public class Main { public class Main {
public static void main(String[] args) { 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); Worm worm1 = new Worm(Coordinate.create(0.0, 0.0), 0.0, "Alpha", 1.0);
System.out.println(1.0/2.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); Team team1 = new Team(null, "TestTeam");
System.out.println((8 + 11) % 2);
}
private static int minRadius = 50; team1.addWorm(worm1);
private static double radius; team1.addWorm(worm2);
public static void setRadius(double radius2) { team1.addWorm(worm3);
if (radius2 < minRadius) { team1.addWorm(worm4);
throw new IllegalArgumentException("Radius is smaller than " + minRadius); team1.getAllWormsOfTeam();
}
radius = radius2;
} }
} }

View File

@@ -1,17 +1,17 @@
package worms.model; package worms.model;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.List;
import worms.util.IllegalNameException; import worms.util.IllegalNameException;
import worms.util.TeamComparator;
public class Team { public class Team {
// region constructor // region constructor
//=================================================================================== //===================================================================================
public Team (World world, String name) { public Team (String name) {
setName(name); setName(name);
this.wormCollection = new TreeSet<>(new TeamComparator());
} }
//=================================================================================== //===================================================================================
@@ -28,11 +28,11 @@ public class Team {
* *
* @throws IllegalArgumentException * @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) { for (Worm w: worm) {
if (canHaveAsWorm(w)) { if (w != null && canHaveAsWorm(w)) {
wormArrayList.add(w); getAllWormsOfTeam().add(w);
sortList(wormArrayList);
} }
else { else {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@@ -41,82 +41,81 @@ public class Team {
} }
/** /**
* @param worm * @param worm ..
* *
* @return ... * @return
* |for each i in [0,team.size()[ * |for each i in [0,team.size()[
* | result == (worm.getMass() > getMinMassTeam()/2 && worm.getMass() < 2 * getMaxMassTeam() * | result == (worm.getMass() > getMinMassTeam()/2 && worm.getMass() < 2 * getMaxMassTeam()
* | && worm.isAlive() && (team.get(i)).getName() != worm.getName()) * | && worm.isAlive() && (team.get(i)).getName() != worm.getName())
*/ */
public boolean canHaveAsWorm(Worm worm) { private boolean canHaveAsWorm(Worm worm) throws IllegalArgumentException {
if (worm.getMass() > getMinMassTeam()/2 && worm.getMass() < 2 * getMaxMassTeam()
if (worm == null) throw new IllegalArgumentException();
Collection<Worm> worms = getAllWormsOfTeam();
if (worms.size() == 0 && !worm.isTerminated()) return true;
if (worm.getMass() >= getMinMassTeam() / 2 && worm.getMass() <= 2 * getMinMassTeam()
&& !worm.isTerminated()) { && !worm.isTerminated()) {
for (int i = 0; i < wormArrayList.size(); i++) { for (Worm elWorm : worms) {
if ((wormArrayList.get(i)).getName() == worm.getName()) if (elWorm.getName().equals(worm.getName())) return false;
return false; }
}
return true; return true;
} }
return false; return false;
} }
/**
* @param team
*
* @post ...
* |
*/
public void sortList(ArrayList<Worm> team) {
Collections.sort(team,
(el1, el2) -> el1.getName().compareToIgnoreCase(el2.getName()));
}
/** /**
* @param worm * @param worm
* *
* @post ... * @post ...
* |(new.team).contains(Worm... worm) == false * |(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) { for (Worm w: worm) {
wormArrayList.remove(w); if (w == null) throw new IllegalArgumentException();
getAllWormsOfTeam().remove(w);
} }
} }
/** /**
* @param worm * @param worm ...
* *
* @return ... * @return ...
* |result == team.contains(worm) * |result == team.contains(worm)
*/ */
public boolean containsWorm(Worm worm) { public boolean containsWorm(Worm worm) throws IllegalArgumentException {
if (wormArrayList.contains(worm)) { if (worm == null) throw new IllegalArgumentException();
return true; return getAllWormsOfTeam().contains(worm);
}
return false;
} }
public ArrayList<Worm> getAllWormsOfTeam() { public Collection<Worm> getAllWormsOfTeam() {
return wormArrayList; return wormCollection;
} }
/** /**
* @param recevingTeam * @param receivingTeam ...
* @param supplyingTeam * @param supplyingTeam ...
* *
* @post ... (NOG NIET ZEKER VAN) * @post ... (NOG NIET ZEKER VAN)
* |recevingTeam == receivingTeam + supplyingTeam * |recevingTeam == receivingTeam + supplyingTeam
* @post ... * @post ...
* |supplyingTeam.isTerminated() == true * |supplyingTeam.isTerminated() == true
*/ */
public void mergeTeams(Team recevingTeam, Team supplyingTeam) { public void mergeTeams(Team receivingTeam, Team supplyingTeam) throws IllegalArgumentException {
for (int i = 0; i < (supplyingTeam.wormArrayList).size(); i++) {
addWormsToTeam((supplyingTeam.wormArrayList).get(i)); if (receivingTeam == null || supplyingTeam == null || receivingTeam.equals(supplyingTeam) ) {
} throw new IllegalArgumentException();
terminate(supplyingTeam); }
Worm[] supWorms = supplyingTeam.getAllWormsOfTeam().toArray(new Worm[0]);
receivingTeam.addWorm(supWorms);
supplyingTeam.removeWormsFromTeam(supWorms);
} }
private ArrayList<Worm> wormArrayList; private Collection<Worm> wormCollection;
//=================================================================================== //===================================================================================
// endregion // endregion
@@ -126,55 +125,14 @@ public class Team {
/** /**
* @return ... * @return ...
* |result == this.minMassTeam
*/ */
public double getMinMassTeam() { private double getMinMassTeam() throws IllegalStateException {
return this.minMassTeam; 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 // endregion
@@ -189,34 +147,10 @@ public class Team {
public String getName() { public String getName() {
return this.name; 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 * @post |new.getName() == name
* *
@@ -225,7 +159,7 @@ public class Team {
*/ */
public void setName(String name) throws IllegalNameException { public void setName(String name) throws IllegalNameException {
int validTeamName = isValidName(name); int validTeamName = Worm.isValidName(name);
if (validTeamName != -1) if (validTeamName != -1)
throw new IllegalNameException(validTeamName, name); throw new IllegalNameException(validTeamName, name);
@@ -241,17 +175,15 @@ public class Team {
//=================================================================================== //===================================================================================
/** /**
* @param team *
* * @post
* @post ...
* |new.terminate == true * |new.terminate == true
*/ */
public void terminate(Team team) { public void terminate() {
this.terminated = true; this.terminated = true;
} }
/** /**
* @param team
* *
* @return ... * @return ...
* |if terminate == true * |if terminate == true
@@ -259,7 +191,7 @@ public class Team {
* |else * |else
* | result == false * | result == false
*/ */
public boolean isTerminated(Team team) { public boolean isTerminated() {
return this.terminated; return this.terminated;
} }

View File

@@ -104,7 +104,7 @@ public class Worm extends GameObject {
public Worm(Coordinate location, double orientation, String name, double radius, double minRadius) public Worm(Coordinate location, double orientation, String name, double radius, double minRadius)
throws IllegalArgumentException { throws IllegalArgumentException {
if (!isValidLocation(location)) /*if (!isValidLocation(location))
throw new IllegalArgumentException("Illegal value for location"); throw new IllegalArgumentException("Illegal value for location");
setLocation(location); setLocation(location);
@@ -116,7 +116,7 @@ public class Worm extends GameObject {
if (!canHaveAsRadius(radius)) if (!canHaveAsRadius(radius))
throw new IllegalArgumentException("Invalid radius"); throw new IllegalArgumentException("Invalid radius");
*/
setRadius(radius); setRadius(radius);
setActionPoints(getMaxActionPoints()); setActionPoints(getMaxActionPoints());
@@ -125,9 +125,11 @@ public class Worm extends GameObject {
if (validName != -1) if (validName != -1)
throw new IllegalNameException(validName, name); throw new IllegalNameException(validName, name);
this.name = name; this.name = name;
this.minRadius= 0.25;
long startHitPoints = 1000 + (new Random().nextLong() * (2000 - 1000)); /*long startHitPoints = 1000 + (new Random().nextLong() * (2000 - 1000));
setHitPoints(startHitPoints); setHitPoints(startHitPoints);*/
} }
//=================================================================================== //===================================================================================

View File

@@ -0,0 +1,12 @@
package worms.util;
import worms.model.Worm;
import java.util.Comparator;
public class TeamComparator implements Comparator<Worm> {
@Override
public int compare(Worm t1, Worm t2) {
return t1.getName().compareTo(t2.getName());
}
}