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); Robot robot = robots.get(robotID);
Position robotPosition = robot.getPosition(); Position robotPosition = robot.getPosition();
Position newPosition = getNewPosition(robotPosition, direction); 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. //There is a wall blocking the robot. It can't proceed.
if (robotMoveIsStoppedByWall(robotPosition, newPosition, direction)) { if (robotMoveIsStoppedByWall(robotPosition, newPosition, direction)) {
return false; 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 another robot is blocking this robot's path, try to shove it.
if (hasRobotOnPosition(newPosition)) { if (hasRobotOnPosition(newPosition)) {
RobotID otherRobotID = getRobotOnPosition(newPosition); RobotID otherRobotID = getRobotOnPosition(newPosition);
@ -187,9 +187,15 @@ public class Board {
* @return True if a wall would stop its path * @return True if a wall would stop its path
*/ */
private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) { private boolean robotMoveIsStoppedByWall(Position robotPosition, Position newPosition, Direction direction) {
try {
return hasWallFacing(robotPosition, direction) || return hasWallFacing(robotPosition, direction) ||
hasWallFacing(newPosition, Direction.getReverseDirection(direction)); hasWallFacing(newPosition, Direction.getReverseDirection(direction));
} }
catch (IllegalArgumentException e) {
return false;
}
}
/** /**
* Checks if the robot is about to step outside of the board, and kills it if it does * Checks if the robot is about to step outside of the board, and kills it if it does

View File

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