From 46f75188cb6de93565843e0cd2e561ba08d9793a Mon Sep 17 00:00:00 2001 From: "Hanno Fleischer hanno.fleischer@unibw.de" Date: Wed, 2 Oct 2024 17:35:34 +0200 Subject: [PATCH] fixed the code for exercise 8 fixed the code so that it uses a for each loop instead of an for int i and adjusted the part for validating ship overlaping to use the isCollidingWith method of Battleship --- .../battleship/game/client/EditorState.java | 29 +++++-------------- .../game/server/ServerGameLogic.java | 21 +++++++++++--- 2 files changed, 25 insertions(+), 25 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 27058c03..3a0eb69b 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 @@ -17,6 +17,7 @@ import java.io.IOException; import java.lang.System.Logger.Level; import java.util.Arrays; +import java.util.List; import static pp.battleship.Resources.lookup; import static pp.battleship.game.client.ClientGameLogic.LOGGER; @@ -264,27 +265,13 @@ private boolean checkMapToLoad(ShipMapDTO dto) { } // check if ships overlap - int[][] tempMap = new int[mapWidth][mapHeight]; - for (int[] row : tempMap) - Arrays.fill(row, 0); - dto.getShips().forEach(s -> { - int deltaX = s.getMaxX() - s.getMinX(); - int deltaY = s.getMaxY() - s.getMinY(); - int lastX = s.getX(); - int lastY = s.getY(); - tempMap[lastX][lastY] += 1; - for (int i = 0; i < deltaX - 1; i++) - for (int j = 0; j < deltaY - 1; j++) { - tempMap[lastX + (deltaX / Math.abs(deltaX))][lastY + (deltaY / Math.abs(deltaY))] += 1; - lastX = lastX + (deltaX / Math.abs(deltaX)); - lastY = lastY + (deltaY / Math.abs(deltaY)); - } - }); - for (int[] row : tempMap) { - for (int cell : row) { - if (cell > 1) { - LOGGER.log(Level.ERROR, "There are multiple ships on one position"); - return false; + List ships = dto.getShips(); + for(Battleship ship:ships){ + for(Battleship compareShip:ships){ + if(!(ship==compareShip)){ + if(ship.collidesWith(compareShip)){ + return false; + } } } } 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 41ddde08..f9d9f3c6 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 @@ -164,8 +164,7 @@ private boolean checkMap(MapMessage msg, int from) { int mapHeight = getPlayerById(from).getMap().getHeight(); // check if ship is out of bounds - for (int i = 0; i < msg.getShips().size(); i++){ - Battleship battleship = msg.getShips().get(i); + for (Battleship battleship : msg.getShips()){ if (battleship.getMaxX() >= mapWidth || battleship.getMinX() < 0 || battleship.getMaxY() >= mapHeight || battleship.getMinY() < 0) { LOGGER.log(Level.ERROR, "Ship is out of bounds ({0})", battleship.toString()); return false; @@ -173,7 +172,21 @@ private boolean checkMap(MapMessage msg, int from) { } // check if ships overlap - int[][] tempMap = new int[mapWidth][mapHeight]; + List ships = msg.getShips(); + for(Battleship ship:ships){ + for(Battleship compareShip:ships){ + if(!(ship==compareShip)){ + if(ship.collidesWith(compareShip)){ + return false; + } + } + } + } + + + + + /*int[][] tempMap = new int[mapWidth][mapHeight]; for (int[] row : tempMap) Arrays.fill(row, 0); msg.getShips().forEach(s -> { @@ -196,7 +209,7 @@ private boolean checkMap(MapMessage msg, int from) { return false; } } - } + }*/ return true; }