info.gridworld.grid
Class BoundedGrid<E>

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

public class BoundedGrid<E>
extends AbstractGrid<E>

A BoundedGrid is a rectangular grid with a finite number of rows and columns.
The implementation of this class is testable on the AP CS AB exam.


Constructor Summary
BoundedGrid(int rows, int cols)
          Constructs an empty bounded grid with the given dimensions.
 
Method Summary
 E get(Location loc)
          Returns the object at a given location in this grid.
 int getNumCols()
          Returns the number of columns in this grid.
 int getNumRows()
          Returns the number of rows 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.
 E put(Location loc, E obj)
          Puts an object at a given location in this grid.
 E remove(Location loc)
          Removes the object at a given location from this grid.
 
Methods inherited from class info.gridworld.grid.AbstractGrid
getEmptyAdjacentLocations, getNeighbors, getOccupiedAdjacentLocations, getValidAdjacentLocations, toString
 
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 bounded grid 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

getNumRows

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

Returns:
the number of rows, or -1 if this grid is unbounded

getNumCols

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

Returns:
the number of columns, or -1 if this grid is unbounded

isValid

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

Parameters:
loc - the location to check
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.

Returns:
an array list of all occupied locations in this grid

get

public E get(Location loc)
Description copied from interface: Grid
Returns the object at a given location in this grid.
Precondition: loc is valid in this grid

Parameters:
loc - a location in this grid
Returns:
the object at location loc (or null if the location is unoccupied)

put

public E put(Location loc,
             E obj)
Description copied from interface: Grid
Puts an object at a given location in this grid.
Precondition: (1) loc is valid in this grid (2) obj is not null

Parameters:
loc - the location at which to put the object
obj - the new object to be added
Returns:
the object previously at loc (or null if the location was previously unoccupied)

remove

public E remove(Location loc)
Description copied from interface: Grid
Removes the object at a given location from this grid.
Precondition: loc is valid in this grid

Parameters:
loc - the location of the object that is to be removed
Returns:
the object that was removed (or null if the location is unoccupied)