Class Room

java.lang.Object
  |
  +--Room

public class Room
extends java.lang.Object

A Room is a "location" in the adventure game. A Room has a name, a description, and it connects to other rooms. In addition, a Room is a container for Things, and you can iterate over the Things in the Room.

Each Room you create should have a unique name. The Room class keeps track of all Rooms that you create. Given a Room theRoom, theRoom.getName() will return its name. Given a name roomName, Room.getRoom(roomName) will return the Room with that name.

You should NOT alter this class unless you want to give new behaviors to ALL Rooms.

Author:
David Matuszek

Field Summary
private  java.util.Vector contents
           
private  java.lang.String description
           
private  java.lang.String name
           
private  java.util.Hashtable paths
           
private static java.util.Hashtable rooms
           
 
Constructor Summary
Room(java.lang.String name)
          Constructor for objects of class Room.
 
Method Summary
 void add(Thing thing)
          Puts the given Thing into the room.
 void addPath(java.lang.String direction, Room destination)
          Creates a new path, in the given direction, to the given Room.
 boolean contains(Thing thing)
          Tests whether the given Thing is in this Room.
 java.util.Vector contents()
           
 java.util.Vector exits()
           
 java.lang.String getDescription()
          Returns the full description of this Room.
static Room getRoom(java.lang.String name)
           
 boolean isPath(java.lang.String direction)
          Tests whether there is a path from this Room in the indicated direction.
 Room nextRoom(java.lang.String direction)
          Returns the Room that will be reached if you go in the given direction from this Room.
 void remove(Thing thing)
          Removes the given Thing from this Room.
 void removePath(java.lang.String direction)
          Removes the path going from this Room in the given direction.
 void setDescription(java.lang.String description)
          Replaces the description of this Room with something more appropriate.
 java.util.Iterator thingIterator()
          Returns an iterator for the Things in the room.
 java.lang.String toString()
          Returns the name of this Room.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

name

private java.lang.String name

description

private java.lang.String description

paths

private java.util.Hashtable paths

contents

private java.util.Vector contents

rooms

private static java.util.Hashtable rooms
Constructor Detail

Room

public Room(java.lang.String name)
Constructor for objects of class Room. The name is required; the description will initially be taken from the name, but you should later supply a more detailed description.
Method Detail

toString

public java.lang.String toString()
Returns the name of this Room.
Overrides:
toString in class java.lang.Object
Returns:
the name of this Room.

getRoom

public static Room getRoom(java.lang.String name)
Returns:
the Room itself, given the name of the Room.

getDescription

public java.lang.String getDescription()
Returns the full description of this Room.
Returns:
a description of this Room.

setDescription

public void setDescription(java.lang.String description)
Replaces the description of this Room with something more appropriate.
Parameters:
description - One or more complete sentences describing this Room.

addPath

public void addPath(java.lang.String direction,
                    Room destination)
Creates a new path, in the given direction, to the given Room.

If there was a prior path in that direction, it is replaced by the new one.

Parameters:
direction - One of "north", "south", "east", "west", "up", or "down".
destination - To what room the direction leads.

removePath

public void removePath(java.lang.String direction)
Removes the path going from this Room in the given direction.

If there is no path in the given direction, this method does nothing.

Parameters:
direction - One of "north", "south", "east", "west", "up", or "down".

isPath

public boolean isPath(java.lang.String direction)
Tests whether there is a path from this Room in the indicated direction.
Parameters:
direction - One of "north", "south", "east", "west", "up", or "down".
Returns:
true if there is a path, false otherwise.

nextRoom

public Room nextRoom(java.lang.String direction)
Returns the Room that will be reached if you go in the given direction from this Room. If there is no path in the given direction, the method returns null.
Parameters:
direction - One of "north", "south", "east", "west", "up", or "down".
Returns:
the Room reached by going in that direction, or null.

thingIterator

public java.util.Iterator thingIterator()
Returns an iterator for the Things in the room. Every Thing is returned, whether visible or not; it is up to the caller if it wishes to ignore invisible Things.

add

public void add(Thing thing)
Puts the given Thing into the room.
Parameters:
thing - The thing to be placed in the room.

remove

public void remove(Thing thing)
Removes the given Thing from this Room.
Parameters:
thing - The thing to be removed the room.

contains

public boolean contains(Thing thing)
Tests whether the given Thing is in this Room.
Parameters:
thing - The Thing to look for in this Room.
Returns:
true if the Thing is in this room, false otherwise.

contents

public java.util.Vector contents()
Returns:
a Vector containing all the Things in this Room.

exits

public java.util.Vector exits()
Returns:
a Vector containing all the directions that one can go from this room.