mirror of
https://athene2.informatik.unibw-muenchen.de/progproj/gruppen-ht24/Gruppe-02.git
synced 2024-11-25 06:59:47 +01:00
Task 8
This commit is contained in:
parent
1ac55a9570
commit
1f75f7bf30
@ -140,10 +140,19 @@ public class ServerGameLogic implements ClientInterpreter {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void received(MapMessage msg, int from) {
|
public void received(MapMessage msg, int from) {
|
||||||
|
List<Battleship> ships = msg.getShips();
|
||||||
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
|
else
|
||||||
playerReady(getPlayerById(from), msg.getShips());
|
|
||||||
|
|
||||||
|
if (!shipsValid(ships)){
|
||||||
|
LOGGER.log(Level.ERROR, "ship placement by player {0} is Invalid", from);
|
||||||
|
send(getPlayerById(from),null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
playerReady(getPlayerById(from), msg.getShips());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -217,4 +226,35 @@ public class ServerGameLogic implements ClientInterpreter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isInBounds(Battleship ship){
|
||||||
|
return ship.getMinX() >= 0 && ship.getMaxX() < config.getMapWidth() &&
|
||||||
|
ship.getMinY() >= 0 && ship.getMaxY() < config.getMapHeight();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private boolean shipsValid(List<Battleship> ships) {
|
||||||
|
|
||||||
|
Set<IntPoint> occupied = new HashSet<>();
|
||||||
|
|
||||||
|
for (Battleship ship : ships) {
|
||||||
|
if (!isInBounds(ship)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int x = ship.getMinX(); x <= ship.getMaxX(); x++) {
|
||||||
|
for (int y = ship.getMinY(); y <= ship.getMaxY(); y++) {
|
||||||
|
IntPoint point = new IntPoint(x,y);
|
||||||
|
if (!occupied.add(point)){
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user