Legger til nye metoder til Board og Robot

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
This commit is contained in:
Kristian Knarvik 2020-02-23 20:10:28 +01:00
parent 0b2f6c78c0
commit 963d9a515f
2 changed files with 47 additions and 11 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}