From f22e577180084ab0158689ee37e8998c65e7c51d Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 10 Mar 2020 18:35:15 +0100 Subject: [PATCH] Legger til forbedring av posisjonssjekking i Board --- .../fiasko/roborally/objects/Board.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/objects/Board.java b/src/main/java/inf112/fiasko/roborally/objects/Board.java index 3ecc739..8326711 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -187,14 +187,20 @@ public class Board { * @return True if a wall would stop its path */ private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) { - try { - return hasWallFacing(robotPosition, direction) || - hasWallFacing(newPosition, Direction.getReverseDirection(direction)); - } - catch (IllegalArgumentException e) { - return false; - } + return hasWallFacing(robotPosition, direction) || (isValidPosition(newPosition) && + hasWallFacing(newPosition, Direction.getReverseDirection(direction))); + } + /** + * Checks whether a given position is valid + * @param position The position to test + * @return True if the position is valid. False otherwise + */ + private boolean isValidPosition(Position position) { + return position.getXCoordinate() >= 0 + && position.getXCoordinate() < boardWidth + && position.getYCoordinate() >= 0 + && position.getYCoordinate() < boardHeight; } /** @@ -204,10 +210,7 @@ public class Board { * @return True if the robot was killed for leaving the board */ private boolean killRobotIfGoesOutsideMap(Robot robot, Position newPosition) { - if (newPosition.getXCoordinate() < 0 - || newPosition.getXCoordinate() >= boardWidth - || newPosition.getYCoordinate() < 0 - || newPosition.getYCoordinate() >= boardHeight) { + if (!isValidPosition(newPosition)) { killRobot(robot); return true; }