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:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user