![]() |
![]() |
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs
The last essential step to gRUe will be to add connections between rooms. Every room will be connected to zero or more other Rooms; connections can be one-way. (That is, you might be able to get from room A to room B, but not back.)
Before proceeding, you'll need to have at least 3-4 Rooms already created in your test-code for Room.
Important: your toString method, when processing the list of adjacent rooms, should not call the toString of any of its adjacent rooms. (This means, don't call the list's default toString.)
Why not? Because if roomA has roomB as a neighbor and in turn roomB has roomA as a neighbor, then printing would be infinite: roomA.toString() would attempt to make a string using (in part) roomB.toString(), but roomB.toString() would attempt to make a string using (in part) roomA.toString(), but roomA.toString() would attempt… you get the picture. Java will report “Stack overflow”.
Instead: just print out (say) the names of the adjacent rooms, or call describablesToString giving it the list of adjacent rooms.
/** Test driver for Room.
*/
static void testRoom() {
//... make rooms roomA,roomB,roomC
System.out.println( "Adding a connection from " + roomA.getName() + " to " + roomB.getName() + ":" );
roomA.connect(roomB);
System.out.println( roomA.toString() );
System.out.println( roomB.toString() );
System.out.println( roomC.toString() );
}
|
System.out.println( "Adding a *two-way* connection between " + roomA.getName() + " and " + roomC.getName() + ":" );
roomA.biconnect(roomC);
System.out.println( roomA.toString() );
System.out.println( roomB.toString() );
System.out.println( roomC.toString() );
|
// Suppose that e1 is an Explorer whose current Room is roomA, // and that roomA has two neighboring rooms (roomB and roomC, // with indices 0 and 1 respectively). e1.moveTo(1); // Now, e1's current room should be roomC. |
1You can make this a static method, if you prefer. The rationale against a regular method is that the two rooms are equally important, so it seems odd to have roomA be considered the “primary” instance, and roomC is a the subsidary input to the action being performed on roomA. For comparison, in the one-way connect method, the asymmetry between the two Rooms is perfectly natural. back
2 If you later have multiple explorers occurring, then you'd want to take precautions not to call this room-setup method twice, but that's not an issue for this assignment. back
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs
| ©2006, Ian Barland, Radford University Last modified 2006.Dec.09 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |