From 963d9a515fbeb91a584b7f15c59260e762036d62 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sun, 23 Feb 2020 20:10:28 +0100 Subject: [PATCH] Legger til nye metoder til Board og Robot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Legger til manglende egenskap facingDirection til Robot, med setter og getter Legger til metoder i Board for å rotere en Robot til venstre eller høyre Forenkler hasWallFacing i Board --- .../fiasko/roborally/objects/Board.java | 37 +++++++++++++------ .../fiasko/roborally/objects/Robot.java | 21 +++++++++++ 2 files changed, 47 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 25edb51..baef73e 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -82,6 +82,26 @@ public class Board { deadRobots.add(robot); } + /** + * Rotates a robot to the right + * @param robotID The id of the robot to rotate + */ + public void rotateRobotLeft(RobotID robotID) { + Robot robot = robots.get(robotID); + Direction newDirection = Direction.getLeftRotatedDirection(robot.getFacingDirection()); + robot.setFacingDirection(newDirection); + } + + /** + * Rotates a robot to the left + * @param robotID The id of the robot to rotate + */ + public void rotateRobotRight(RobotID robotID) { + Robot robot = robots.get(robotID); + Direction newDirection = Direction.getRightRotatedDirection(robot.getFacingDirection()); + robot.setFacingDirection(newDirection); + } + /** * Moves a robot one unit in a specified direction * @param robotID ID of the robot to move @@ -211,17 +231,12 @@ public class Board { if (wall == null) { return false; } - switch (wall.getDirection()) { - case NORTH_EAST: - return direction == Direction.NORTH || direction == Direction.EAST; - case NORTH_WEST: - return direction == Direction.NORTH || direction == Direction.WEST; - case SOUTH_WEST: - return direction == Direction.SOUTH || direction == Direction.WEST; - case SOUTH_EAST: - return direction == Direction.SOUTH || direction == Direction.EAST; - default: - return wall.getDirection() == direction; + int wallDirectionId = wall.getDirection().getDirectionID(); + if (wallDirectionId % 2 == 0) { + return (wallDirectionId % 8) + 1 == direction.getDirectionID() + || (((wallDirectionId - 2) + 8) % 8) + 1 == direction.getDirectionID(); + } else { + return wall.getDirection() == direction; } } diff --git a/src/main/java/inf112/fiasko/roborally/objects/Robot.java b/src/main/java/inf112/fiasko/roborally/objects/Robot.java index 12d7d1a..be9ef0a 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Robot.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Robot.java @@ -1,5 +1,6 @@ package inf112.fiasko.roborally.objects; +import inf112.fiasko.roborally.element_properties.Direction; import inf112.fiasko.roborally.element_properties.Position; import inf112.fiasko.roborally.element_properties.RobotID; @@ -13,6 +14,7 @@ public class Robot { private int lastFlagVisited = 0; private Position backupPosition; private Position currentPosition; + private Direction facingDirection; /** * Instantiates a new robot @@ -23,6 +25,7 @@ public class Robot { this.robotId = robotId; this.backupPosition = spawnPosition; this.currentPosition = spawnPosition; + this.facingDirection = Direction.NORTH; } /** @@ -107,4 +110,22 @@ public class Robot { return robotId; } + /** + * Gets the direction the robot is currently facing + * @return The direction the robot is facing + */ + public Direction getFacingDirection() { + return this.facingDirection; + } + + /** + * Sets the direction the robot is currently facing + * @param newFacingDirection The new direction the robot should be facing + */ + public void setFacingDirection(Direction newFacingDirection) { + if (newFacingDirection.getDirectionID() % 2 == 0) { + throw new IllegalArgumentException("A robot is unable to face that direction."); + } + this.facingDirection = newFacingDirection; + } }