Struct cellumina::rule::EnvironmentRule
source · pub struct EnvironmentRule {
pub row_range: usize,
pub col_range: usize,
pub boundaries: (BoundaryBehaviour, BoundaryBehaviour),
pub cell_transform: fn(_: &CellGrid) -> char,
}
Expand description
An environment rule uses the neighborhood (up to a certain range as specified) of a cell and applies a function to it. The result of this function is the next value of that cell. Applying this to each cell yields the entire transformation.
Note that each application of the cell_transform
function will read from the entire untransformed array.
Also, the environment will wrap around the grid edges.
use cellumina::rule::Rule;
let rule = cellumina::rule::EnvironmentRule {
row_range: 1,
col_range: 1,
boundaries: (cellumina::rule::BoundaryBehaviour::Periodic, cellumina::rule::BoundaryBehaviour::Periodic),
cell_transform: |env: &cellumina::CellGrid| match env
// Iterate over neighbors.
.iter()
.enumerate()
.map(|val| match val {
// The cell we are transforming does not get counted.
(4, 'X') => 0,
// Any cell containing an 'X' counts for 1 (alive).
(_, 'X') => 1,
// Any cell containing any other entry (only ' ' in our initial configuration) counts as 0 (dead).
_ => 0,
})
// Sum over these 9 values...
.sum()
// ... and map the sum to the new enty of our cell:
{
// 2 neighbors: The cell keeps its state.
2 => env[1][1],
// 3 neighbors: The cell gets born.
3 => 'X',
// 0, 1 or more than 3 neighbors: The cell dies.
_ => ' ',
},
};
let mut grid = grid::grid![[' ', ' ', 'X', ' ', ' '][' ', ' ', 'X',' ', ' '][' ', ' ', ' ', ' ', ' '][' ', ' ', 'X', ' ', ' '][' ', ' ', 'X', ' ', ' ']];
rule.transform(&mut grid);
assert_eq!(
grid,
grid::grid![[' ', 'X', 'X', 'X', ' '][' ', ' ', ' ',' ', ' '][' ', ' ', ' ', ' ', ' '][' ', ' ', ' ', ' ', ' '][' ', 'X', 'X', 'X', ' ']]
);
rule.transform(&mut grid);
rule.transform(&mut grid);
rule.transform(&mut grid);
assert_eq!(
grid,
grid::grid![[' ', 'X', ' ', 'X', ' '][' ', ' ', 'X',' ', ' '][' ', ' ', ' ', ' ', ' '][' ', ' ', 'X', ' ', ' '][' ', 'X', ' ', 'X', ' ']]
);
Fields§
§row_range: usize
The vertical range of an environment, extending in both direction from the cell to be transformed.
Your cell_transform
function will receive a grid of height 2 * row_range + 1
, centered on the cell that will be replaced by the output.
col_range: usize
The horizontal range of an environment, extending in both direction from the cell to be transformed.
Your cell_transform
function will receive a grid of width 2 * col_range + 1
, centered on the cell that will be replaced by the output.
boundaries: (BoundaryBehaviour, BoundaryBehaviour)
How the rule is supposed to handle cells at the edges of the state space. The first item describes how to handle trying to access rows out of range, the second columns out of range.
cell_transform: fn(_: &CellGrid) -> char
The function that calculates the next state of a single cell based on its environment.
Receives a grid of size 2 * row_range + 1
x 2 * col_range + 1
. Must return a character.
In the next iteration after applying this rule, the cell in the center of the received grid will contain the return value of this function.
Trait Implementations§
source§impl Clone for EnvironmentRule
impl Clone for EnvironmentRule
source§fn clone(&self) -> EnvironmentRule
fn clone(&self) -> EnvironmentRule
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for EnvironmentRule
impl Debug for EnvironmentRule
source§impl Default for EnvironmentRule
impl Default for EnvironmentRule
source§impl Rule for EnvironmentRule
impl Rule for EnvironmentRule
impl Copy for EnvironmentRule
Auto Trait Implementations§
impl RefUnwindSafe for EnvironmentRule
impl Send for EnvironmentRule
impl Sync for EnvironmentRule
impl Unpin for EnvironmentRule
impl UnwindSafe for EnvironmentRule
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<R, P> ReadPrimitive<R> for Pwhere
R: Read + ReadEndian<P>,
P: Default,
impl<R, P> ReadPrimitive<R> for Pwhere R: Read + ReadEndian<P>, P: Default,
source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.