(Mondays 2 PM)
(Fridays 2 PM)
Another view of this assignment: In this lab you create a maze. In the next lab, you will help a monster look for cheese in the maze.
OK, I confess: the true nature of this lab is to teach you the design and use of an ADT (abstract data type) for a set. But the maze and network applications will make this more fun. Moreover, the maze serves as a visualization that can help you debug and test your set ADT.
The design will be discussed Monday in class. In Lab, your graded design will be returned. No other handouts or code will be issued. So, think carefully about your design!
Remember to type the following lines at the top of any files that use the terminal or canvas classes.
import cs101.terminal.*; import cs101.canvas.*;
A new building has been constructed on our campus. This building is unusual, in that its inhabitants will consist of robots without eyes. A robot navigates the building by always keeping one of its "hands" in contact with a wall surface.
This building contains a rectangular grid of square rooms. Each room has four walls, and each wall is equipped with a door for exit from the room. When the doors of two adjecent rooms are opened, a hallway is formed, so people can travel freely between the two rooms.
However, this building has a strange property: if ever a set of rooms is interconnected to form a loop, the world as we know it would cease to exist. (How's that for a strict building code?)
The rooms were placed in the buildings with their doors closed. Then, a painter was airdropped into each room to paint the room.
Currently, there is a painter inside each room. Strangely, each painter is busily busily painting his or her room a unique color. Unexpectedly, the roof arrives and is placed on the building. Consequently, with all doors closed, each painter is trapped inside his or her room, as shown below
Each solid square is a room. You can see the outline of the potential
hallways, but in the picture, all doors are closed.
The picture shows there are 15 sets, with one room in each set.
Suddenly, each painter is overcome by an urge to escape and paint the entire set of rooms his or her unique color. Frustratingly, each painter is trapped and does not know which doors to open, given the strange properties of this building. Randomly, painters in adjacent rooms call to each other to see if the doors between them should be opened. How do they decide if it is safe?
If the two rooms are currently in different sets (painted different colors), then the doors between them can and should be opened. Otherwise, the doors must not be opened because a loop would be formed.When the doors are opened, the two painters are necessarily in rooms of different colors.
|Above, you see two rooms with their doors closed. The potential hallways are shown in outline form.||Above, the left room has opened its East door and the right room has opened its West door. As a result, a hallway is formed and people can move between the two rooms.|
To save time, the painters agree to paint the smaller set of rooms the color of the larger set. If both sets contain the same number of rooms, the painters arm-wrestle or throw a fair coin to see who wins.
The process of opening doors and painting rooms continues.
| This picture shows an intermediate
point in the computation.
Here, there are three sets of rooms: light-green, black, and dark-green.
| One step later, two rooms in the bottom row
As a result, two of the sets have been merged. Because the dark-green rooms outnumber the black ones, the black rooms are painted dark-green as they are merged into the same set.
The process of opening doors continues until all the rooms are interconnected and painted the same color. At this point, all rooms are in the same set.
The computation is finished, and the potential hallways are erased.
Listclass is tested.
RoomSetclass is tested. For this test, two rows of rooms are created. The transcript will show the order in which rooms are unioned. When your code works properly, the rooms will become the same color when their sets are unioned.
Mazeclass is tested. A 3x5 maze of rooms is generated. Ten times, the doors are closed and randomly opened according to the instructions of this lab. When your code works properly, each iteration should show you a valid maze.
Room(int row, int col, int pixs, CS101Canvas canvas)
col. The location of the room on the canvas is determined by its row and column. The
pixsparameter specifies the size of the room's rectangle.
void nowMemberOf(RoomSet set)
This class is provided for you, as described in class. It is
a generic list, suitable for subclassing. Its public methods can
be called on any subclass of
List. Its protected fields
and methods are available only within the subclasses.
List.java. Specifically, fill in the methods
Room getRoom()returns the room contained in this list element. Be sure to cast appropriately!
RoomList getRestRooms()returns a reference to the rest of the rooms in the list.
Room findRoom(int n)finds the nth room. For this, you must use
findmethod, callable from the subclass as
VertHall, classes that respectively represent horizontal and vertical hallways betwee rooms.
Design (and turn in) the superclass
Hall that can
be extended to create
As a hint, consider the following questions:
RoomSet. You will have to learn how Java encodes a color, which is similar to how we enocde a Card in Lab 6.
(The files contain some more specific instructions.)