corrected 'EditorState' and 'ServerGameLogic'
This commit is contained in:
@@ -258,31 +258,19 @@ public void loadMap(File file) throws IOException {
|
||||
* @return true, if the map is valid
|
||||
*/
|
||||
private boolean verifyMap(ShipMapDTO dto){ //MapMessage msg, int playerID
|
||||
int mapWidth = dto.getWidth();
|
||||
int mapHeight = dto.getHeight();
|
||||
return verifyBounds(dto,mapWidth,mapHeight) && verifyOverlap(dto);
|
||||
return verifyBounds(dto) && verifyOverlap(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks, whether a ship is out of the grid
|
||||
* @param dto is the Map, where the ships are in
|
||||
* @param mapWidth the width of the map
|
||||
* @param mapHeight the height ig the map
|
||||
* @return true, if all ships are in bound
|
||||
*/
|
||||
private boolean verifyBounds(ShipMapDTO dto, int mapWidth,int mapHeight){
|
||||
private boolean verifyBounds(ShipMapDTO dto){
|
||||
for(Battleship ship: dto.getShips()){
|
||||
int x = ship.getX();
|
||||
int y = ship.getY();
|
||||
Rotation r = ship.getRot();
|
||||
if(0 > x || 0> y || x> mapWidth|| y> mapHeight){ // tests, whether the nose of the ship is in the gri
|
||||
return false;
|
||||
}
|
||||
int x2 = x + ship.getLength()* r.dx();
|
||||
int y2 = y + ship.getLength() * r.dy();
|
||||
if(0 > x2 || 0> y2 || x2> mapWidth|| y2> mapHeight){ //tests, whether the back of the ship is in the grid
|
||||
return false;
|
||||
}
|
||||
int mapWidth = dto.getWidth();
|
||||
int mapHeight = dto.getHeight();
|
||||
if(ship.getMaxX()>mapWidth || ship.getMinX()<0 || ship.getMaxY()> mapHeight || ship.getMinY() < 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -294,10 +282,23 @@ private boolean verifyBounds(ShipMapDTO dto, int mapWidth,int mapHeight){
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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 colides with itself
|
||||
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
|
||||
@@ -305,6 +306,7 @@ private boolean verifyOverlap(ShipMapDTO dto){
|
||||
}
|
||||
}
|
||||
return true;
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -158,31 +158,20 @@ else if(!verifyMap(msg,from)){
|
||||
* @return true, if the map is valid
|
||||
*/
|
||||
private boolean verifyMap(MapMessage msg, int playerID){
|
||||
int mapWidth = getPlayerById(playerID).getMap().getWidth();
|
||||
int mapHeight = getPlayerById(playerID).getMap().getHeight();
|
||||
return verifyBounds(msg,mapWidth,mapHeight) && verifyOverlap(msg);
|
||||
return verifyBounds(msg, playerID) && verifyOverlap(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks, whether a ship is out of the grid
|
||||
*
|
||||
* @param msg is the message, where the map is in
|
||||
* @param mapWidth is the map width
|
||||
* @param mapHeight is the map height
|
||||
* @return true, if all ships are in bound
|
||||
*/
|
||||
private boolean verifyBounds(MapMessage msg, int mapWidth,int mapHeight){
|
||||
private boolean verifyBounds(MapMessage msg, int playerID){
|
||||
int mapWidth = getPlayerById(playerID).getMap().getWidth();
|
||||
int mapHeight = getPlayerById(playerID).getMap().getHeight();
|
||||
for(Battleship ship: msg.getShips()){
|
||||
int x = ship.getX();
|
||||
int y = ship.getY();
|
||||
Rotation r = ship.getRot();
|
||||
if(0 > x || 0> y || x> mapWidth|| y> mapHeight){ // checks, whether the nose is in the grid
|
||||
return false;
|
||||
}
|
||||
int x2 = x + ship.getLength()* r.dx();
|
||||
int y2 = y + ship.getLength() * r.dy();
|
||||
if(0 > x2 || 0> y2 || x2> mapWidth|| y2> mapHeight){ // check, whether the back of the ship is in the grid
|
||||
return false;
|
||||
}
|
||||
if(ship.getMaxX() > mapWidth || ship.getMinX()<0 || ship.getMaxY()> mapHeight || ship.getMinY() < 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -194,6 +183,19 @@ private boolean verifyBounds(MapMessage msg, int mapWidth,int mapHeight){
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
@@ -206,7 +208,7 @@ private boolean verifyOverlap(MapMessage msg){
|
||||
}
|
||||
return true;
|
||||
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user