Added extendend client-side ship check
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParseException;
|
||||||
import pp.battleship.message.server.GameDetails;
|
import pp.battleship.message.server.GameDetails;
|
||||||
import pp.battleship.model.Battleship;
|
import pp.battleship.model.Battleship;
|
||||||
|
import pp.battleship.model.IntPoint;
|
||||||
import pp.battleship.model.ShipMap;
|
import pp.battleship.model.ShipMap;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -20,6 +21,7 @@
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.System.Logger;
|
import java.lang.System.Logger;
|
||||||
import java.lang.System.Logger.Level;
|
import java.lang.System.Logger.Level;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,6 +63,7 @@ public boolean fits(GameDetails details) {
|
|||||||
int numShips = details.getShipNums().values().stream().mapToInt(Integer::intValue).sum();
|
int numShips = details.getShipNums().values().stream().mapToInt(Integer::intValue).sum();
|
||||||
if (numShips != ships.size())
|
if (numShips != ships.size())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (var e : details.getShipNums().entrySet()) {
|
for (var e : details.getShipNums().entrySet()) {
|
||||||
final int shipLength = e.getKey();
|
final int shipLength = e.getKey();
|
||||||
final int requiredNum = e.getValue();
|
final int requiredNum = e.getValue();
|
||||||
@@ -70,6 +73,20 @@ public boolean fits(GameDetails details) {
|
|||||||
if (requiredNum != actualNum)
|
if (requiredNum != actualNum)
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user