Package application.model.simulation
Class Grid
java.lang.Object
application.model.simulation.Grid
- Direct Known Subclasses:
Simulator
public class Grid extends java.lang.Object implements java.lang.Iterable<Block>, java.util.Iterator<Block>
Represents and serves as pattern storage.
You may also iterate over it like in a loop.
Example Usage:
You may also iterate over it like in a loop.
Example Usage:
Grid grid = new Grid("bo$obo$o2bo$bobo$2bo!"); iterateCells(cell -> { System.out.println(cell); });
-
Field Summary
Fields Modifier and Type Field Description static int
BLOCK_SIZE
The size of the blocks used -
Constructor Summary
-
Method Summary
Modifier and Type Method Description void
addBlock(Coordinate coordinate)
Adds a block at coordinate (x, y) into the hashmapjava.util.List<Coordinate>
bfs(int distance, Coordinate[] neighbourhood)
Performs BFS (breath-first search) on the pattern and returns a list of coordinates represents the cells that are a certain distance from the living cells.void
clearCells()
Clears all cells in the gridvoid
clearCells(Coordinate start, Coordinate end)
Clears all cells between the coordinates specifiedint
convertCell(int state)
Convert the cell state based on the background Used to for B0 / strobing rulesGrid
deepCopy()
Deep copies the gridboolean
equals(java.lang.Object o)
Check if the inputted grid is equal to this grid (independent of translation)void
fromApgcode(java.lang.String apgcode, Coordinate startCoordinate)
Inserts the apgcode into the gridvoid
fromRLE(java.lang.String rle, Coordinate startCoordinate)
Inserts the RLE into the gridint
getBackground()
Gets the background of the gridBlock
getBlock(Coordinate coordinate)
Gets the block that the cell is inCoordinate
getBlockCoordinate(Coordinate coordinate)
Gets the coordinate of the block of a cell at a given coordinate (x, y)java.util.Set<Coordinate>
getBlockCoordinates()
Gets the coordinates of all the blocks in the gridjava.util.Collection<Block>
getBlocks()
Gets the blocks in the gridorg.javatuples.Pair<Coordinate,Coordinate>
getBounds()
Gets the bounds of the gridint
getCell(int x, int y)
Gets the state of the cell at (x, y)int
getCell(Coordinate coordinate)
Gets the state of the cell at the specified coordinateGrid
getCells(Coordinate start, Coordinate end)
Get all cells between the start and end coordinatesint
getPopulation()
Gets the population of the grid (all cells above state 0)int
hashCode()
Gets the hash of the grid.int
hashCode(Coordinate startCoordinate, Coordinate endCoordinate)
Gets the hash of the grid.int
hashCode(java.util.List<Coordinate> coordinates, Coordinate startCoordinate)
Gets the hash of the grid.boolean
hasNext()
void
insertCells(Grid pattern, Coordinate coordinate)
Inserts the provided pattern at the specified coordinatevoid
iterateCells(java.util.function.Consumer<Coordinate> methodToCall)
Calls a method on every live cell (not state 0) in the gridvoid
iterateCellsInBlock(java.util.function.Consumer<Coordinate> methodToCall, Block block)
Calls a method on every live cell (not state 0) in the provided blockjava.util.Iterator<Block>
iterator()
Block
next()
void
reflectCellsX(Coordinate startCoordinate, Coordinate endCoordinate)
Reflects the cells in the grid between the start and end coordinates horizontallyvoid
reflectCellsY(Coordinate startCoordinate, Coordinate endCoordinate)
Reflects the cells in the grid between the start and end coordinates verticallyvoid
removeBlock(Coordinate coordinate)
Removes the block at coordinate (x, y) from the hashmapvoid
rotateCCW(Coordinate startCoordinate, Coordinate endCoordinate)
Rotates the cells in the grid between the start and end coordinates counter-clockwisevoid
rotateCW(Coordinate startCoordinate, Coordinate endCoordinate)
Rotates the cells in the grid between the start and end coordinates clockwisevoid
setBackground(int background)
Set the background of the gridvoid
setCell(int x, int y, int state)
Sets the cell at position (x, y) to the specified statevoid
setCell(Coordinate coordinate, int state)
Sets the cell at position coordinate to the specified stateGrid
shallowCopy()
Shallow copies the gridint
size()
Gets the size of the gridboolean
slowEquals(Grid grid, int displacementX, int displacementY)
Checks if 2 grids are identical given a translation.Coordinate[]
toArray()
Converts the pattern into an array of coordinatesjava.lang.String
toRLE()
Converts the entire pattern into an RLEjava.lang.String
toRLE(Coordinate startCoordinate, Coordinate endCoordinate)
Converts pattern between the start and end coordinates to the run length encoded (RLE) format.void
updateBounds()
Updates the bounds of the grid.
-
Field Details
-
BLOCK_SIZE
public static final int BLOCK_SIZEThe size of the blocks used- See Also:
- Constant Field Values
-
-
Constructor Details
-
Method Details
-
setCell
Sets the cell at position coordinate to the specified state- Parameters:
coordinate
- The coordinate of the cellstate
- The state of the cell
-
setCell
public void setCell(int x, int y, int state)Sets the cell at position (x, y) to the specified state- Parameters:
x
- The x-coordinate of the celly
- The y-coordinate of the cellstate
- The state of the cell
-
addBlock
Adds a block at coordinate (x, y) into the hashmap- Parameters:
coordinate
- The coordinate of the block that is to be removed
-
removeBlock
Removes the block at coordinate (x, y) from the hashmap- Parameters:
coordinate
- The coordinate of the block that is to be removed
-
insertCells
Inserts the provided pattern at the specified coordinate- Parameters:
pattern
- The pattern to be insertedcoordinate
- The coordinate where the pattern is to be inserted
-
clearCells
Clears all cells between the coordinates specified- Parameters:
start
- The starting coordinateend
- The end coordinate
-
clearCells
public void clearCells()Clears all cells in the grid -
getCell
Gets the state of the cell at the specified coordinate- Parameters:
coordinate
- The coordinate of the cell- Returns:
- Returns the state of the cell
-
getCell
public int getCell(int x, int y)Gets the state of the cell at (x, y)- Parameters:
x
- The x-coordinate of the celly
- The y-coordinate of the cell- Returns:
- Returns the state of the cell
-
getCells
Get all cells between the start and end coordinates- Parameters:
start
- The start coordinateend
- The end coordinate- Returns:
- Returns a grid contains the pattern
-
updateBounds
public void updateBounds()Updates the bounds of the grid. -
getBounds
Gets the bounds of the grid- Returns:
- Returns a pair with the first entry being the start coordinate and the second entry being the end coordinate
-
toArray
Converts the pattern into an array of coordinates- Returns:
- Returns an array conains the coordinates
-
reflectCellsX
Reflects the cells in the grid between the start and end coordinates horizontally- Parameters:
startCoordinate
- The start coordinateendCoordinate
- The end coordinate
-
reflectCellsY
Reflects the cells in the grid between the start and end coordinates vertically- Parameters:
startCoordinate
- The start coordinateendCoordinate
- The end coordinate
-
rotateCW
Rotates the cells in the grid between the start and end coordinates clockwise- Parameters:
startCoordinate
- The start coordinateendCoordinate
- The end coordinate
-
rotateCCW
Rotates the cells in the grid between the start and end coordinates counter-clockwise- Parameters:
startCoordinate
- The start coordinateendCoordinate
- The end coordinate
-
toRLE
Converts pattern between the start and end coordinates to the run length encoded (RLE) format.- Parameters:
startCoordinate
- The start coordinateendCoordinate
- The end coordinate- Returns:
- Returns the RLE body (no header)
-
toRLE
public java.lang.String toRLE()Converts the entire pattern into an RLE- Returns:
- Returns the RLE body (no headers)
-
fromRLE
Inserts the RLE into the grid- Parameters:
rle
- The RLE body (no headers, no \n)startCoordinate
- The coordinate to insert the new cells
-
fromApgcode
Inserts the apgcode into the grid- Parameters:
apgcode
- The apgcode to insert into the gridstartCoordinate
- The coordinate to insert the new cells
-
bfs
Performs BFS (breath-first search) on the pattern and returns a list of coordinates represents the cells that are a certain distance from the living cells.- Parameters:
distance
- The "distance" from the living cellneighbourhood
- The neighbourhood to use in the BFS- Returns:
- Returns the list of cells that are a certain distance from the living cells
-
deepCopy
Deep copies the grid- Returns:
- A deep copy of the grid
-
shallowCopy
Shallow copies the grid- Returns:
- A shallow copy of the grid
-
size
public int size()Gets the size of the grid- Returns:
- Returns the size of the grid
-
slowEquals
Checks if 2 grids are identical given a translation. Used to check for hash collisions.- Parameters:
grid
- The grid to check.displacementX
- The distance the first grid is translated in the x-direction compared to the second griddisplacementY
- The distance the first grid is translated in the y-direction compared to the second grid- Returns:
- Returns true if the grids are equal, false if they aren't
-
equals
public boolean equals(java.lang.Object o)Check if the inputted grid is equal to this grid (independent of translation)- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- The other grid- Returns:
- Returns true if the grids are equal, false if they aren't
-
hashCode
public int hashCode()Gets the hash of the grid.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- Returns the grid's hash (uses Golly's hash algorithm).
-
hashCode
Gets the hash of the grid.- Parameters:
startCoordinate
- The start coordinate of the region where the hash is calculatedendCoordinate
- The end coordinate of the region where the hash is calculated- Returns:
- Returns the grid's hash (uses Golly's hash algorithm).
-
hashCode
Gets the hash of the grid.- Parameters:
coordinates
- The coordinates to consider in the hash algorithmstartCoordinate
- The start coordinate of the list of coordinates provided- Returns:
- Returns the grid's hash (uses Golly's hash algorithm).
-
setBackground
public void setBackground(int background)Set the background of the grid- Parameters:
background
- The background
-
getBackground
public int getBackground()Gets the background of the grid- Returns:
- Returns the background of the grid
-
convertCell
public int convertCell(int state)Convert the cell state based on the background Used to for B0 / strobing rules- Parameters:
state
- The current state of the cell- Returns:
- Returns the new cell state
-
iterateCells
Calls a method on every live cell (not state 0) in the grid- Parameters:
methodToCall
- The method to call with the coordinate of the cells as a parameter
-
iterateCellsInBlock
Calls a method on every live cell (not state 0) in the provided block- Parameters:
methodToCall
- The method to call with the coordinate of the cells as a parameterblock
- The block to iterate through
-
getBlockCoordinate
Gets the coordinate of the block of a cell at a given coordinate (x, y)- Parameters:
coordinate
- The coordinate of the cell- Returns:
- Returns the block coordinate
-
getBlock
Gets the block that the cell is in- Parameters:
coordinate
- The coordinate of the cell- Returns:
- Returns the block of the cell
-
getPopulation
public int getPopulation()Gets the population of the grid (all cells above state 0)- Returns:
- Returns the population of the grid
-
getBlocks
Gets the blocks in the grid- Returns:
- Returns the blocks in the grid
-
getBlockCoordinates
Gets the coordinates of all the blocks in the grid- Returns:
- Returns the coordinates of all the blocks in the grid.
-
hasNext
public boolean hasNext()- Specified by:
hasNext
in interfacejava.util.Iterator<Block>
-
next
- Specified by:
next
in interfacejava.util.Iterator<Block>
-
iterator
- Specified by:
iterator
in interfacejava.lang.Iterable<Block>
-