mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2024-11-28 23:39:45 +01:00
testValidMap
This commit is contained in:
parent
91b24db6ca
commit
3386d395fe
@ -7,6 +7,13 @@
|
|||||||
|
|
||||||
package pp.battleship.game.server;
|
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.BattleshipConfig;
|
||||||
import pp.battleship.message.client.ClientInterpreter;
|
import pp.battleship.message.client.ClientInterpreter;
|
||||||
import pp.battleship.message.client.MapMessage;
|
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.Battleship;
|
||||||
import pp.battleship.model.IntPoint;
|
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.
|
* Controls the server-side game logic for Battleship.
|
||||||
* Manages game states, player interactions, and message handling.
|
* Manages game states, player interactions, and message handling.
|
||||||
@ -134,6 +134,7 @@ public class ServerGameLogic implements ClientInterpreter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the reception of a MapMessage.
|
* Handles the reception of a MapMessage.
|
||||||
|
* Also tests valid ship placement on the Map.
|
||||||
*
|
*
|
||||||
* @param msg the received MapMessage
|
* @param msg the received MapMessage
|
||||||
* @param from the ID of the sender client
|
* @param from the ID of the sender client
|
||||||
@ -142,7 +143,16 @@ public class ServerGameLogic implements ClientInterpreter {
|
|||||||
public void received(MapMessage msg, int from) {
|
public void received(MapMessage msg, int from) {
|
||||||
if (state != ServerState.SET_UP)
|
if (state != ServerState.SET_UP)
|
||||||
LOGGER.log(Level.ERROR, "playerReady not allowed in {0}", state); //NON-NLS
|
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());
|
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