Class CellularAutomaton
java.lang.Object
com.github.yellowstonegames.grid.CellularAutomaton
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 -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a CellularAutomaton with an unfilled 64x64 Region, that can be altered later viacurrent.CellularAutomaton(int width, int height) Constructs a CellularAutomaton with an unfilled Region of the specified width and height, that can be altered later viacurrent.CellularAutomaton(Region current) Stores a direct reference tocurrentas this object'scurrentfield, and initializes the other necessary fields. -
Method Summary
Modifier and TypeMethodDescriptionRe-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 thecurrentRegion and removes any cells that have a diagonal neighbor if that neighbor cannot be accessed from shared orthogonal neighbors.This takes thecurrentRegion, 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.
-
Field Details
-
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 viacurrent. -
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 viacurrent.- Parameters:
width- the width of the CellularAutomatonheight- the height of the CellularAutomaton
-
CellularAutomaton
-
-
Method Details
-
remake
Re-initializes this CellularAutomaton using a different Region as a basis. If the previous Region used has the same dimensions asnext, 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. Ifnextis 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
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 thecurrentRegion 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
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."
- Returns:
- a direct reference to the changed Region this considers its main state,
current
-
runDiagonalGapCleanup
This takes thecurrentRegion 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:
currentafter orthogonally-inaccessible pairs of diagonal "on" cells are removed
-
runDiagonalGapWiden
This takes thecurrentRegion, 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:
currentafter orthogonally-inaccessible pairs of diagonal "on" cells are widened
-