com.collegeboard.gridworld.grid
Class BoundedGrid<E>

java.lang.Object
  extended by com.collegeboard.gridworld.grid.BoundedGrid<E>
All Implemented Interfaces:
Grid<E>

public class BoundedGrid<E>
extends Object
implements Grid<E>


Constructor Summary
BoundedGrid(int rows, int cols)
          Constructs an empty BoundedGrid object with the given dimensions.
 
Method Summary
 E get(Location loc)
          Returns the object at a specific location in this grid
 ArrayList<Location> getOccupiedLocations()
          Gets the locations in this grid that contain objects.
 boolean isValid(Location loc)
          Checks whether a location is valid in this grid.
 int numCols()
          Returns number of columns in this grid.
 int numRows()
          Returns number of rows in this grid.
 E put(Location loc, E obj)
          Adds a new object to this grid.
 E remove(Location loc)
          Removes an object from this grid.
 String toString()
          Creates a string representing all the objects in this grid (not necessarily in any particular order).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BoundedGrid

public BoundedGrid(int rows,
                   int cols)
Constructs an empty BoundedGrid object with the given dimensions. (Precondition: rows > 0 and cols > 0.)

Parameters:
rows - number of rows in BoundedGrid
cols - number of columns in BoundedGrid
Method Detail

numRows

public int numRows()
Description copied from interface: Grid
Returns number of rows in this grid.

Specified by:
numRows in interface Grid<E>
Returns:
the number of rows, or -1 if the grid is unbounded

numCols

public int numCols()
Description copied from interface: Grid
Returns number of columns in this grid.

Specified by:
numCols in interface Grid<E>
Returns:
the number of columns, or -1 if the grid is unbounded

isValid

public boolean isValid(Location loc)
Description copied from interface: Grid
Checks whether a location is valid in this grid.

Specified by:
isValid in interface Grid<E>
Parameters:
loc - location to check (must not be null)
Returns:
true if loc is valid in this grid; false otherwise

getOccupiedLocations

public ArrayList<Location> getOccupiedLocations()
Description copied from interface: Grid
Gets the locations in this grid that contain objects.

Specified by:
getOccupiedLocations in interface Grid<E>
Returns:
an array of all occupied locations in this grid

get

public E get(Location loc)
Description copied from interface: Grid
Returns the object at a specific location in this grid

Specified by:
get in interface Grid<E>
Parameters:
loc - the location in which to look (must be valid)
Returns:
the object at location loc; null if loc is empty

put

public E put(Location loc,
             E obj)
Description copied from interface: Grid
Adds a new object to this grid.

Specified by:
put in interface Grid<E>
Parameters:
loc - the location at which to put the object (must be valid)
obj - the new object to be added (must not be null)
Returns:
the object that was removed, or null if the location was previously unoccupied.

remove

public E remove(Location loc)
Description copied from interface: Grid
Removes an object from this grid.

Specified by:
remove in interface Grid<E>
Parameters:
loc - the location of the object that is to be removed (must be valid)
Returns:
the object that was removed, or null if no object was in the given location

toString

public String toString()
Creates a string representing all the objects in this grid (not necessarily in any particular order).

Overrides:
toString in class Object
Returns:
a string indicating all the objects in this environment