Added extendend client-side ship check

This commit is contained in:
Cedric Beck
2024-10-02 21:09:48 +02:00
parent af221ad693
commit 6496a5a6b7

View File

@@ -12,6 +12,7 @@
import com.google.gson.JsonParseException;
import pp.battleship.message.server.GameDetails;
import pp.battleship.model.Battleship;
import pp.battleship.model.IntPoint;
import pp.battleship.model.ShipMap;
import java.io.File;
@@ -20,6 +21,7 @@
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.ArrayList;
import java.util.List;
/**
@@ -61,6 +63,7 @@ public boolean fits(GameDetails details) {
int numShips = details.getShipNums().values().stream().mapToInt(Integer::intValue).sum();
if (numShips != ships.size())
return false;
for (var e : details.getShipNums().entrySet()) {
final int shipLength = e.getKey();
final int requiredNum = e.getValue();
@@ -70,6 +73,20 @@ public boolean fits(GameDetails details) {
if (requiredNum != actualNum)
return false;
}
List<IntPoint> occupied = new ArrayList<>();
for (BattleshipDTO dto: ships){
Battleship battleship = dto.toBattleship();
int x = battleship.getX();
int y = battleship.getY();
for(int i = 0; i < battleship.getLength(); i++){
if(x >= 0 && x < details.getWidth() && y >= 0 && y < details.getHeight() && !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;
}