mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 23:29:36 +01:00
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:
parent
0b2f6c78c0
commit
963d9a515f
@ -82,6 +82,26 @@ public class Board {
|
|||||||
deadRobots.add(robot);
|
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
|
* Moves a robot one unit in a specified direction
|
||||||
* @param robotID ID of the robot to move
|
* @param robotID ID of the robot to move
|
||||||
@ -211,16 +231,11 @@ public class Board {
|
|||||||
if (wall == null) {
|
if (wall == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
switch (wall.getDirection()) {
|
int wallDirectionId = wall.getDirection().getDirectionID();
|
||||||
case NORTH_EAST:
|
if (wallDirectionId % 2 == 0) {
|
||||||
return direction == Direction.NORTH || direction == Direction.EAST;
|
return (wallDirectionId % 8) + 1 == direction.getDirectionID()
|
||||||
case NORTH_WEST:
|
|| (((wallDirectionId - 2) + 8) % 8) + 1 == direction.getDirectionID();
|
||||||
return direction == Direction.NORTH || direction == Direction.WEST;
|
} else {
|
||||||
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;
|
return wall.getDirection() == direction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package inf112.fiasko.roborally.objects;
|
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.Position;
|
||||||
import inf112.fiasko.roborally.element_properties.RobotID;
|
import inf112.fiasko.roborally.element_properties.RobotID;
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ public class Robot {
|
|||||||
private int lastFlagVisited = 0;
|
private int lastFlagVisited = 0;
|
||||||
private Position backupPosition;
|
private Position backupPosition;
|
||||||
private Position currentPosition;
|
private Position currentPosition;
|
||||||
|
private Direction facingDirection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new robot
|
* Instantiates a new robot
|
||||||
@ -23,6 +25,7 @@ public class Robot {
|
|||||||
this.robotId = robotId;
|
this.robotId = robotId;
|
||||||
this.backupPosition = spawnPosition;
|
this.backupPosition = spawnPosition;
|
||||||
this.currentPosition = spawnPosition;
|
this.currentPosition = spawnPosition;
|
||||||
|
this.facingDirection = Direction.NORTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,4 +110,22 @@ public class Robot {
|
|||||||
return robotId;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user