public class Lab4 { public static void turnRight() { Robot.turnLeft(); Robot.turnLeft(); Robot.turnLeft(); } public static void turnAround() { Robot.turnLeft(); Robot.turnLeft(); } public static void backUp() { turnAround(); Robot.move(); turnAround(); } public static void completeBars() { while (Robot.frontIsClear()) { completeOneBar(); Robot.move(); } completeOneBar(); // complete last bar } // Precondition: Robot is facing right at // bottom of a column // Postcondition: Robot fills in bar and is // at bottom of the same column public static void completeOneBar() { Robot.turnLeft(); while (!Robot.onDark()) { Robot.makeDark(); Robot.move(); } turnAround(); while (Robot.frontIsClear()) { Robot.move(); } Robot.turnLeft(); } public static void testCompleteBars1() { Robot.load("bars1.txt"); Robot.setDelay(0.025); completeBars(); } public static void testCompleteBars2() { Robot.load("bars2.txt"); Robot.setDelay(0.025); completeBars(); } public static void combinePiles() { // While the Robot is on a dark cell in // first column, move it to 2nd column while (Robot.onDark()) { moveDarkFromLeftToRight(); // after: robot returns to current position, // now on a light cell if (Robot.frontIsClear()) { Robot.move(); } } // Return to bottom left when done (optional) turnAround(); moveForwardToEdge(); turnAround(); } public static void moveDarkFromLeftToRight() { Robot.makeLight(); moveForwardToEdge(); turnRight(); Robot.move(); turnRight(); // Move down right column until you hit a // dark cell. CAUTION: There might not be any! while (Robot.frontIsClear() && !Robot.onDark()) { Robot.move(); } if (Robot.onDark()) { backUp(); } Robot.makeDark(); returnToOriginalPosition(); } public static void moveForwardToEdge() { while (Robot.frontIsClear()) { Robot.move(); } } public static void returnToOriginalPosition() { // Precondition: Robot is facing down in 2nd column moveForwardToEdge(); turnRight(); Robot.move(); turnRight(); while (Robot.frontIsClear() && !Robot.onDark()) { Robot.move(); } backUp(); } public static void testCombinePiles1() { Robot.load("piles1.txt"); Robot.setDelay(0.025); combinePiles(); } public static void testCombinePiles2() { Robot.load("piles2.txt"); Robot.setDelay(0.025); combinePiles(); } public static void connectDots() { while (hasAnotherDot()) { // If hasAnotherDot returns true, then // the robot will be facing the dot. // (i.e. hasAnotherDot has a "side effect" connectOneDot(); } } public static boolean hasAnotherDot() { // Determines if the robot has another dot // to connect to, looking to its right first, // then straight ahead, and then to its left turnRight(); if (hasDotAhead()) { return true; } Robot.turnLeft(); if (hasDotAhead()) { return true; } Robot.turnLeft(); if (hasDotAhead()) { return true; } return false; } public static boolean hasDotAhead() { // Returns true if the robot has a dot 2 steps ahead of it // (based on its current orientation) if (!Robot.frontIsClear()) // can it move 1 step? If not, no dot. { return false; } Robot.move(); if (!Robot.frontIsClear()) // can it move a 2nd step? { backUp(); // if not, backup to original position return false; // and return false (no dot) } Robot.move(); if (Robot.onDark()) // we've moved 2 steps. on dark? { backUp(); // yes, backup to original position backUp(); // and return true (there is a dot) return true; } else { backUp(); // otherwise, backup to original position backUp(); // and return false (no dot) return false; } } public static void connectOneDot() { // This method is called only if there is a dot 2 steps // from the robot's current position (in its current orientation) Robot.move(); Robot.makeDark(); // fill the cell between the two dots Robot.move(); } public static void testConnectDots1() { Robot.load("connect1.txt"); Robot.setDelay(0.025); connectDots(); } public static void testConnectDots2() { Robot.load("connect2.txt"); Robot.setDelay(0.025); connectDots(); } }