Class Grid

java.lang.Object
application.model.simulation.Grid
All Implemented Interfaces:
java.lang.Iterable<Block>, java.util.Iterator<Block>
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:
 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

    Constructors 
    Constructor Description
    Grid()
    Constructs a grid with an empty pattern and a background of 0.
    Grid​(java.lang.String RLE)
    Constructs a grid the RLE pattern and a background of 0.
  • Method Summary

    Modifier and Type Method Description
    void addBlock​(Coordinate coordinate)
    Adds a block at coordinate (x, y) into the hashmap
    java.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 grid
    void clearCells​(Coordinate start, Coordinate end)
    Clears all cells between the coordinates specified
    int convertCell​(int state)
    Convert the cell state based on the background Used to for B0 / strobing rules
    Grid deepCopy()
    Deep copies the grid
    boolean 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 grid
    void fromRLE​(java.lang.String rle, Coordinate startCoordinate)
    Inserts the RLE into the grid
    int getBackground()
    Gets the background of the grid
    Block getBlock​(Coordinate coordinate)
    Gets the block that the cell is in
    Coordinate 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 grid
    java.util.Collection<Block> getBlocks()
    Gets the blocks in the grid
    org.javatuples.Pair<Coordinate,​Coordinate> getBounds()
    Gets the bounds of the grid
    int 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 coordinate
    Grid getCells​(Coordinate start, Coordinate end)
    Get all cells between the start and end coordinates
    int 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 coordinate
    void iterateCells​(java.util.function.Consumer<Coordinate> methodToCall)
    Calls a method on every live cell (not state 0) in the grid
    void iterateCellsInBlock​(java.util.function.Consumer<Coordinate> methodToCall, Block block)
    Calls a method on every live cell (not state 0) in the provided block
    java.util.Iterator<Block> iterator()  
    Block next()  
    void reflectCellsX​(Coordinate startCoordinate, Coordinate endCoordinate)
    Reflects the cells in the grid between the start and end coordinates horizontally
    void reflectCellsY​(Coordinate startCoordinate, Coordinate endCoordinate)
    Reflects the cells in the grid between the start and end coordinates vertically
    void removeBlock​(Coordinate coordinate)
    Removes the block at coordinate (x, y) from the hashmap
    void rotateCCW​(Coordinate startCoordinate, Coordinate endCoordinate)
    Rotates the cells in the grid between the start and end coordinates counter-clockwise
    void rotateCW​(Coordinate startCoordinate, Coordinate endCoordinate)
    Rotates the cells in the grid between the start and end coordinates clockwise
    void setBackground​(int background)
    Set the background of the grid
    void setCell​(int x, int y, int state)
    Sets the cell at position (x, y) to the specified state
    void setCell​(Coordinate coordinate, int state)
    Sets the cell at position coordinate to the specified state
    Grid shallowCopy()
    Shallow copies the grid
    int size()
    Gets the size of the grid
    boolean 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 coordinates
    java.lang.String toRLE()
    Converts the entire pattern into an RLE
    java.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.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator

    Methods inherited from interface java.util.Iterator

    forEachRemaining, remove
  • Field Details

  • Constructor Details

    • Grid

      public Grid()
      Constructs a grid with an empty pattern and a background of 0. The bounds of the grid are also uninitialised.
    • Grid

      public Grid​(java.lang.String RLE)
      Constructs a grid the RLE pattern and a background of 0.
      Parameters:
      RLE - The RLE of the pattern
  • Method Details

    • setCell

      public void setCell​(Coordinate coordinate, int state)
      Sets the cell at position coordinate to the specified state
      Parameters:
      coordinate - The coordinate of the cell
      state - 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 cell
      y - The y-coordinate of the cell
      state - The state of the cell
    • addBlock

      public void addBlock​(Coordinate coordinate)
      Adds a block at coordinate (x, y) into the hashmap
      Parameters:
      coordinate - The coordinate of the block that is to be removed
    • removeBlock

      public void removeBlock​(Coordinate coordinate)
      Removes the block at coordinate (x, y) from the hashmap
      Parameters:
      coordinate - The coordinate of the block that is to be removed
    • insertCells

      public void insertCells​(Grid pattern, Coordinate coordinate)
      Inserts the provided pattern at the specified coordinate
      Parameters:
      pattern - The pattern to be inserted
      coordinate - The coordinate where the pattern is to be inserted
    • clearCells

      public void clearCells​(Coordinate start, Coordinate end)
      Clears all cells between the coordinates specified
      Parameters:
      start - The starting coordinate
      end - The end coordinate
    • clearCells

      public void clearCells()
      Clears all cells in the grid
    • getCell

      public int getCell​(Coordinate coordinate)
      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 cell
      y - The y-coordinate of the cell
      Returns:
      Returns the state of the cell
    • getCells

      public Grid getCells​(Coordinate start, Coordinate end)
      Get all cells between the start and end coordinates
      Parameters:
      start - The start coordinate
      end - The end coordinate
      Returns:
      Returns a grid contains the pattern
    • updateBounds

      public void updateBounds()
      Updates the bounds of the grid.
    • getBounds

      public org.javatuples.Pair<Coordinate,​Coordinate> 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

      public Coordinate[] toArray()
      Converts the pattern into an array of coordinates
      Returns:
      Returns an array conains the coordinates
    • reflectCellsX

      public void reflectCellsX​(Coordinate startCoordinate, Coordinate endCoordinate)
      Reflects the cells in the grid between the start and end coordinates horizontally
      Parameters:
      startCoordinate - The start coordinate
      endCoordinate - The end coordinate
    • reflectCellsY

      public void reflectCellsY​(Coordinate startCoordinate, Coordinate endCoordinate)
      Reflects the cells in the grid between the start and end coordinates vertically
      Parameters:
      startCoordinate - The start coordinate
      endCoordinate - The end coordinate
    • rotateCW

      public void rotateCW​(Coordinate startCoordinate, Coordinate endCoordinate)
      Rotates the cells in the grid between the start and end coordinates clockwise
      Parameters:
      startCoordinate - The start coordinate
      endCoordinate - The end coordinate
    • rotateCCW

      public void rotateCCW​(Coordinate startCoordinate, Coordinate endCoordinate)
      Rotates the cells in the grid between the start and end coordinates counter-clockwise
      Parameters:
      startCoordinate - The start coordinate
      endCoordinate - The end coordinate
    • toRLE

      public java.lang.String toRLE​(Coordinate startCoordinate, Coordinate endCoordinate)
      Converts pattern between the start and end coordinates to the run length encoded (RLE) format.
      Parameters:
      startCoordinate - The start coordinate
      endCoordinate - 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

      public void fromRLE​(java.lang.String rle, Coordinate startCoordinate)
      Inserts the RLE into the grid
      Parameters:
      rle - The RLE body (no headers, no \n)
      startCoordinate - The coordinate to insert the new cells
    • fromApgcode

      public void fromApgcode​(java.lang.String apgcode, Coordinate startCoordinate)
      Inserts the apgcode into the grid
      Parameters:
      apgcode - The apgcode to insert into the grid
      startCoordinate - The coordinate to insert the new cells
    • bfs

      public java.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.
      Parameters:
      distance - The "distance" from the living cell
      neighbourhood - The neighbourhood to use in the BFS
      Returns:
      Returns the list of cells that are a certain distance from the living cells
    • deepCopy

      public Grid deepCopy()
      Deep copies the grid
      Returns:
      A deep copy of the grid
    • shallowCopy

      public 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

      public boolean slowEquals​(Grid grid, int displacementX, int displacementY)
      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 grid
      displacementY - 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 class java.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 class java.lang.Object
      Returns:
      Returns the grid's hash (uses Golly's hash algorithm).
    • hashCode

      public int hashCode​(Coordinate startCoordinate, Coordinate endCoordinate)
      Gets the hash of the grid.
      Parameters:
      startCoordinate - The start coordinate of the region where the hash is calculated
      endCoordinate - The end coordinate of the region where the hash is calculated
      Returns:
      Returns the grid's hash (uses Golly's hash algorithm).
    • hashCode

      public int hashCode​(java.util.List<Coordinate> coordinates, Coordinate startCoordinate)
      Gets the hash of the grid.
      Parameters:
      coordinates - The coordinates to consider in the hash algorithm
      startCoordinate - 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

      public void iterateCells​(java.util.function.Consumer<Coordinate> methodToCall)
      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

      public void iterateCellsInBlock​(java.util.function.Consumer<Coordinate> methodToCall, Block block)
      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 parameter
      block - The block to iterate through
    • getBlockCoordinate

      public Coordinate getBlockCoordinate​(Coordinate coordinate)
      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

      public Block getBlock​(Coordinate coordinate)
      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

      public java.util.Collection<Block> getBlocks()
      Gets the blocks in the grid
      Returns:
      Returns the blocks in the grid
    • getBlockCoordinates

      public java.util.Set<Coordinate> 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 interface java.util.Iterator<Block>
    • next

      public Block next()
      Specified by:
      next in interface java.util.Iterator<Block>
    • iterator

      public java.util.Iterator<Block> iterator()
      Specified by:
      iterator in interface java.lang.Iterable<Block>