Merge branch 'master' of gitlab.principis.be:OGP/worms

This commit is contained in:
2018-04-03 15:35:59 +02:00
3 changed files with 222 additions and 58 deletions

View File

@@ -1,5 +1,39 @@
package worms.model; package worms.model;
import static java.lang.Math.*;
public class Food { 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
} }

View File

@@ -2,93 +2,170 @@ 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); public Team (World world, String name) {
setName(name);
}
//===================================================================================
// endregion
// region changesTeam
//===================================================================================
public void addWormsToTeam(Worm... worm) {
for (Worm w: worm) {
if (canHaveAsWorm(w)) {
team.add(w);
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() { public void sortList(ArrayList<Worm> list) {
return listWorms; Collections.sort(list,
(el1, el2) -> el1.getName().compareToIgnoreCase(el2.getName()));
} }
public void removeWormsFromTeam(Worm... worm) {
for (Worm w: worm) {
team.remove(w);
}
}
public boolean containsWorm(Worm worm) {
if (team.contains(worm)) {
return true;
}
return false;
}
public ArrayList<Worm> getAllWormsOfTeam() {
return 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> listWorms; private ArrayList<Worm> team;
//===================================================================================
// endregion
// region mass
//===================================================================================
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;
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
//===================================================================================
public String getName() {
return this.name;
} }
public void setTeamName() { 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; 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;
//===================================================================================
// endregion
public String getWinner(){ // region terminate
return this.winner; //===================================================================================
}
public void setWinner() { //TODO region terminate
if (listWorms.size() == 1) {
winner = (listWorms.get(0)).getName();
}
else {
winner = this.getTeamName();
}
}
private String winner; public void terminate(Team team) {
}
public boolean isTerminated(Team team) {
return false;
}
//===================================================================================
// endregion
} }

View File

@@ -593,14 +593,30 @@ public class Worm {
subtractActionPoints(cost); subtractActionPoints(cost);
} }
public void collision(Worm smallestWorm, Worm largestWorm) { public Coordinate getFurthestLocationInDirection() {
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 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 // endregion
@@ -764,6 +780,27 @@ public class Worm {
double force = 5 * getActionPoints() + getMass() * G; double force = 5 * getActionPoints() + getMass() * G;
return force / getMass() * FORCE_TIME; 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 @Raw
private void setHitPoints(long hitPoints) { private void setHitPoints(long hitPoints) {
if (hitPoints <= 0) if (hitPoints <= 0)
// TODO worm sterft + weghalen van gamewereld terminate();
this.hitPoints = hitPoints; this.hitPoints = hitPoints;
} }
@@ -790,11 +827,27 @@ public class Worm {
setHitPoints(getHitPoints() - value); setHitPoints(getHitPoints() - value);
} }
/**
* terminate the worm
*/
public void terminate() {
}
//=================================================================================== //===================================================================================
// endregion // endregion
//TODO nog te implementeren
public boolean isAlive() { public boolean isAlive() {
return true; return false;
} }
// region falling
//===================================================================================
public void fall() {
}
//===================================================================================
// endregion
} }