Class CA2D
A 2D discrete cellular automaton (CA).
public class CA2D
- Inheritance
-
CA2D
- Inherited Members
Remarks
Instances of this class represent a cellular automaton, but the class also provides CA-related functionality via static methods for those interested in directly handling the CA state.
Constructors
CA2D(ICA2DRule, int, int, bool, int)
Create a new 2D discrete cellular automaton.
public CA2D(ICA2DRule rule, int xDim, int yDim, bool toroidal, int nonToroidalBorderCells = 0)
Parameters
ruleICA2DRuleTransition rule governing the CA.
xDimintWidth of the CA.
yDimintHeight of the CA.
toroidalboolIs the CA toroidal?
nonToroidalBorderCellsintThe value of off-grid cells in case the CA is not toroidal.
Properties
this[int, int]
A read-only indexer returning the value of the CA cell at the specified indexes.
public int this[int x, int y] { get; }
Parameters
Property Value
- int
Value of the CA cell at the specified indexes.
NonToroidalOffGridCells
What is the value of off-grid cells in case the CA is not toroidal?
public int NonToroidalOffGridCells { get; }
Property Value
Toroidal
Is the CA toroidal?
public bool Toroidal { get; }
Property Value
XDim
Width of the CA.
public int XDim { get; }
Property Value
YDim
Height of the CA.
public int YDim { get; }
Property Value
Methods
CountNeighbors(int, int, int, Neighborhood, int, bool)
Count neighbors with a specific value around a given cell.
public int CountNeighbors(int x, int y, int radius, Neighborhood neighType = Neighborhood.Moore, int neighValue = 1, bool countSelf = false)
Parameters
xintHorizontal position of the cell.
yintVertical position of the cell.
radiusintNeighborhood radius to search.
neighTypeNeighborhoodNeighborhood type.
neighValueintCount neighbors with this value.
countSelfboolCount self?
Returns
- int
Number of neighbors with a specific value around a given cell.
CountNeighbors(int[], int, int, int, int, bool, int, int, int, bool, Neighborhood)
Count neighbors with a specific value around a given cell.
public static int CountNeighbors(int[] map, int xDim, int yDim, int xCell, int yCell, bool toroidal = true, int nonToroidalOffGridCells = 0, int radius = 1, int neighValue = 1, bool countSelf = false, Neighborhood neighType = Neighborhood.Moore)
Parameters
mapint[]An integer array representing a grid in row-major format.
xDimintGrid width.
yDimintGrid height.
xCellintHorizontal cell position.
yCellintVertical cell position.
toroidalboolConsider the grid toroidal?
nonToroidalOffGridCellsintValue of the off-grid cells in case the grid is not toroidal.
radiusintNeighborhood radius.
neighValueintValue of neighbors to count.
countSelfboolCount central cell?
neighTypeNeighborhoodNeighborhood type.
Returns
- int
The number of neighbors with a specific value around a given cell.
DoStep()
Perform a step in the CA evolution.
public void DoStep()
Exceptions
- InvalidOperationException
Exception thrown if the CA is not initialized.
InitExact(int[,])
Initialize the CA with the exact state given.
public void InitExact(int[,] initialState)
Parameters
initialStateint[,]The state with which to initialize the CA.
Exceptions
- ArgumentException
Exception thrown if the size of the given initial state is different from the size of the CA grid.
InitExact(int[])
Initialize the CA with the exact state given.
public void InitExact(int[] initialState)
Parameters
initialStateint[]The state with which to initialize the CA in row-major order.
Exceptions
- ArgumentException
Exception thrown if the size of given initial state is different from the size of the CA grid.
InitFunc(Func<int, int, int>)
Initialize the CA using a delegate.
public void InitFunc(Func<int, int, int> initializer)
Parameters
initializerFunc<int, int, int>Delegate used to initialize the CA, which should accept the
xandycell position and return its value.
InitRandom(int[], float[], Func<float>)
Randomly initialize the CA with an external random number generator.
public void InitRandom(int[] values, float[] probabilities, Func<float> nextFloat)
Parameters
valuesint[]Possible values with which to initialize the cells.
probabilitiesfloat[]Probabilities of each value being assigned. If not given, each value will be assigned with equal probability.
nextFloatFunc<float>Delegate which will return a random float between 0 and 1.
InitRandom(int[], float[], int?)
Randomly initialize the CA using the system's random number generator.
public void InitRandom(int[] values, float[] probabilities = null, int? seed = null)
Parameters
valuesint[]Possible values with which to initialize the cells.
probabilitiesfloat[]Probabilities of each value being assigned. If not given, each value will be assigned with equal probability.
seedint?Seed for the random number generator. If not given, generator will be randomly initialized.
RandomFill(int[], int[], float[], Func<float>)
Randomly fill the map with the given
values with the specified
probabilities using a delegate method for
random number generation.
public static void RandomFill(int[] map, int[] values, float[] probabilities, Func<float> nextFloat)
Parameters
mapint[]Integer array to fill.
valuesint[]Values with which to fill the array.
probabilitiesfloat[]Probabilities associated with each value.
nextFloatFunc<float>Delegate which will return a random float between 0 and 1.
Exceptions
- InvalidOperationException
Thrown if
valuesandprobabilitieshave different lengths.