public class Rooms { public static void carpetRooms() { carpetRoom(); carpetRoom(); carpetRoom(); carpetRoom(); carpetRoom(); carpetRoom(); carpetRoom(); carpetRoom(); } // before: facing east, bottom row, // in square before a possible room location // after: facing east, bottom row, // in square after a possible room location // (i.e. in square before next possible // room location) public static void carpetRoom() { // 1. enter room Rooms.enterRoom(); // 2. if surrounded by walls, make it dark (carpet it) if ( Rooms.surroundedByWalls() ) { Robot.makeDark(); } // 3. exit room Rooms.exitRoom(); } public static void enterRoom() { Robot.move(); Robot.turnLeft(); Robot.move(); } public static void exitRoom() { Lesson.turnAround(); Robot.move(); Robot.turnLeft(); Robot.move(); } public static boolean surroundedByWalls() { // && means AND --> true if both conditions are true // || means OR --> true if either condition is true return leftHasWall() && rightHasWall(); } public static boolean leftHasWall() { Robot.turnLeft(); if ( ! Robot.frontIsClear() ) { Lesson.turnRight(); return true; // immediately exit with answer } else { Lesson.turnRight(); return false; } } public static boolean rightHasWall() { Lesson.turnRight(); if ( ! Robot.frontIsClear() ) { Robot.turnLeft(); return true; } else { Robot.turnLeft(); return false; } } } // end class