Class Room

java.lang.Object
  extended by Room

public class Room
extends Object

Provides a virtual room for a Vroomba to clean.

When a Room is created, it must contain a Vroomba. The Room's clean() method will then instruct that Vroomba to clean the room, monitoring its progress.

The details of a room's geography are specified by a map of characters. Room maps are normally read from file, but can be specified directly as a 2D array of appropriate characters. Acceptable room features (walls, empty floor, etc) are listed by this class as char constants.

Every map must include at least one square of floor space. If the map does not specify the location of the contained Vroomba, the Vroomba will be placed randomly on the floor. A map does not need to specify its borders with walls; any location off the map is assumed to be a WALL.

When instructing a Vroomba to clean, a Room monitors each move of the Vroomba. It will stop the cleaning if the Vroomba should move over a DROP, crash 3 consecutive times into the same obstruction, fail to move at all, or exceeds the max number of moves allowed. Max moves are determined from the amount of floor to clean and the MAX_MOVES_MODIFIER.

Version:
08 Nov 2008
Author:
Zach Tomaszewski

Nested Class Summary
 class Room.GUI
          Provides a graphical view of a Room and its cleaning process.
static class Room.Result
          An enumeration of the various possible outcomes of a Vroomba cleaning a room.
 
Field Summary
static char DIRT
           
static char DROP
           
static char FLOOR
           
static int MAX_MOVES_MODIFIER
          A Vroomba must finish cleaning any room within (MAX_MOVES_MODIFIER * this.floorSpaces) moves.
static char VROOMBA
           
static char WALL
           
 
Constructor Summary
Room(char[][] map, Vroomba v)
          Construct a new Room with the given map, to be cleaned by the given Vroomba.
Room(String mapFilename, Vroomba v)
          Constructs a room by loading the given file and using the given Vroomba.
 
Method Summary
 String clean()
          Has this room cleaned by its occupying Vroomba.
protected  int count(char feature)
          Counts the number of times the given char is found in the current room map.
 int getCollision()
          Returns the number of collisions this Room's Vroomba has had so far.
protected  char getFeature(Direction d)
          Returns the room feature currently in the given direction from this Room's Vroomba.
protected  char getFeature(int row, int col)
          Returns the room feature at the given location in this room.
 int getMaxMoves()
          Returns the maximum number of moves a Vroomba is allowed for this Room.
 int getMoves()
          Returns the number of moves this Room's Vroomba has currently made.
 Room.Result getResult()
          Returns the result of cleaning this room.
 String getStatus()
          Returns the current status of this Room's Vroomba's cleaning performance as a single line of text.
 String getStatus(boolean asMove)
          Returns the current status, formatted as per getStatus().
 boolean isClean()
          Returns whether this room currently contains any DIRT.
protected  boolean moveVroomba()
          Instructs this room's Vroomba to take a turn of cleaning.
 String toString()
          Returns a snapshot of this Room's current map as a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FLOOR

public static final char FLOOR
See Also:
Constant Field Values

DIRT

public static final char DIRT
See Also:
Constant Field Values

WALL

public static final char WALL
See Also:
Constant Field Values

DROP

public static final char DROP
See Also:
Constant Field Values

VROOMBA

public static final char VROOMBA
See Also:
Constant Field Values

MAX_MOVES_MODIFIER

public static final int MAX_MOVES_MODIFIER
A Vroomba must finish cleaning any room within (MAX_MOVES_MODIFIER * this.floorSpaces) moves. Current default: 3.

See Also:
Constant Field Values
Constructor Detail

Room

public Room(char[][] map,
            Vroomba v)
     throws InvalidMapException
Construct a new Room with the given map, to be cleaned by the given Vroomba.

Verifies that the map is valid: It must not contain any unrecoginzed chars. Every row of the map must be the same length. A map may not contain more than a single 'V' character, which is where the given Vroomba should be initially placed. If no such 'V' is found, the Vroomba will be placed randomly on the floor somewhere (on either a FLOOR or DIRTy square).

Thus, every map must contain at least one VROOMBA, FLOOR, or DIRT character.

Throws:
InvalidMapException - if the given map is invalid

Room

public Room(String mapFilename,
            Vroomba v)
     throws FileNotFoundException,
            InvalidMapException
Constructs a room by loading the given file and using the given Vroomba.

The contents of the file will be converted to a 2D array of characters. File must exist, be readable, and contain a valid map.

Throws:
FileNotFoundException - if the given file does not exist
InvalidMapException - if file does not contain a valid map
See Also:
Room(char[][], Vroomba)
Method Detail

clean

public String clean()
Has this room cleaned by its occupying Vroomba. Will continue until the Vroomba stops, exceeds its allowed number of moves, or has a mishap. See moveVroomba() for more.

Once called, additional calls to clean() do nothing and will return null.

Returns a transcript of the cleaning session as formed by a call of getStatus() every turn. The transcript includes only moves, however, and does not include the finished result.


getCollision

public int getCollision()
Returns the number of collisions this Room's Vroomba has had so far.


getMaxMoves

public int getMaxMoves()
Returns the maximum number of moves a Vroomba is allowed for this Room.


getMoves

public int getMoves()
Returns the number of moves this Room's Vroomba has currently made.


getResult

public Room.Result getResult()
Returns the result of cleaning this room. If cleaning has not started or is still in progress, will return null.


getStatus

public String getStatus()
Returns the current status of this Room's Vroomba's cleaning performance as a single line of text.

If still cleaning, the line returned will begin with "CLEANING:" and the direction of the last move. If done, the line will instead begin with "RESULT:" and the completion status. In either case, line also includes the number of moves made over the max allowed moves, as well as the number of collisions.


getStatus

public String getStatus(boolean asMove)
Returns the current status, formatted as per getStatus(). However, if asMove is true, will print the status as if the Vroomba were still cleaning. This allows for printing of the last move a Vroomba made before finishing the cleaning.


isClean

public boolean isClean()
Returns whether this room currently contains any DIRT.


toString

public String toString()
Returns a snapshot of this Room's current map as a string.

Overrides:
toString in class Object

count

protected int count(char feature)
Counts the number of times the given char is found in the current room map.


getFeature

protected char getFeature(int row,
                          int col)
Returns the room feature at the given location in this room. If this does not correspond to a space on the map, it is treated as a Room.WALL.


getFeature

protected char getFeature(Direction d)
Returns the room feature currently in the given direction from this Room's Vroomba. If this does not correspond to a space on the map, it is treated as a Room.WALL.


moveVroomba

protected boolean moveVroomba()
Instructs this room's Vroomba to take a turn of cleaning.

Passes this room's Vroomba its current surroundings by calling its Vroomba.move(char[]) method and then attempts to move it in its requested direction.

After each move, determines whether the Vroomba is done cleaning. It is done if it chooses to move to Direction.HERE, if it has exceeded its max number of moves, if it goes over a drop, or if it crashes into the same obstruction from the same direction for 3 consecutive moves.

Returns whether this Vroomba needs to move again because it is not done cleaning.

Returns:
true if needs to continue; false if done cleaning.