Adds method that moves robot according to action enum

This commit is contained in:
Torbjørn Lunde Jensen 2020-03-10 17:29:36 +01:00
parent 7a5b4fccd2
commit cba469d09f
2 changed files with 111 additions and 42 deletions

View File

@ -115,6 +115,14 @@ public class Board {
moveRobot(robotID, robots.get(robotID).getFacingDirection());
}
/**
* Moves a robot one unit backwards according to the direction it's currently facing
* @param robotID The robot to move
*/
public void reverseRobot(RobotID robotID) {
moveRobot(robotID, Direction.getReverseDirection(robots.get(robotID).getFacingDirection()));
}
/**
* Moves a robot one unit in a specified direction
* @param robotID ID of the robot to move

View File

@ -1,5 +1,6 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Action;
import inf112.fiasko.roborally.element_properties.Position;
import inf112.fiasko.roborally.element_properties.RobotID;
import inf112.fiasko.roborally.utility.BoardLoaderUtil;
@ -52,6 +53,14 @@ public class RoboRallyGame implements IDrawableGame {
return gameBoard.getAliveRobots();
}
/**
* Makes the game thread wait a given time amount before continuing.
* @throws InterruptedException If interrupted while trying to sleep.
*/
private void sleep() throws InterruptedException {
long cycleDelay = 600;
TimeUnit.MILLISECONDS.sleep(cycleDelay);
}
/**
* Initializes the game with a debugging board
*/
@ -94,48 +103,100 @@ public class RoboRallyGame implements IDrawableGame {
* @throws InterruptedException If interrupted while trying to sleep
*/
private void runGameLoop() throws InterruptedException {
long cycleDelay = 600;
TimeUnit.SECONDS.sleep(3);
gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotLeft(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2);
makeMove(RobotID.ROBOT_1, Action.MOVE_1);
makeMove(RobotID.ROBOT_1, Action.MOVE_2);
makeMove(RobotID.ROBOT_1, Action.BACK_UP);
makeMove(RobotID.ROBOT_1, Action.BACK_UP);
makeMove(RobotID.ROBOT_1, Action.MOVE_3);
makeMove(RobotID.ROBOT_1, Action.ROTATE_LEFT);
makeMove(RobotID.ROBOT_1, Action.U_TURN);
makeMove(RobotID.ROBOT_1, Action.ROTATE_RIGHT);
makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT);
makeMove(RobotID.ROBOT_2, Action.MOVE_3);
makeMove(RobotID.ROBOT_2, Action.MOVE_3);
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
makeMove(RobotID.ROBOT_2, Action.U_TURN);
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
makeMove(RobotID.ROBOT_2, Action.MOVE_3);
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
makeMove(RobotID.ROBOT_2, Action.BACK_UP);
makeMove(RobotID.ROBOT_2, Action.ROTATE_LEFT);
makeMove(RobotID.ROBOT_2, Action.U_TURN);
}
/**
* Makes the given robot move according to to the action input.
* @param robotID The ID of the robot to move.
* @param action The specific movement the robot is to take.
* @throws InterruptedException If interrupted wile trying to sleep.
*/
private void makeMove(RobotID robotID, Action action) throws InterruptedException {
if (!gameBoard.isRobotAlive(robotID)) {
return;
}
sleep();
switch (action) {
case MOVE_1:
moveForward(robotID);
break;
case MOVE_2:
moveForward(robotID);
moveForward(robotID);
break;
case MOVE_3:
moveForward(robotID);
moveForward(robotID);
moveForward(robotID);
break;
case ROTATE_RIGHT:
gameBoard.rotateRobotRight(robotID);
break;
case ROTATE_LEFT:
gameBoard.rotateRobotLeft(robotID);
break;
case U_TURN:
gameBoard.rotateRobotLeft(robotID);
gameBoard.rotateRobotLeft(robotID);
break;
case BACK_UP:
gameBoard.reverseRobot(robotID);
break;
}
}
/**
* Helper method for makeMove. Takes care of movement forward of given robot.
* @param robotID ID of the given robot.
* @throws InterruptedException If interrupted wile sleeping.
*/
private void moveForward(RobotID robotID) throws InterruptedException {
if (!gameBoard.isRobotAlive(robotID)) {
return;
}
sleep();
gameBoard.moveRobotForward(robotID);
}
}