Class CellularAutomaton

java.lang.Object
squidpony.squidmath.CellularAutomaton

public class CellularAutomaton
extends Object
Created by Tommy Ettinger on 7/3/2017.
  • Field Summary

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

    Constructors 
    Constructor Description
    CellularAutomaton()
    Constructs a CellularAutomaton with an unfilled 64x64 GreasedRegion, that can be altered later via current.
    CellularAutomaton​(int width, int height)
    Constructs a CellularAutomaton with an unfilled GreasedRegion of the specified width and height, that can be altered later via current.
    CellularAutomaton​(GreasedRegion 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
    CellularAutomaton remake​(GreasedRegion next)
    Re-initializes this CellularAutomaton using a different GreasedRegion as a basis.
    GreasedRegion 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.
    GreasedRegion runDiagonalGapCleanup()
    This takes the current GreasedRegion and removes any cells that have a diagonal neighbor if that neighbor cannot be accessed from shared orthogonal neighbors.
    GreasedRegion 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.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • CellularAutomaton

      Constructs a CellularAutomaton with an unfilled 64x64 GreasedRegion, that can be altered later via current.
    • CellularAutomaton

      public CellularAutomaton​(int width, int height)
      Constructs a CellularAutomaton with an unfilled GreasedRegion 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​(GreasedRegion current)
      Stores a direct reference to current as this object's current field, and initializes the other necessary fields.
      Parameters:
      current - a GreasedRegion that will be used directly; changes will be shared
  • Method Details

    • remake

      Re-initializes this CellularAutomaton using a different GreasedRegion as a basis. If the previous GreasedRegion 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 GreasedRegions adjust in size, which involves some allocation. If next is null, this does nothing and returns itself without changes.
      Parameters:
      next - a GreasedRegion to set this CellularAutomaton to read from and adjust
      Returns:
      this, for chaining
    • 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 GreasedRegion 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 GreasedRegion this considers its main state, current
    • 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 GreasedRegion this considers its main state, current
    • runDiagonalGapCleanup

      This takes the current GreasedRegion 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