public class SwapSlides { private Robot robot; private static final int DELAY = 150; public static void main(String[] args) { SwapSlides swap = new SwapSlides(); swap.createEnviroment(); swap.swapAll(); } private void createEnviroment() { RobotWorld world = RobotWorld.load("src/swap.txt"); robot = new Robot(1, world.getNumCols() / 2 , Robot.SOUTH, world); robot.setDelay(DELAY); } //swapping colors on all across cells in the corridor //before: the robot is located at the beginning of the corridor, facing the corridor //after: the robot has the same location and facing the same direction public void swapAll() { boolean finished = false; while (!finished) { swapTwoCells(1); if (!atEndOfCorridor()) robot.move(); else finished = true; } returnToStartPosition(); } //returns true if the robot is at the end of the corridor, otherwise false private boolean atEndOfCorridor() { return !robot.frontIsClear(); } // swapping colors of two across cell at position n //before: robot is in the corridor facing the corridor //after: robot is in the corridor facing the corridor private void swapTwoCells(int n) { if (leftCellIsDark(n) != rightCellIsDark(n)) changeColors(n); } //returns true if the cell on left side of the robot is dark, otherwise false //before: robot is in the corridor facing the corridor //after: robot is in the corridor facing the corridor private boolean leftCellIsDark(int n) { robot.turnLeft(); steps(n); boolean isDark = robot.onDark(); turnAround(); steps(n); robot.turnLeft(); return isDark; } // private void steps(int n) { for (int i = 0; i < n; i = i + 1) robot.move(); } //before: none //after: robot is facing the opposite direction private void turnAround() { robot.turnLeft(); robot.turnLeft(); } //returns true if the cell on right side of the robot is dark, otherwise false //before: robot is in the corridor facing the corridor //after: robot is in the corridor facing the corridor private boolean rightCellIsDark(int n) { turnRight(); steps(n); boolean isDark = robot.onDark(); turnAround(); steps(n); turnRight(); return isDark; } //before: none //after: robot has turned 90 degree to right private void turnRight() { turnAround(); robot.turnLeft(); } //change colors of the cells on left side and on right side of the robot //before: robot is in the corridor facing the corridor //after: robot is in the corridor facing the corridor private void changeColors(int n) { changeColorOfLeftCell(n); changeColorOfRightCell(n); } //change color of the cell on left side of the robot //before: robot is in the corridor facing the corridor //after: robot is in the corridor facing the corridor private void changeColorOfLeftCell(int n) { robot.turnLeft(); steps(n); switchColor(); turnAround(); steps(n); robot.turnLeft(); } //change color of the cell on right side of the robot //before: robot is in the corridor facing the corridor //after: robot is in the corridor facing the corridor private void changeColorOfRightCell(int n) { turnRight(); steps(n); switchColor(); turnAround(); steps(n); turnRight(); } //switch color of the cell //before: none //after: if the cell the robot is at was dark it has become light, // and if the cell was light it has become dark private void switchColor() { if (robot.onDark()) robot.makeLight(); else robot.makeDark(); } //before: robot is at end of the corridor, facing the wall //after: robot is at beginning of the corridor, facing the corridor private void returnToStartPosition() { turnAround(); goToEndOfCorridor(); turnAround(); } //before: robot is in the corridor, facing the cooridor //after: robot is at the end of the corridor, facing the wall private void goToEndOfCorridor() { while (!atEndOfCorridor()) { robot.move(); } } }