improved world class

This commit is contained in:
2018-04-03 17:05:02 +02:00
parent adb06f032e
commit 7ffe8803c2
3 changed files with 193 additions and 36 deletions

View File

@@ -369,7 +369,7 @@ public class Facade implements IFacade {
}
/**
* Check whether the given worls is terminated.
* Check whether the given world is terminated.
*
* @param world
*/

View File

@@ -1,10 +1,9 @@
package worms.model;
import java.util.*;
public class World {
private boolean terminated = false;
public World(double width, double height, boolean[][] map) {
if (!isValidDimension(width) || !isValidDimension(height)) throw new IllegalArgumentException();
@@ -12,12 +11,67 @@ public class World {
this.width = width;
this.height = height;
this.map = map;
this.wormList = new ArrayList<>();
this.foodSet = new HashSet<>();
lengthX = width / map[0].length;
lengthY = height / map.length;
this.lengthX = width / map[0].length;
this.lengthY = height / map.length;
this.game = false;
}
// region game
//===================================================================================
public boolean hasActiveGame() {
return this.game;
}
public void startGame() {
this.game = true;
this.activeWorm = 0;
}
public void finishGame() {
this.game = false;
}
public void activateNextWorm() {
this.activeWorm++;
if (this.activeWorm == getWormList().size()) {
this.activeWorm = 0;
}
}
public Worm getActiveWorm() {
return getWormList().get(this.activeWorm);
}
public String getWinner() {
if (getWormList().size() == 1) {
return getWormList().get(0).getName();
} else if (getWormList().size() > 1) {
Team lastTeam = getWormList().get(0).getTeam();
for(Worm worm: getWormList()) {
if (!lastTeam.equals(worm.getTeam())) {
return null;
}
}
return lastTeam.getName();
} else {
return null;
}
}
private boolean game;
private int activeWorm;
//===================================================================================
// endregion
// region Dimensions
//===================================================================================
@@ -38,15 +92,37 @@ public class World {
// ===================================================================================
// endregion
// region map
//===================================================================================
private final double lengthX;
private final double lengthY;
public boolean[][] getMap() {
return map;
}
public boolean isTerminated() {
return this.terminated;
}
public void terminate() {
this.terminated = true;
}
private final boolean[][] map;
private boolean terminated = false;
// ===================================================================================
// endregion
// region passable
//===================================================================================
/**
*
* @param location ...
* @return ...
*/
public boolean isPassable(double[] location) {
if (location[0] / lengthX >= getMap()[0].length || location[0] < 0.0 ||
@@ -56,23 +132,12 @@ public class World {
return this.map[(int) Math.floor(location[1] / lengthY)][(int) Math.floor(location[0] / lengthX)];
}
private boolean isPassable(double x, double y) {
int xCoord = (int) Math.floor(x / lengthX);
int yCoord = (int) Math.floor(y / lengthY);
return this.map[yCoord][xCoord];
}
private boolean canHaveAsPartialCircle(double x, double y, double[] center) {
if (!isPassable(x + center[0], y + center[1])) return false;
x *= -1;
if (!isPassable(x + center[0], y + center[1])) return false;
y *= -1;
if (!isPassable(x + center[0], y + center[1])) return false;
x *= -1;
return isPassable(x + center[0], y + center[1]);
}
/**
*
* @param center ...
* @param radius ...
* @return ...
*/
public boolean isPassable(double[] center, double radius) {
int radX = (int) Math.floor(radius / lengthX);
@@ -90,6 +155,12 @@ public class World {
return true;
}
/**
*
* @param center ...
* @param radius ...
* @return ...
*/
public boolean isAdjacent(double[] center, double radius) {
double maxDistance = radius * 0.1;
@@ -99,20 +170,105 @@ public class World {
return isPassable(center, radius) && !isPassable(center, radius + maxDistance + length);
}
public boolean isTerminated() {
return terminated;
private final double lengthX;
private final double lengthY;
/**
*
* @param x ...
* @param y ...
* @return ...
*/
private boolean isPassable(double x, double y) {
int xCoord = (int) Math.floor(x / lengthX);
int yCoord = (int) Math.floor(y / lengthY);
return getMap()[yCoord][xCoord];
}
public void terminate() {
setTerminated(true);
}
/**
*
* @param x ...
* @param y ...
* @param center ...
* @return
*/
private boolean canHaveAsPartialCircle(double x, double y, double[] center) {
private final boolean[][] map;
private void setTerminated(boolean terminated) {
this.terminated = terminated;
if (!isPassable(x + center[0], y + center[1])) return false;
x *= -1;
if (!isPassable(x + center[0], y + center[1])) return false;
y *= -1;
if (!isPassable(x + center[0], y + center[1])) return false;
x *= -1;
return isPassable(x + center[0], y + center[1]);
}
// ===================================================================================
// endregion
// region objects
//===================================================================================
public Collection<Object> getAllItems() {
Set<Object> all = new HashSet<Object>();
all.add(getWormList());
all.add(getFoodSet());
return all;
}
// public Set<Team> getAllTeams() {
//
// Set<Team> teams = new HashSet<Team>();
// for(Worm worm: getWormList()) {
// teams.add(worm.getTeam());
// }
// }
public boolean hasAsWorm(Worm worm) throws NullPointerException {
return getWormList().contains(worm);
}
public void addWorm(Worm worm) throws IllegalArgumentException, NullPointerException, IllegalStateException {
if (hasActiveGame()) throw new IllegalStateException();
if (hasAsWorm(worm)) throw new IllegalArgumentException();
getWormList().add(worm);
}
public void removeWorm(Worm worm) throws IllegalArgumentException, NullPointerException {
if (!getWormList().remove(worm)) throw new IllegalArgumentException();
}
private List<Worm> getWormList() {
return this.wormList;
}
public boolean hasAsFood(Food food) throws NullPointerException {
return getFoodSet().contains(food);
}
public void addFood(Food food) throws IllegalArgumentException, NullPointerException, IllegalStateException {
if (hasActiveGame()) throw new IllegalStateException();
if (!getFoodSet().add(food)) throw new IllegalArgumentException();
}
public void removeFood(Food food) throws IllegalArgumentException, NullPointerException {
if (!getFoodSet().remove(food)) throw new IllegalArgumentException();
}
private Set<Food> getFoodSet() {
return this.foodSet;
}
private List<Worm> wormList;
private Set<Food> foodSet;
// ===================================================================================
// endregion
}

View File

@@ -1,6 +1,7 @@
package worms.model;
import be.kuleuven.cs.som.annotate.*;
import jdk.jshell.spi.ExecutionControl;
import worms.util.Coordinate;
import worms.util.IllegalNameException;
@@ -593,7 +594,7 @@ public class Worm {
}
public Coordinate getFurthestLocationInDirection() {
return null;
}
public void collision(Worm basicWorm, Worm... worm) {