Package squidpony.squidmath
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 viacurrent
.CellularAutomaton(int width, int height)
Constructs a CellularAutomaton with an unfilled GreasedRegion of the specified width and height, that can be altered later viacurrent
.CellularAutomaton(GreasedRegion current)
Stores a direct reference tocurrent
as this object'scurrent
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 thecurrent
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.
-
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 GreasedRegion, that can be altered later viacurrent
. -
CellularAutomaton
Constructs a CellularAutomaton with an unfilled GreasedRegion 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
Stores a direct reference tocurrent
as this object'scurrent
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 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 GreasedRegions adjust in size, which involves some allocation. Ifnext
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 thecurrent
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."
- Returns:
- a direct reference to the changed GreasedRegion this considers its main state,
current
-
runDiagonalGapCleanup
This takes thecurrent
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
-