Table of Contents

Class CA2D

Namespace
LibGameAI.PCG
Assembly
PCG.dll

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

rule ICA2DRule

Transition rule governing the CA.

xDim int

Width of the CA.

yDim int

Height of the CA.

toroidal bool

Is the CA toroidal?

nonToroidalBorderCells int

The 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

x int

Horizontal position of the cell.

y int

Vertical position of the cell.

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

int

Toroidal

Is the CA toroidal?

public bool Toroidal { get; }

Property Value

bool

XDim

Width of the CA.

public int XDim { get; }

Property Value

int

YDim

Height of the CA.

public int YDim { get; }

Property Value

int

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

x int

Horizontal position of the cell.

y int

Vertical position of the cell.

radius int

Neighborhood radius to search.

neighType Neighborhood

Neighborhood type.

neighValue int

Count neighbors with this value.

countSelf bool

Count 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

map int[]

An integer array representing a grid in row-major format.

xDim int

Grid width.

yDim int

Grid height.

xCell int

Horizontal cell position.

yCell int

Vertical cell position.

toroidal bool

Consider the grid toroidal?

nonToroidalOffGridCells int

Value of the off-grid cells in case the grid is not toroidal.

radius int

Neighborhood radius.

neighValue int

Value of neighbors to count.

countSelf bool

Count central cell?

neighType Neighborhood

Neighborhood 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

initialState int[,]

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

initialState int[]

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

initializer Func<int, int, int>

Delegate used to initialize the CA, which should accept the x and y cell 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

values int[]

Possible values with which to initialize the cells.

probabilities float[]

Probabilities of each value being assigned. If not given, each value will be assigned with equal probability.

nextFloat Func<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

values int[]

Possible values with which to initialize the cells.

probabilities float[]

Probabilities of each value being assigned. If not given, each value will be assigned with equal probability.

seed int?

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

map int[]

Integer array to fill.

values int[]

Values with which to fill the array.

probabilities float[]

Probabilities associated with each value.

nextFloat Func<float>

Delegate which will return a random float between 0 and 1.

Exceptions

InvalidOperationException

Thrown if values and probabilities have different lengths.