Fikser bug der robot kan gå forbi vegger som er på kanten av brettet.

This commit is contained in:
torlunjen 2020-03-10 18:18:32 +01:00
parent 4d309d4321
commit a86b4efbb7
2 changed files with 13 additions and 26 deletions

View File

@ -133,14 +133,14 @@ public class Board {
Robot robot = robots.get(robotID);
Position robotPosition = robot.getPosition();
Position newPosition = getNewPosition(robotPosition, direction);
//Robot tried to go outside of the map. Kill it.
if (killRobotIfGoesOutsideMap(robot, newPosition)) {
return true;
}
//There is a wall blocking the robot. It can't proceed.
if (robotMoveIsStoppedByWall(robotPosition, newPosition, direction)) {
return false;
}
//Robot tried to go outside of the map. Kill it.
if (killRobotIfGoesOutsideMap(robot, newPosition)) {
return true;
}
//If another robot is blocking this robot's path, try to shove it.
if (hasRobotOnPosition(newPosition)) {
RobotID otherRobotID = getRobotOnPosition(newPosition);
@ -187,8 +187,14 @@ public class Board {
* @return True if a wall would stop its path
*/
private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) {
return hasWallFacing(robotPosition, direction) ||
hasWallFacing(newPosition, Direction.getReverseDirection(direction));
try {
return hasWallFacing(robotPosition, direction) ||
hasWallFacing(newPosition, Direction.getReverseDirection(direction));
}
catch (IllegalArgumentException e) {
return false;
}
}
/**

View File

@ -105,46 +105,27 @@ public class RoboRallyGame implements IDrawableGame {
private void runGameLoop() throws InterruptedException {
TimeUnit.SECONDS.sleep(3);
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);
makeMove(RobotID.ROBOT_2, Action.MOVE_1);
}
/**