mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2024-11-28 21:19:44 +01:00
testValidMap
This commit is contained in:
parent
91b24db6ca
commit
3386d395fe
@ -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<Battleship> 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<Battleship> ships) {
|
||||
Set<IntPoint> 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
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user