fixed bugs in the JSON validation

- the method isWithinBounds(Battleship ship) couldn't check if the ship is the bounds of the map
This commit is contained in:
Daniel Grigencha
2024-10-05 13:20:20 +02:00
parent a3b5452fb9
commit 046707642f
2 changed files with 18 additions and 16 deletions

View File

@@ -305,14 +305,14 @@ private boolean inBounds(ShipMapDTO dto) {
* @return true if the ship is within bounds, false otherwise
*/
private boolean isWithinBounds(Battleship ship, int width, int height) {
int x1 = ship.getX();
int y1 = ship.getY();
Rotation r = ship.getRot();
int x2 = x1 + ship.getLength() * r.dx();
int y2 = y1 + ship.getLength() * r.dy();
return x1 >= 0 && y1 >= 0 && x1 <= width && y1 <= height &&
x2 >= 0 && y2 >= 0 && x2 <= width && y2 <= height;
int minX = ship.getMinX();
int maxX = ship.getMaxX();
int minY = ship.getMinY();
int maxY = ship.getMaxY();
return minX >= 0 && minX < width &&
minY >= 0 && minY < height &&
maxX >= 0 && maxX < width &&
maxY >= 0 && maxY < height;
}
/**

View File

@@ -185,14 +185,16 @@ private boolean inBounds(List<Battleship> ships) {
* @return true if the ship is within bounds, false otherwise
*/
private boolean isWithinBounds(Battleship ship) {
int x1 = ship.getX();
int y1 = ship.getY();
Rotation r = ship.getRot();
int x2 = x1 + ship.getLength() * r.dx();
int y2 = y1 + ship.getLength() * r.dy();
return x1 >= 0 && y1 >= 0 && x1 <= config.getMapWidth() && y1 <= config.getMapHeight() &&
x2 >= 0 && y2 >= 0 && x2 <= config.getMapWidth() && y2 <= config.getMapHeight();
int minX = ship.getMinX();
int maxX = ship.getMaxX();
int minY = ship.getMinY();
int maxY = ship.getMaxY();
int width = config.getMapWidth();
int height = config.getMapHeight();
return minX >= 0 && minX < width &&
minY >= 0 && minY < height &&
maxX >= 0 && maxX < width &&
maxY >= 0 && maxY < height;
}
/**