From 175059a6e1b922b3c6f4a3fc22c5f09fce80bc86 Mon Sep 17 00:00:00 2001 From: Benjamin Feyer Date: Wed, 2 Oct 2024 17:41:06 +0200 Subject: [PATCH] corrected 'EditorState' and 'ServerGameLogic' --- .../battleship/game/client/EditorState.java | 38 ++++++++++--------- .../game/server/ServerGameLogic.java | 38 ++++++++++--------- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/EditorState.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/EditorState.java index 02600b46..c42d0bd4 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/client/EditorState.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/client/EditorState.java @@ -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 ships = dto.getShips(); + for(Battleship ship : ships){ + for(Battleship compareShip : ships){ + if(!(ships==compareShip)){ + if(ship.collidesWith(compareShip)){ + return false; + } + } + } + } + return true; + + /* List ships = dto.getShips(); List 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; + */ } /** diff --git a/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java b/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java index 6360ddf0..cfc7fd6e 100644 --- a/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java +++ b/Projekte/battleship/model/src/main/java/pp/battleship/game/server/ServerGameLogic.java @@ -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 ships = msg.getShips(); + for(Battleship ship:ships){ + for(Battleship compareShip:ships){ + if(!(ship==compareShip)){ + if(ship.collidesWith(compareShip)){ + return false; + } + } + } + } + return true; + + /* List ships = msg.getShips(); // the list the first loop iterates through List 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; - + */ } /**