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 a5987fa..3f08a46 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 @@ -7,6 +7,13 @@ package pp.battleship.game.server; +import java.lang.System.Logger; +import java.lang.System.Logger.Level; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + import pp.battleship.BattleshipConfig; import pp.battleship.message.client.ClientInterpreter; import pp.battleship.message.client.MapMessage; @@ -18,13 +25,6 @@ import pp.battleship.message.server.StartBattleMessage; import pp.battleship.model.Battleship; import pp.battleship.model.IntPoint; -import java.lang.System.Logger; -import java.lang.System.Logger.Level; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - /** * Controls the server-side game logic for Battleship. * Manages game states, player interactions, and message handling. @@ -134,6 +134,7 @@ public class ServerGameLogic implements ClientInterpreter { /** * Handles the reception of a MapMessage. + * Also tests valid ship placement on the Map. * * @param msg the received MapMessage * @param from the ID of the sender client @@ -142,7 +143,16 @@ public class ServerGameLogic implements ClientInterpreter { 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 + + + List ships = msg.getShips(); + + if (! mapTest(ships)){ + LOGGER.log(Level.ERROR, "Geladene Karte von Spieler {0} enthält unzulässige Schiffe", from); + send(getPlayerById(from),null); + return; + } + playerReady(getPlayerById(from), msg.getShips()); } @@ -217,4 +227,26 @@ public class ServerGameLogic implements ClientInterpreter { } } } -} + //Aufgabe 8 + + private boolean isInBounds(Battleship ship){ + return ship.getMinX() >= 0 && ship.getMaxX() < config.getMapWidth() && + ship.getMinY() >= 0 && ship.getMaxY() < config.getMapHeight(); + } + + + + private boolean mapTest(List ships) { + Set belegt = new HashSet<>(); + return ships.stream().allMatch(ship -> isInBounds(ship) && + ship.getPositions().stream().allMatch(belegt::add)); + } + + // public void displayError(String message) { + // System.out.println("Fehlermeldung: " + message); + // // Optional: GUI-Element aktualisieren, um die Nachricht anzuzeigen + // } + + } + +