Java+Lab+3





Mrs. McLead's Mclead's Solution: 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 //after: facing east, bottom row,// in square after the possible room, // room carpeted (if was room) public static void carpetRoom { enterRoom; if (surroundedByWalls) { Robot.makeDark; } exitRoom; }

public static void enterRoom { Robot.move; Robot.turnLeft; Robot.move; }

public static void exitRoom { Lesson.backUp; Lesson.turnRight; Robot.move; }

public static boolean frontIsBlocked { return !Robot.frontIsClear; }

public static boolean leftIsBlocked { Robot.turnLeft; if (Rooms.frontIsBlocked) { Lesson.turnRight; return true; } else { Lesson.turnRight; return false; } }

public static boolean rightIsBlocked { Lesson.turnRight; if (Rooms.frontIsBlocked) { Robot.turnLeft; return true; } else { Robot.turnLeft; return false; } }

public static boolean surroundedByWalls { return leftIsBlocked && rightIsBlocked; } }

Lab 3: Ifs
Each exercise will describe a general scenario, and will show just one example world illustrating that general scenario. **Your program must correctly handle //any// map that matches the description in the exercise.**You are strongly advised to test your program on more than one map. As always, your solutions should not contain excessively long methods or extensive duplicate code. In each exercise, you will be given a task for the robot to complete. It does not matter where the robot ends up at the completion of the task, as long as the appropriate squares have changed color. To receive full credit, your program must complete the task **and terminate without any errors.** Open Lab3.java in DrJava. All the code you write for this assignment should be written in this file.
 * Hint: For each method, the final position and direction of the robot should not depend on what conditions were true or false.**

Exercise 1: lightCandles
In this exercise, the robot initially finds itself on a map like the one shown in the example below. Complete the method, which should darken the square on top of each "candle" (those columns consisting of 1 or 2 walls). After calling the method on the example shown above, the map should appear as follows: You should assume that the robot will always start in the same position, facing the same direction, in the same size map, with the same number of candles appearing in the same locations. The only variation will be in the heights of the candles (1 or 2 squares). Be sure to test your code on both "candles1.txt" and "candles2.txt". (For your convenience you may use the and  methods to test your code on these maps.)

Exercise 2: completeRoom
In this exercise, the robot initially finds itself in a square-shaped room, like the one shown in the example below. Complete the method, which should complete the border of the room by darkening each cell where a wall appears to be missing. Of course, if the cell is already dark, then that cell should remain dark. After calling the method on the example shown above, the room should appear as follows: You should assume that the robot will always start in the same position, facing the same direction, in the same size room. Be sure to test your code on both "room1.txt" and "room2.txt".
 * Hint:**If you're stuck, try solving the problem for the case where there are no cells that are already dark. Then modify your code to handle the case where some cells may already be dark.

Exercise 3: swapAll
In this exercise, the robot initially finds itself between two columns of light and dark squares, like the ones shown in the example below (lefthand picture). Complete the method, which should swap the pattern of light and dark squares that make up the column to the west, with the pattern of light and dark squares that make up the column to the east. After calling the method on the example shown in the lefthand picture above, the room should appear as shown in the righthand picture. You should assume that the robot will always start in the same position, that the columns will always be 10 squares tall, and that there are no walls in the robot's path. Be sure to test your code on both "swap1.txt" and "swap2.txt".
 * Before : || After : ||
 * [[image:http://www.cs.cmu.edu/~tcortina/activate/java/lab3/swapbefore.gif]] || [[image:http://www.cs.cmu.edu/~tcortina/activate/java/lab3/swapafter.gif]] ||

Challenge: carpetTallRooms
Write a method called, which should carpet any complete room that is 1, 2, or 3 squares tall. For example, the following world should be carpeted like this You should assume that the robot is positioned at the west end of a row of 8 rooms. A complete room must have completed walls on 3 sides: north, west, and east. In the example above, there are only 3 complete rooms: the 1st, 2nd, and 3rd. The 4th, 5th, 6th, 7th, and 8th are not complete, because each is missing at least part of one side. Your method should instruct the robot to carpet each complete room (by darkening all squares inside the room).