info.gridworld.grid
Interface Grid<E>

All Known Implementing Classes:
AbstractGrid, BoundedGrid, UnboundedGrid

public interface Grid<E>

Grid provides an interface for a two-dimensional, grid-like environment containing arbitrary objects.
This interface is testable on the AP CS A and AB exams.


Method Summary
 E get(Location loc)
          Returns the object at a given location in this grid.
 ArrayList<Location> getEmptyAdjacentLocations(Location loc)
          Gets the valid empty locations adjacent to a given location in all eight compass directions (north, northeast, east, southeast, south, southwest, west, and northwest).
 ArrayList<E> getNeighbors(Location loc)
          Gets the neighboring occupants in all eight compass directions (north, northeast, east, southeast, south, southwest, west, and northwest).
 ArrayList<E> getOccupants()
          Gets the occupants in all locations 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> getOccupiedAdjacentLocations(Location loc)
          Gets the valid occupied locations adjacent to a given location in all eight compass directions (north, northeast, east, southeast, south, southwest, west, and northwest).
 ArrayList<Location> getOccupiedLocations()
          Gets the locations in this grid that contain objects.
 ArrayList<Location> getValidAdjacentLocations(Location loc)
          Gets the valid locations adjacent to a given location in all eight compass directions (north, northeast, east, southeast, south, southwest, west, and northwest).
 boolean isValid(Location loc)
          Checks whether a location is valid in this grid.
 boolean isEmpty(Location loc)
          Checks whether a location is valid and empty 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.
 

Method Detail

getNumRows

int getNumRows()
Returns the number of rows in this grid.

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

getNumCols

int getNumCols()
Returns the number of columns in this grid.

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

isValid

boolean isValid(Location loc)
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

isEmpty

boolean isEmpty(Location loc)
Checks whether a location is valid and empty in this grid.
Precondition: loc is not null

Parameters:
loc - the location to check
Returns:
true if loc is valid and empty in this grid, false otherwise

put

E put(Location loc,
      E obj)
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

E remove(Location loc)
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)

get

E get(Location loc)
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)

getOccupiedLocations

ArrayList<Location> getOccupiedLocations()
Gets the locations in this grid that contain objects.

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

getValidAdjacentLocations

ArrayList<Location> getValidAdjacentLocations(Location loc)
Gets the valid locations adjacent to a given location in all eight compass directions (north, northeast, east, southeast, south, southwest, west, and northwest).
Precondition: loc is valid in this grid

Parameters:
loc - a location in this grid
Returns:
an array list of the valid locations adjacent to loc in this grid

getEmptyAdjacentLocations

ArrayList<Location> getEmptyAdjacentLocations(Location loc)
Gets the valid empty locations adjacent to a given location in all eight compass directions (north, northeast, east, southeast, south, southwest, west, and northwest).
Precondition: loc is valid in this grid

Parameters:
loc - a location in this grid
Returns:
an array list of the valid empty locations adjacent to loc in this grid

getOccupiedAdjacentLocations

ArrayList<Location> getOccupiedAdjacentLocations(Location loc)
Gets the valid occupied locations adjacent to a given location in all eight compass directions (north, northeast, east, southeast, south, southwest, west, and northwest).
Precondition: loc is valid in this grid

Parameters:
loc - a location in this grid
Returns:
an array list of the valid occupied locations adjacent to loc in this grid

getOccupants

ArrayList<E> getOccupants()
Gets all occupants in this grid.

Parameters:
loc - a location in this grid
Returns:
returns an array list of the objects in all occupied locations

getNeighbors

ArrayList<E> getNeighbors(Location loc)
Gets the neighboring occupants in all eight compass directions (north, northeast, east, southeast, south, southwest, west, and northwest).
Precondition: loc is valid in this grid

Parameters:
loc - a location in this grid
Returns:
returns an array list of the objects in the occupied locations adjacent to loc in this grid