corrected 'Editorstate' and 'GameServerLogic' and edited 'SeaSynchronizer' and added assets
This commit is contained in:
@@ -241,8 +241,8 @@ public void loadMap(File file) throws IOException {
|
||||
final ShipMapDTO dto = ShipMapDTO.loadFrom(file);
|
||||
if (!dto.fits(logic.getDetails()))
|
||||
throw new IOException(lookup("map.doesnt.fit"));
|
||||
if(!verifyMap(dto)){
|
||||
throw new IOException("Map is not valid");
|
||||
if (!verifyMap(dto)) {
|
||||
throw new IOException(lookup("map is not valid"));
|
||||
}
|
||||
ownMap().clear();
|
||||
dto.getShips().forEach(ownMap()::add);
|
||||
@@ -257,20 +257,22 @@ public void loadMap(File file) throws IOException {
|
||||
* @param dto the map
|
||||
* @return true, if the map is valid
|
||||
*/
|
||||
private boolean verifyMap(ShipMapDTO dto){ //MapMessage msg, int playerID
|
||||
private boolean verifyMap(ShipMapDTO dto) { //MapMessage msg, int playerID
|
||||
return verifyBounds(dto) && verifyOverlap(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks, whether a ship is out of the grid
|
||||
*
|
||||
* @param dto is the Map, where the ships are in
|
||||
* @return true, if all ships are in bound
|
||||
*/
|
||||
private boolean verifyBounds(ShipMapDTO dto){
|
||||
for(Battleship ship: dto.getShips()){
|
||||
int mapWidth = dto.getWidth();
|
||||
int mapHeight = dto.getHeight();
|
||||
if(ship.getMaxX()>mapWidth || ship.getMinX()<0 || ship.getMaxY()> mapHeight || ship.getMinY() < 0) return false;
|
||||
private boolean verifyBounds(ShipMapDTO dto) {
|
||||
int mapWidth = dto.getWidth();
|
||||
int mapHeight = dto.getHeight();
|
||||
for (Battleship ship : dto.getShips()) {
|
||||
if (ship.getMaxX() > mapWidth || ship.getMinX() < 0 || ship.getMaxY() > mapHeight || ship.getMinY() < 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -281,32 +283,30 @@ private boolean verifyBounds(ShipMapDTO dto){
|
||||
* @param dto ist the map, where the ships are in
|
||||
* @return true, if no ships overlap
|
||||
*/
|
||||
private boolean verifyOverlap(ShipMapDTO dto){
|
||||
List<Battleship> ships = dto.getShips();
|
||||
for(Battleship ship : ships){
|
||||
for(Battleship compareShip : ships){
|
||||
if(!(ships==compareShip)){
|
||||
if(ship.collidesWith(compareShip)){
|
||||
return false;
|
||||
}
|
||||
private boolean verifyOverlap(ShipMapDTO dto) {
|
||||
List<Battleship> battleshipList = dto.getShips();
|
||||
for (int i = 0; i < battleshipList.size(); i++) {
|
||||
Battleship ship1 = battleshipList.get(i);
|
||||
for (int j = i + 1; j < battleshipList.size(); j++) {
|
||||
Battleship ship2 = battleshipList.get(j);
|
||||
if (ship1.collidesWith(ship2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
/*
|
||||
List<Battleship> ships = dto.getShips();
|
||||
List<Battleship> shipsReduceList = new ArrayList<>(ships);
|
||||
for(Battleship ship: ships){ //iterate through the list of all ships on the grid
|
||||
shipsReduceList.remove(ship); // delete the current ship from the second list, so it will not test, if it collides with itself
|
||||
for(Battleship battleship:shipsReduceList){ //iterates through the other ships
|
||||
if(ship.collidesWith(battleship)){// tests, for all other remaining ships, if they collide with the current one
|
||||
return false; // return false, if there is a collision
|
||||
for (Battleship ship : ships) {
|
||||
for (Battleship compareShip : ships) {
|
||||
if (ships != compareShip) {
|
||||
if (ship.collidesWith(compareShip)) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -143,8 +143,8 @@ public Player addPlayer(int id) {
|
||||
public void received(MapMessage msg, int from) {
|
||||
if (state != ServerState.SET_UP)
|
||||
LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS
|
||||
else if(!verifyMap(msg,from)){
|
||||
LOGGER.log(Level.ERROR,"player submitted invalid map",state);
|
||||
else if (!verifyMap(msg, from)) {
|
||||
LOGGER.log(Level.ERROR, "player submitted invalid map", state);
|
||||
}
|
||||
else
|
||||
playerReady(getPlayerById(from), msg.getShips());
|
||||
@@ -153,11 +153,11 @@ else if(!verifyMap(msg,from)){
|
||||
/**
|
||||
* this method returns true, if the given map is valid (don't overlap or out of bound)
|
||||
*
|
||||
* @param msg the message, where the map is in
|
||||
* @param msg the message, where the map is in
|
||||
* @param playerID the player, who committed the map
|
||||
* @return true, if the map is valid
|
||||
*/
|
||||
private boolean verifyMap(MapMessage msg, int playerID){
|
||||
private boolean verifyMap(MapMessage msg, int playerID) {
|
||||
return verifyBounds(msg, playerID) && verifyOverlap(msg);
|
||||
}
|
||||
|
||||
@@ -167,11 +167,12 @@ private boolean verifyMap(MapMessage msg, int playerID){
|
||||
* @param msg is the message, where the map is in
|
||||
* @return true, if all ships are in bound
|
||||
*/
|
||||
private boolean verifyBounds(MapMessage msg, int playerID){
|
||||
private boolean verifyBounds(MapMessage msg, int playerID) {
|
||||
int mapWidth = getPlayerById(playerID).getMap().getWidth();
|
||||
int mapHeight = getPlayerById(playerID).getMap().getHeight();
|
||||
for(Battleship ship: msg.getShips()){
|
||||
if(ship.getMaxX() > mapWidth || ship.getMinX()<0 || ship.getMaxY()> mapHeight || ship.getMinY() < 0) return false;
|
||||
for (Battleship ship : msg.getShips()) {
|
||||
if (ship.getMaxX() > mapWidth || ship.getMinX() < 0 || ship.getMaxY() > mapHeight || ship.getMinY() < 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -182,27 +183,24 @@ private boolean verifyBounds(MapMessage msg, int playerID){
|
||||
* @param msg is the message, where the map is in
|
||||
* @return true, if no ships overlap
|
||||
*/
|
||||
private boolean verifyOverlap(MapMessage msg){
|
||||
List<Battleship> ships = msg.getShips();
|
||||
for(Battleship ship:ships){
|
||||
for(Battleship compareShip:ships){
|
||||
if(!(ship==compareShip)){
|
||||
if(ship.collidesWith(compareShip)){
|
||||
return false;
|
||||
}
|
||||
private boolean verifyOverlap(MapMessage msg) {
|
||||
List<Battleship> battleshipList = msg.getShips();
|
||||
for (int i = 0; i < battleshipList.size(); i++) {
|
||||
Battleship ship1 = battleshipList.get(i);
|
||||
for (int j = i + 1; j < battleshipList.size(); j++) {
|
||||
Battleship ship2 = battleshipList.get(j);
|
||||
if (ship1.collidesWith(ship2)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
/*
|
||||
List<Battleship> ships = msg.getShips(); // the list the first loop iterates through
|
||||
List<Battleship> shipsReduceList = new ArrayList<>(ships); // the second list, holds the other remaining ships
|
||||
for(Battleship ship: ships){ // iterates through the first list
|
||||
shipsReduceList.remove(ship);//remove the current ship, so it doesn't check the collision with it self
|
||||
for(Battleship battleship:shipsReduceList){ // iterates through the second list
|
||||
if(ship.collidesWith(battleship)){ //if there is a collision, it returns false
|
||||
return false;
|
||||
List<Battleship> ships = msg.getShips();
|
||||
for (Battleship ship : ships) {
|
||||
for (Battleship compareShip : ships) {
|
||||
if (ship != compareShip) {
|
||||
if (ship.collidesWith(compareShip)) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user