edu.kzoo.grid
Class BoundedGrid
java.lang.Object
|
+--edu.kzoo.grid.Grid
|
+--edu.kzoo.grid.BoundedGrid
- public class BoundedGrid
- extends Grid
Grid Container Package:
A BoundedGrid
is a rectangular, bounded two-dimensional
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.
A BoundedGrid
is implemented as a two-dimensional array
corresponding to the dimensions of the grid. This gives it the
following time and space characteristics:
numObjects | | O(1 ) |
allObjects | | O(r * c ) |
isEmpty, objectAt | | O(1 ) |
add | | O(1 ) |
remove | | O(1 ) |
space | | O(r * c ) |
where r
is the number of rows and rc
is the
number of columns in the grid.
The BoundedGrid
class and its internal 2D array
implementation are based on the College Board's
BoundedEnv
class, as allowed by the GNU General
Public License.
- Version:
- 13 December 2003
- Author:
- Alyce Brady
Nested Class Summary |
protected static class |
BoundedGrid.Array2DGridRep
The Array2DGridRep class represents an internal bounded
grid using a two-dimensional array. |
Constructor Summary |
BoundedGrid(boolean includeDiagonalNeighbors,
int rows,
int cols)
Constructs an empty BoundedGrid object with the given dimensions. |
BoundedGrid(int rows,
int cols)
Constructs an empty BoundedGrid object with the given dimensions. |
Method Summary |
int |
numCols()
Returns number of columns in this grid. |
int |
numRows()
Returns number of rows in this grid. |
Methods inherited from class edu.kzoo.grid.Grid |
add, allObjects, getDirection, getNeighbor, isEmpty, isValid, neighborsOf, numAdjacentNeighbors, numObjects, objectAt, randomDirection, remove, remove, removeAll, toString |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
BoundedGrid
public BoundedGrid(int rows,
int cols)
- Constructs an empty BoundedGrid object with the given dimensions.
A cell's neighbors include only the cells to its north, south,
east, and west, not the cells on the diagonals.
(Precondition:
rows > 0
and cols > 0
.)
- Parameters:
rows
- number of rows in BoundedGridcols
- number of columns in BoundedGrid
- Throws:
java.lang.IllegalArgumentException
- if the precondition is not met
BoundedGrid
public BoundedGrid(boolean includeDiagonalNeighbors,
int rows,
int cols)
- Constructs an empty BoundedGrid object with the given dimensions.
Each cell in this grid will have at most four or eight
adjacent neighbors, depending on the value of the
includeDiagonalNeighbors
parameter. Cells along
the grid boundaries will have fewer than the maximum four or
eight neighbors. If includeDiagonalNeighbors
is
true
, a cell's adjacent neighbors include 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
,
a cell's adjacent neighbors include only the four cells to its
north, south, east, and west.
(Precondition: rows > 0
and cols > 0
.)
- Parameters:
includeDiagonalNeighbors
- whether to include the four
diagonal locations as neighborsrows
- number of rows in BoundedGridcols
- number of columns in BoundedGrid
- Throws:
java.lang.IllegalArgumentException
- if the precondition is not met
numRows
public int numRows()
- Returns number of rows in this grid.
- Specified by:
numRows
in class Grid
- Returns:
- the number of rows in this grid
numCols
public int numCols()
- Returns number of columns in this grid.
- Specified by:
numCols
in class Grid
- Returns:
- the number of columns in this grid