Added ex. 8
This commit is contained in:
@@ -238,9 +238,6 @@ public void loadMap(File file) throws IOException {
|
|||||||
final ShipMapDTO dto = ShipMapDTO.loadFrom(file);
|
final ShipMapDTO dto = ShipMapDTO.loadFrom(file);
|
||||||
if (!dto.fits(logic.getDetails()))
|
if (!dto.fits(logic.getDetails()))
|
||||||
throw new IOException(lookup("map.doesnt.fit"));
|
throw new IOException(lookup("map.doesnt.fit"));
|
||||||
//TODO: Server JSON Check
|
|
||||||
|
|
||||||
|
|
||||||
ownMap().clear();
|
ownMap().clear();
|
||||||
dto.getShips().forEach(ownMap()::add);
|
dto.getShips().forEach(ownMap()::add);
|
||||||
harbor().clear();
|
harbor().clear();
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
package pp.battleship.game.client;
|
package pp.battleship.game.client;
|
||||||
|
|
||||||
|
import pp.battleship.message.server.GameDetails;
|
||||||
import pp.battleship.message.server.StartBattleMessage;
|
import pp.battleship.message.server.StartBattleMessage;
|
||||||
|
|
||||||
import java.lang.System.Logger.Level;
|
import java.lang.System.Logger.Level;
|
||||||
@@ -38,4 +39,11 @@ public void receivedStartBattle(StartBattleMessage msg) {
|
|||||||
logic.setInfoText(msg.getInfoTextKey());
|
logic.setInfoText(msg.getInfoTextKey());
|
||||||
logic.setState(new BattleState(logic, msg.isMyTurn()));
|
logic.setState(new BattleState(logic, msg.isMyTurn()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void receivedGameDetails(GameDetails details) {
|
||||||
|
ClientGameLogic.LOGGER.log(Level.WARNING, "Invalid Map"); //NON-NLS
|
||||||
|
logic.setInfoText("invalid.map");
|
||||||
|
logic.setState(new EditorState(logic));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -142,8 +142,38 @@ public Player addPlayer(int id) {
|
|||||||
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
|
else{
|
||||||
playerReady(getPlayerById(from), msg.getShips());
|
if(checkMap(msg.getShips())){
|
||||||
|
playerReady(getPlayerById(from), msg.getShips());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
LOGGER.log(Level.WARNING, "Invalid Map sent from player {0}", from); //NON-NLS
|
||||||
|
send(players.get(from), new GameDetails(config));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkMap(List<Battleship> ships){
|
||||||
|
//Anzahl
|
||||||
|
int numShips = config.getShipNums().values().stream().mapToInt(Integer::intValue).sum();
|
||||||
|
if (numShips != ships.size()) return false;
|
||||||
|
|
||||||
|
List<IntPoint> occupied = new ArrayList<>();
|
||||||
|
//Every Ship Position Is in Map Height/Width
|
||||||
|
//Überlappen
|
||||||
|
for (Battleship battleship: ships){
|
||||||
|
int x = battleship.getX();
|
||||||
|
int y = battleship.getY();
|
||||||
|
for(int i = 0; i < battleship.getLength(); i++){
|
||||||
|
if(x >= 0 && x < config.getMapWidth() && y >= 0 && y < config.getMapHeight() && !occupied.contains(new IntPoint(x,y))){
|
||||||
|
occupied.add(new IntPoint(x,y));
|
||||||
|
x += battleship.getRot().dx();
|
||||||
|
y += battleship.getRot().dy();
|
||||||
|
}
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -37,3 +37,4 @@ dialog.error=Error
|
|||||||
dialog.question=Question
|
dialog.question=Question
|
||||||
port.must.be.integer=Port must be an integer number
|
port.must.be.integer=Port must be an integer number
|
||||||
map.doesnt.fit=The map doesn't fit to this game
|
map.doesnt.fit=The map doesn't fit to this game
|
||||||
|
invalid.map=Invalid Map!
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ host.name=Host
|
|||||||
port.number=Port
|
port.number=Port
|
||||||
wait.its.not.your.turn=Warte, Du bist nicht dran!!
|
wait.its.not.your.turn=Warte, Du bist nicht dran!!
|
||||||
menu.quit=Spiel beenden
|
menu.quit=Spiel beenden
|
||||||
menu.return-to-game=Zurück zum Spiel
|
menu.return-to-game=Zur<EFBFBD>ck zum Spiel
|
||||||
menu.sound-enabled=Sound eingeschaltet
|
menu.sound-enabled=Sound eingeschaltet
|
||||||
menu.map.load=Karte von Datei laden...
|
menu.map.load=Karte von Datei laden...
|
||||||
menu.map.save=Karte in Datei speichern...
|
menu.map.save=Karte in Datei speichern...
|
||||||
@@ -37,3 +37,4 @@ dialog.error=Fehler
|
|||||||
dialog.question=Frage
|
dialog.question=Frage
|
||||||
port.must.be.integer=Der Port muss eine ganze Zahl sein
|
port.must.be.integer=Der Port muss eine ganze Zahl sein
|
||||||
map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel
|
map.doesnt.fit=Diese Karte passt nicht zu diesem Spiel
|
||||||
|
invalid.map=Ung<EFBFBD>ltige Karte!
|
||||||
|
|||||||
Reference in New Issue
Block a user