Added extendend client-side ship check
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user