Class CellularAutomaton

java.lang.Object
com.github.yellowstonegames.grid.CellularAutomaton

public class CellularAutomaton extends Object
Various simple cellular-automata rules and the data they operate on. This has Conway's Game of Life, when you can run on a Region given to the constructor previously with runGameOfLife(); this version of Life doesn't wrap at the edges. It has a smoothing method that rounds off hard edges, runBasicSmoothing(). It also has two methods that may be useful for ensuring maps are always possible to pass through by a creature moving orthogonally-only (Manhattan metric); these are runDiagonalGapCleanup() and runDiagonalGapWiden().
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Returned directly by some methods, but you may want to change this at some other point.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a CellularAutomaton with an unfilled 64x64 Region, that can be altered later via current.
    CellularAutomaton(int width, int height)
    Constructs a CellularAutomaton with an unfilled Region of the specified width and height, that can be altered later via current.
    Stores a direct reference to current as this object's current field, and initializes the other necessary fields.
  • Method Summary

    Modifier and Type
    Method
    Description
    remake(Region next)
    Re-initializes this CellularAutomaton using a different Region as a basis.
    Reduces the sharpness of corners by only considering a cell on if the previous version has 5 of the 9 cells in the containing 3x3 area as "on." Typically, this method is run repeatedly.
    This takes the current Region and removes any cells that have a diagonal neighbor if that neighbor cannot be accessed from shared orthogonal neighbors.
    This takes the current Region, then takes any "on" cells that have an "on" diagonal neighbor and that neighbor cannot be accessed from shared orthogonal neighbors, and sets the shared orthogonal neighbors to "on." That is, if a 2x2 area contains two "off" cells that are diagonally adjacent and contains two "on" cells that are diagonally adjacent, this sets that whole 2x2 area to "on."
    Runs one step of the simulation called Conway's Game of Life, which has relatively simple rules: Any "on" cell with fewer than two "on" neighbors becomes "off." Any "on" cell with two or three "on" neighbors (no more than three) stays "on." Any "on" cell with more than three "on" neighbors becomes "off." Any "off" cell with exactly three "on" neighbors becomes "on." These rules can bring about complex multi-step patterns in many cases, eventually stabilizing to predictable patterns in most cases.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • current

      public Region current
      Returned directly by some methods, but you may want to change this at some other point.
  • Constructor Details

    • CellularAutomaton

      public CellularAutomaton()
      Constructs a CellularAutomaton with an unfilled 64x64 Region, that can be altered later via current.
    • CellularAutomaton

      public CellularAutomaton(int width, int height)
      Constructs a CellularAutomaton with an unfilled Region of the specified width and height, that can be altered later via current.
      Parameters:
      width - the width of the CellularAutomaton
      height - the height of the CellularAutomaton
    • CellularAutomaton

      public CellularAutomaton(Region current)
      Stores a direct reference to current as this object's current field, and initializes the other necessary fields.
      Parameters:
      current - a Region that will be used directly; changes will be shared
  • Method Details

    • remake

      public CellularAutomaton remake(Region next)
      Re-initializes this CellularAutomaton using a different Region as a basis. If the previous Region used has the same dimensions as next, then this performs no allocations and simply sets the existing contents. Otherwise, it makes one new 2D array and also has all 9 of the internal Regions adjust in size, which involves some allocation. If next is null, this does nothing and returns itself without changes.
      Parameters:
      next - a Region to set this CellularAutomaton to read from and adjust
      Returns:
      this, for chaining
    • runBasicSmoothing

      public Region runBasicSmoothing()
      Reduces the sharpness of corners by only considering a cell on if the previous version has 5 of the 9 cells in the containing 3x3 area as "on." Typically, this method is run repeatedly. It does not return itself for chaining, because it returns a direct reference to the current Region that this will use for any future calls to this, and changes to current will be used here.
      Returns:
      a direct reference to the changed Region this considers its main state, current
    • runGameOfLife

      public Region runGameOfLife()
      Runs one step of the simulation called Conway's Game of Life, which has relatively simple rules:
      • Any "on" cell with fewer than two "on" neighbors becomes "off."
      • Any "on" cell with two or three "on" neighbors (no more than three) stays "on."
      • Any "on" cell with more than three "on" neighbors becomes "off."
      • Any "off" cell with exactly three "on" neighbors becomes "on."
      These rules can bring about complex multi-step patterns in many cases, eventually stabilizing to predictable patterns in most cases. Filling the whole state of this CellularAutomaton won't produce interesting patterns most of the time, even if the fill is randomized; you might have better results by using known patterns. Some key well-known patterns are covered on Wikipedia's detailed article on Conway's Game of Life.
      Returns:
      a direct reference to the changed Region this considers its main state, current
    • runDiagonalGapCleanup

      public Region runDiagonalGapCleanup()
      This takes the current Region and removes any cells that have a diagonal neighbor if that neighbor cannot be accessed from shared orthogonal neighbors. That is, if a 2x2 area contains two "off" cells that are diagonally adjacent and contains two "on" cells that are diagonally adjacent, this sets that whole 2x2 area to "off."
      Returns:
      current after orthogonally-inaccessible pairs of diagonal "on" cells are removed
    • runDiagonalGapWiden

      public Region runDiagonalGapWiden()
      This takes the current Region, then takes any "on" cells that have an "on" diagonal neighbor and that neighbor cannot be accessed from shared orthogonal neighbors, and sets the shared orthogonal neighbors to "on." That is, if a 2x2 area contains two "off" cells that are diagonally adjacent and contains two "on" cells that are diagonally adjacent, this sets that whole 2x2 area to "on."
      Returns:
      current after orthogonally-inaccessible pairs of diagonal "on" cells are widened