|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--edu.kzoo.grid.Grid
Grid Container Package:
A Grid
is a two-dimensional, rectangular container
data structure. It can contain any kind of object that can be
modeled using an extension of the GridObject
class.
For example, a grid could be used to model a board for a
tic-tac-toe or chess game, an environment of fish for a marine
biology simulation, etc.
The Grid
class is based on the College Board's
Environment
interface and SquareEnvironment
class, as allowed by the GNU General Public License.
Direction
,
Location
,
GridObject
Nested Class Summary | |
static class |
Grid.BoundedGridValidityChecker
A BoundedGridValidityChecker implements a strategy for
determining the validity of a location in a bounded grid. |
protected static interface |
Grid.InternalRepresentation
The InternalRepresentation interface specifies
the methods that any internal representation of the
Grid class must implement. |
static class |
Grid.UnboundedGridValidityChecker
An UnboundedGridValidityChecker implements a strategy
for determining the validity of a location in an unbounded grid. |
static interface |
Grid.ValidityChecker
A ValidityChecker specifies a strategy for determining
the validity of a location in a grid. |
Field Summary | |
protected boolean |
includeDiagonals
Instance variable indicating whether the set of neighbors around each cell in the grid should include the 4 cells on the diagonals as well as the 4 cells with shared sides. |
protected Grid.InternalRepresentation |
internalRep
Instance variable containing the internal representation of the grid, which could be implemented in a number of ways. |
static int |
UNBOUNDED
A constant representing an unbounded (or infinite) number of rows or columns in a grid. |
Constructor Summary | |
protected |
Grid(Grid.InternalRepresentation rep)
Constructs a Grid object with the specified internal
representation. |
protected |
Grid(Grid.InternalRepresentation rep,
boolean includeDiagonalNeighbors)
Constructs a Grid object with the specified internal
representation. |
Method Summary | |
void |
add(GridObject obj,
Location loc)
Adds a new object to this grid at the specified location. |
GridObject[] |
allObjects()
Returns all the objects in this grid. |
Direction |
getDirection(Location fromLoc,
Location toLoc)
Returns the direction from one location to another. |
Location |
getNeighbor(Location fromLoc,
Direction compassDir)
Returns the adjacent neighbor (whether valid or invalid) of a location in the specified direction. |
boolean |
isEmpty(Location loc)
Determines whether a specific location in this grid is empty. |
boolean |
isValid(Location loc)
Verifies whether a location is valid in this grid. |
java.util.ArrayList |
neighborsOf(Location ofLoc)
Returns the adjacent neighbors of a specified location. |
int |
numAdjacentNeighbors()
Returns the number of adjacent neighbors around each cell. |
abstract int |
numCols()
Returns number of columns in this grid. |
int |
numObjects()
Returns the number of objects in this grid. |
abstract int |
numRows()
Returns number of rows in this grid. |
GridObject |
objectAt(Location loc)
Returns the object at a specific location in this grid. |
Direction |
randomDirection()
Generates a random direction. |
void |
remove(GridObject obj)
Removes the specified object from this grid. |
void |
remove(Location loc)
Removes whatever object is at the specified location in this grid. |
void |
removeAll()
Removes all objects from this grid. |
java.lang.String |
toString()
Creates a single string representing all the objects in this environment (not necessarily in any particular order). |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int UNBOUNDED
protected final boolean includeDiagonals
protected final Grid.InternalRepresentation internalRep
Constructor Detail |
protected Grid(Grid.InternalRepresentation rep)
Grid
object with the specified internal
representation. Each cell in this grid will have at most four
adjacent neighbors, the cells to its north, south, east, and west.
If the grid is bounded, cells along the boundaries will have fewer
than four neighbors.
rep
- the internal representation for the grid and the
objects it containsprotected Grid(Grid.InternalRepresentation rep, boolean includeDiagonalNeighbors)
Grid
object with the specified internal
representation. Each cell in this grid will have at most four or
eight adjacent neighbors, depending on the value of the
includeDiagonalNeighbors
parameter. If
includeDiagonalNeighbors
is true
, each
cell will have at most eight adjacent neighbors, the cells to
its north, south, east, and west and the cells on the diagonals,
to the northeast, southeast, northwest, and southwest. If
includeDiagonalNeighbors
is false
,
each cell will have at most the four neighbors to its north,
south, east, and west. If the grid is bounded, cells along
the boundaries will have fewer than the maximum four or eight
neighbors.
rep
- the internal representation for the grid and the
objects it containsMethod Detail |
public abstract int numRows()
UNBOUNDED
if the grid
is unboundedpublic abstract int numCols()
UNBOUNDED
if the grid
is unboundedpublic boolean isValid(Location loc)
loc
- location to check
true
if loc
is valid;
false
otherwisepublic int numAdjacentNeighbors()
public Direction randomDirection()
randomDirection
reflects the direction from
a cell in the grid to one of its adjacent neighbors.
public Direction getDirection(Location fromLoc, Location toLoc)
fromLoc
and toLoc
are the same,
getDirection
arbitrarily returns Direction.NORTH
.
fromLoc
- starting location for searchtoLoc
- destination location
fromLoc
to toLoc
public Location getNeighbor(Location fromLoc, Direction compassDir)
fromLoc
- starting location for searchcompassDir
- direction in which to look for adjacent neighbor
fromLoc
in given direction
(whether valid or not)public java.util.ArrayList neighborsOf(Location ofLoc)
ofLoc
- location whose neighbors to get
ofLoc
public int numObjects()
public GridObject[] allObjects()
public boolean isEmpty(Location loc)
loc
- the location to test
true
if loc
is a
valid location in the context of this grid
and is empty; false
otherwisepublic GridObject objectAt(Location loc)
loc
- the location in which to look
loc
;
null
if loc
is not
in the grid or is emptypublic java.lang.String toString()
toString
in class java.lang.Object
public void add(GridObject obj, Location loc)
obj.grid()
and
obj.location()
are both null
;
loc
is a valid empty location in this grid.)
obj
- the new object to be added
java.lang.IllegalArgumentException
- if the precondition is not metpublic void remove(GridObject obj)
obj
is in this grid.)
obj
- the object to be removed
java.lang.IllegalArgumentException
- if the precondition is not metpublic void remove(Location loc)
loc
- the location from which to remove an objectpublic void removeAll()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |