public class BoundedGrid extends Grid
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 ) | |
isValid, isEmpty, objectAt | O(1 ) | |
add | O(1 ) | |
remove | O(1 ) | |
removeAll | O(r * c ) | |
space | O(r * c ) |
r
is the number of rows and c
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.
Modifier and Type | Class and Description |
---|---|
protected static class |
BoundedGrid.Array2DGridRep
The
Array2DGridRep class represents an internal bounded
grid using a two-dimensional array. |
Grid.BoundedGridValidityChecker, Grid.InternalRepresentation, Grid.UnboundedGridValidityChecker, Grid.ValidityChecker
includeDiagonals, internalRep, UNBOUNDED
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
int |
numCols()
Returns number of columns in this grid.
|
int |
numRows()
Returns number of rows in this grid.
|
add, allObjects, getDirection, getNeighbor, isEmpty, isValid, neighborsOf, numAdjacentNeighbors, numObjects, objectAt, randomDirection, remove, remove, removeAll, toString
public BoundedGrid(int rows, int cols)
rows > 0
and cols > 0
.)rows
- number of rows in BoundedGridcols
- number of columns in BoundedGridjava.lang.IllegalArgumentException
- if the precondition is not metpublic BoundedGrid(boolean includeDiagonalNeighbors, int rows, int cols)
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
.)includeDiagonalNeighbors
- whether to include the four
diagonal locations as neighborsrows
- number of rows in BoundedGridcols
- number of columns in BoundedGridjava.lang.IllegalArgumentException
- if the precondition is not met