Conway Class
Constructor
Item Index
Methods
Properties
Events
Methods
_cellChange
-
eventFacade
Default function for the cellChange event.
Parameters:
-
eventFacade
Object
_cellStep
-
grid
-
height
-
width
-
index
-
birth
-
life
-
toroidal
Determines the outcome of one step of the simulation for one cell.
Parameters:
Returns:
- cell
- Boolean. The current value of the cell.
- index
- Number. The cell's array index.
- x
- Number. The cell's x coordinate.
- y
- Number. The cell's y coordinate.
getCell
-
x
-
[y]
Returns the value of a specific cell. The cell may be specified with x and y coordinates or with a single array index. (index = x + y * width)
Parameters:
Returns:
setCell
-
x
-
[y]
-
value
Sets the value of a specific cell. The cell may be specified with x and y coordinates or with a single array index. (index = x + y * width)
step
-
[callbackFunction]
Runs one step of the simulation. The simulation is resolved asynchronously and will call the callback function when it completes. Once this method is called, it should not be called again until the first call is complete. Calling getCell, setCell, or step while the simulation is still being resolved may cause unexpected side effects. If you wish to ensure that unexpected side effects never occur and don't mind incurring a small overhead cost, use gallery-mutex to obtain an exclusive lock when calling these methods.
Parameters:
-
[callbackFunction]
Function optionalOptional. This function will be called once the simulation is resolved. This function will be passed a single argument, an array. For each cell that changed value during the simulation, there will be an object in the array with the following properties:
- cell
- Boolean. The current value of the cell.
- index
- Number. The cell's array index.
- x
- Number. The cell's x coordinate.
- y
- Number. The cell's y coordinate.
Properties
_history
Object
protected
This object's keys are the indices of cells that have changed value since the previous step of the simulation. This is used to improve performance while resolving the next step of the simulation.
Attributes
birth
[Number]
In Conway's Game of Life, a cell is born (becomes alive) if it is not currently alive and if it has exactly three living neighbors. This is the default functionality but it can be modified. For example, if this attribute is set to [1,4,6] a cell will be born if it is not currently alive and if it has exactly one, four, or six living neighbors.
Default: [3]
Fires event birthChange
Fires when the value for the configuration attribute birth
is
changed. You can listen for the event using the on
method if you
wish to be notified before the attribute's value has changed, or
using the after
method if you wish to be notified after the
attribute's value has changed.
Parameters:
-
e
EventFacadeAn Event Facade object with the following attribute-specific properties added:
grid
[Boolean]
The grid of cell values. The array values should never be manipulated directly; doing so may cause unexpected side effects.
Fires event gridChange
Fires when the value for the configuration attribute grid
is
changed. You can listen for the event using the on
method if you
wish to be notified before the attribute's value has changed, or
using the after
method if you wish to be notified after the
attribute's value has changed.
Parameters:
-
e
EventFacadeAn Event Facade object with the following attribute-specific properties added:
height
Number
The height of the grid.
Fires event heightChange
Fires when the value for the configuration attribute height
is
changed. You can listen for the event using the on
method if you
wish to be notified before the attribute's value has changed, or
using the after
method if you wish to be notified after the
attribute's value has changed.
Parameters:
-
e
EventFacadeAn Event Facade object with the following attribute-specific properties added:
life
[Number]
In Conway's Game of Life, a living cell remains alive if it currently has exactly two or three living neighbors. Otherwise the cell will die either from loneliness or overcrowding. This is the default functionality but it can be modified. For example, if this attribute is set to [1,3,5] a living cell will remain alive if it has exactly one, three, or five living neighbors.
Default: [2,3]
Fires event lifeChange
Fires when the value for the configuration attribute life
is
changed. You can listen for the event using the on
method if you
wish to be notified before the attribute's value has changed, or
using the after
method if you wish to be notified after the
attribute's value has changed.
Parameters:
-
e
EventFacadeAn Event Facade object with the following attribute-specific properties added:
toroidal
Boolean
Since the grid is a fixed size, there is a good chance that the simulation will have to interact with the boundaries of the grid. By default, the grid exists on an infinite two-dimensional plane but all cells beyond the limits of the grid are considered to be dead. When this attribute is set to a truthy value, the universe of the simulation will become a three-dimensional torus instead of a two-dimensional plane. The grid will represent the entire surface area of the torus. Basically all this means is, when the grid is toroidal, the left and right edges of the grid will be connected and the top and bottom edges of the grid will be connected, so while there is still a finite area there are no real boundaries.
Default: false
Fires event toroidalChange
Fires when the value for the configuration attribute toroidal
is
changed. You can listen for the event using the on
method if you
wish to be notified before the attribute's value has changed, or
using the after
method if you wish to be notified after the
attribute's value has changed.
Parameters:
-
e
EventFacadeAn Event Facade object with the following attribute-specific properties added:
width
Number
The width of the grid.
Fires event widthChange
Fires when the value for the configuration attribute width
is
changed. You can listen for the event using the on
method if you
wish to be notified before the attribute's value has changed, or
using the after
method if you wish to be notified after the
attribute's value has changed.
Parameters:
-
e
EventFacadeAn Event Facade object with the following attribute-specific properties added:
Events
cellChange
If this event is prevented, the cell's value will not be changed.
Event Payload:
-
cell
BooleanThe current value of the cell. Note that the cell's value is changed by the default function so the value of this property will be different between on and after subscriptions.
-
height
NumberThe current height of the grid.
-
index
NumberThe cell's array index.
-
toroidal
BooleanTrue if the grid is currently toroidal.
-
width
NumberThe current width of the grid.
-
x
NumberThe cell's x coordinate.
-
y
NumberThe cell's y coordinate.