CS 101 (Fall 2000)
Lab 10: Design for Mazes

Lab Assigned Design Due
(In class)

1 PM
Implement
(In Lab)
Demo
(In Lab)
Lab Due
(In class)
Friday
1 PM
13 Nov None 15-16 Nov 28-29 Nov 1 Dec

This document contains the class-sponsored design, which you are free to use if you wish. You can take some or all of it---it's really up to you. The Files.

You may look at the javadoc of the sample solution.


Given to you:

Element
This is provided for you. It is an interface that specifies what a set element can do.
Interface
boolean elEquals(Element other)
returns true iff this element equals the supplied one.
Set belongsTo()
returns the Set that currently contains this element.
Set
This is provided for you. It is an interface that specifies what a set can do.
Interface
int sizeOf();
returns the number of elements in the set.
boolean isEmpty();
returns true iff the set is empty.
void insert(Element x);
inserts x into the set without duplication.
void delete(Element x);
deletes x if it is in the set.
boolean hasElement(Element x);
returns true iff x is an element of the set.
void union(Set other);
sets the set equal to the union of itself with the other set.
void intersection(Set other);
sets the set equal to the intersection of itself with the other set.
void difference(Set other);
sets the set equal to the difference of itself with the other set.
Element[] getContents();
returns an array containing the elements of the set.
SetA
This is the implementation of a set based on unordered lists, as discussed in class. It is provided to you in the Examples, so it is included in this Lab. It implements the Set interface.
ListOfObjects
As given in class.

Designed classes that you might complete

Maze
represents a maze, as discussed in class.
Constructor(s)
Maze(int rows, int cols, int pixsPerRoom)
constructs a resuable maze. That is, the maze can recreate itself (see create in the API below).
Accessor(s)
ListOfObjects getSolution()
solved the maze and returns the solution as a ListOfObjects of Room<s. The maze is solved by invoking explore(...) on the maze's starting room (upper, left-hand corner).
Room getRoom(int row, int col)
returns the room at the specified row and column. No error-checking is performed on the input parameters.
int getRows()
returns the number of rows in this maze.
int getCols()
returns the number of columns in this maze.
Mutator(s)
void create()
is responsible for setting up the maze.
  • Each Room is initialized by calling its init(). method.
  • The potential hallways (stored in a Halls object) are shuffled using the object's shuffle method.
  • The potential hallways are then considered, and openedUpHallway() is attempted on each one.
  • Each room is then told to drop its representation of unused hallways by calling dropEmptyHalls().
Room
represents a room.
Constructor(s)
Room(int row, int col, int pixs, CS101Canvas canvas) implements CODE>Element
This constructs a room at the specified location (row and column). The room is pixs wide and tall, and is to be drawn on the specified canvas. You will have to place the Room so there is space for the rectangles that represent the room's doorways (potential hallway adjoining the Room with one of its neighbors.

The doorways appear on the screen, but are not represented in the class. Instead, each room can be connected to its neighboring rooms using the methods shown below (connectNorth, etc).

Accessor(s)
RoomSet getSet()
returns the RoomSet to which this Room currently belongs.
Mutator(s)
init()
resets the room. It belongs to a unique set at this point, and it is not connected to any of its neighboring rooms.
nowMemberOf(RoomSet set)
tells the room that it is now part of the specified Set. The room should respond to this message by changing the color of its representative Rect and the Rectangles that represent its hallways.
void dropEmptyHalls()
causes those hallways not in use for this room to disappear. The easiest thing to do is to make them white.
void connectNorth(Room r)()
makes this room connect via its North "door" to the specified Room.
void connectSouth(Room r)()
makes this room connect via its South "door" to the specified Room.
void connectEast(Room r)()
makes this room connect via its East "door" to the specified Room.
void connectWest(Room r)()
makes this room connect via its West "door" to the specified Room.
Other(s)
boolean explore(Room prev, Room end, ListOfObjects ans)
initiates a search using this room. The previous room is specified as a parameter, and the search can continue (recursively) using any other room to which this room is connected. The method returns true if the treasure (end Room) was found further in the recursion.

On entry to this room, the room should be made a brighter color. On exit from this room, the room should be red if the treasure was found; otherwise, the room should be made a darker color (than it was originally).

boolean elEquals(Element other)
are we the same room as other? This can be based on our location.
Set belongsTo()
Necessary for the Set interface. Here we can just return the set to which the room currently belongs (i.e, return getSet()).
RoomSet extends SetA
Constructor(s)
RoomSet(Room singleton)
constructs this set with the supplied Room as its only element. Be sure you understand subclassing here! You don't need to make another Set, as RoomSet is already a Set.
Accessor(s)
Color getColor()
returns the Color of this RoomSet.
Mutator(s)
void insert(Element x)
overrides the superclass's definition, so that the supplied Element, which is really a Room, can be told it is now a member of this set.
Halls
represents the potential hallways.
Constructor(s)
Halls(Maze m)
constructs the halls for the specified maze. You need the Maze so you can get to the information about the size of the maze and its rooms. The constructor should make a represenation of all potential hallways.
Accessor(s)
int getNumHalls()
returns the number of potential halls.
Hall getHall(int i)
returns the ith Hall
Mutator(s)
shuffle()
randomly rearranges the order of the potential halls
Hall
This is an abstract class, which means you cannot instantiate it. But it has two usful subclasses, described next. Even though this class cannot be instantiated, you can still call methods on any Hall, as detailed below.
Constructor(s)
Hall(Room r1, Room r2)
can be called from a subclass constructor. The rooms are the ones connected by this Hall.
Accessor(s)
getRoom1()
returns the first room specified in the constructor call.
getRoom2()
returns the second room specified in the constructor call.
Other(s)
boolean openedUpHallway()
This method determines if it is safe to open up this Hall. If so, then it
  1. performs the union of the sets of the two rooms now adjoined
  2. calls openDoors, defined by the subclass for this Hall
  3. returns true
Otherwise, it returns false
HorizHall
represents a horizontal hallway. Such a hallway adjoins the East door of its first room with the West door of its second room. This class must define openDoors() accordingly.
VertHall
represents a vertical hallway. Such a hallway adjoins the South door of its first room with the North door of its second room. This class must define openDoors() accordingly.

By the way, to get a random color, you can use the canvas's WebSafeColor class. Pass a random integer between 0 and 215 to its constructor, and it will hold one of 216 random web-safe colors. Use the getColor() method on that object to return a java.awt.Color, which you can use to color a Rect.