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.
Hint: For each method, the final position and direction of the robot
should not depend on what conditions were true or false.
Download lab3.zip. Open Lab3.java in DrJava.
All the code you write for this assignment should be written in this
file.
Exercise 1: lightCandles
In this exercise, the robot initially finds itself on a map like the
one shown in the example below.
Complete the lightCandles method, which should darken the
square on top of each "candle" (those columns consisting of 1 or 2
walls). After calling the lightCandles 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 testLightCandles1 and
testLightCandles2 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 completeRoom 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 completeRoom 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.
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.
Be sure to test your code on both "room1.txt" and "room2.txt".
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).
Before swapAll: |
After swapAll: |
|
|
Complete the swapAll 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 swapAll 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".
Challenge: carpetTallRooms
Write a method called carpetTallRooms,
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).