abstract class Grid[Element] extends AnyRef
The class Grid
represents rectangular grids that contain elements of a particular
kind. Each element in a grid is located at a unique pair of coordinates, represented
as a GridPos.
X coordinates run from 0 to width-1
, y coordinates from 0 to height-1
.
(0,0) corresponds to the upper left corner of the grid.
There are different kinds of grids: the type of element that a grid contains is
defined by a type parameter. For instance Grid[Square]
is a grid where each pair
of x and y coordinates contains a Square
object, and Grid[House]
is a grid
containing House
objects.
A Grid
is mutable: it is possible to replace an element at a particular GridPos
with another. (Depending on the element type, the elements may be individually
mutable, too.) The width and height of a grid never change, however.
Upon creation, a Grid
initializes itself by calling initialElements, which
produces an initial state for the grid.
This class has an alias in the top-level package o1, so it’s accessible to students
simply via import o1._
.
- Alphabetic
- By Inheritance
- Grid
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new Grid(width: Int, height: Int)
- width
the number of elements in each row of the grid
- height
the number of elements in each column of the grid
Abstract Value Members
- abstract def initialElements: Seq[Element]
Generates the elements that initially occupy the grid.
Generates the elements that initially occupy the grid. This method is automatically invoked by every
Grid
object upon creation. Subclasses should implement this method as appropriate for the particular sort of grid desired.Note that since this method produces the initial contents of the
Grid
, it gets called during initialization before theGrid
actually has any elements as content. Therefore, subclass implementations of this method must not depend on the state of the newGrid
(by callingneighbors
orelementAt
, for instance) or attempt to modify theGrid
(withupdate
, for instance).This method is meant for initialization purposes only. To access all the elements of an already-initialized
Grid
, call allElements instead.- returns
a collection of size
width
timesheight
that contains the initial grid elements. The first element in the collection will appear atGridPos
(0,0), the second at (1,0), and so on, filling in the first row before continuing on the second row at (1,0).
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##(): Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def allElements: Vector[Element]
Returns a collection of all the elements currently in the grid.
- def allPositions: Vector[GridPos]
Returns a collection of all the locations on the grid.
- def apply(location: GridPos): Element
Returns the element at the given pair of coordinates.
Returns the element at the given pair of coordinates. (This does the same as
elementAt
.)- location
a location on the grid (must be within range)
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def contains(location: GridPos): Boolean
Determines whether the grid contains the given pair of coordinates.
Determines whether the grid contains the given pair of coordinates. For instance,a grid with a width and height of 5 will contain (0, 0) and (4, 4) but not (-1, -1), (4, 5) or (5, 4).
- def elementAt(location: GridPos): Element
Returns the element at the given pair of coordinates.
Returns the element at the given pair of coordinates. (This does the same as
apply
.)- location
a location on the grid (must be within range)
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- val height: Int
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def neighbors(middleLoc: GridPos, includeDiagonals: Boolean): Vector[Element]
Returns a vector of all the neighboring elements of the indicated element.
Returns a vector of all the neighboring elements of the indicated element. Depending on the value of the second parameter, either only the four neighbors in cardinal compass directions (north, east, south, west) are considered, or the four diagonals as well.
Note that an element that is at the edge of the grid has fewer neighbors than one in the middle. For instance, the element at (0, 0) of a 5-by-5 grid has only three neighbors, diagonals included.
- middleLoc
the location between the neighbors
- includeDiagonals
true
if diagonal neighbors also count (resulting in up to eight neighbors),false
if only cardinal directions count (resulting in up to four)
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- val size: Int
the number of elements in this grid, in total.
the number of elements in this grid, in total. (Equals
width
timesheight
.) - def swap(location1: GridPos, location2: GridPos): Unit
Swaps the elements at two given locations on the grid, which must be within range.
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString: String
- Definition Classes
- AnyRef → Any
- def update(location: GridPos, newElement: Element): Unit
Modifies the grid by replacing the existing element at the given location with the new element.
Modifies the grid by replacing the existing element at the given location with the new element.
- location
a location on the grid (must be within range)
- newElement
the new element that replaces the old one at
location
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- val width: Int
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated @deprecated
- Deprecated
(Since version ) see corresponding Javadoc for more information.