Class Room

java.lang.Object
  |
  +--Room

public class Room
extends Object

Don't panic! You don't have to understand the implementation of this class; just see the documentation of method moveToDirection.

Class Room describes a room that may contain some objects and that may be connected to other rooms by doors. The implementation of the class is rather complicated.


Constructor Summary
Room(String name)
          Constructs a new room.
 
Method Summary
 void addObject(Object item)
          Inserts an object to a room.
 void connect(Room other, int direction)
          Connects two rooms.
 boolean containsObject(Object item)
          Does this room contain the given object?
 Object getObjectAt(int index)
          Retrieves the n'th object in this room.
 boolean moveToDirection(Robot robot, int direction)
          Tries to move a robot to the given direction.
 boolean possibleDirection(int direction)
          Is this room connected to another room in the given direction?
 boolean removeObject(Object item)
          Removes an object from a room.
 String toString()
          Redefine the predefined toString method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Room

public Room(String name)
Constructs a new room.
Parameters:
name - The name of the room. May be any string.
Method Detail

connect

public void connect(Room other,
                    int direction)
Connects two rooms.
Parameters:
other - the other room that is connected to this room.
direction - the direction of the connection.

possibleDirection

public boolean possibleDirection(int direction)
Is this room connected to another room in the given direction?
Parameters:
direction - the direction of the connection.
Returns:
true if there is door to another room in the given direction and false if there is no such door.

addObject

public void addObject(Object item)
Inserts an object to a room. The room may contain many objects, for instance, robots.
Parameters:
item - the object that we insert to this room.

removeObject

public boolean removeObject(Object item)
Removes an object from a room.
Parameters:
item - the object that we remove from this room.
Returns:
true if the room contained the given object and false if it did not.

containsObject

public boolean containsObject(Object item)
Does this room contain the given object?
Parameters:
item - the object that we check.
Returns:
true if the room contained the given object and false if it did not.

getObjectAt

public Object getObjectAt(int index)
Retrieves the n'th object in this room.
Parameters:
index - the index of the object.
Returns:
the object if the room contained enough objects and null if it did not.

moveToDirection

public boolean moveToDirection(Robot robot,
                               int direction)
Tries to move a robot to the given direction. Note that the robot must initially be in this room and there must be a connection to another room in the given direction.
Parameters:
robot - the robot to be moved
direction - the direction of the move
Returns:
true if the move was possible and false otherwise.

toString

public String toString()
Redefine the predefined toString method.
Overrides:
toString in class Object
Returns:
a string describing this room