public void Evolve() { CellsNeighborhood cellsNeighborhood = new CellsNeighborhood(); CopyCurrentStateToPreviousStates(); foreach (var evolvingCell in PreviousState) { if (IsCellBoundary(evolvingCell)) { cellsNeighborhood = GetCellsNeighborhoodForNotBoundaryCell(evolvingCell); } else { switch (BoundaryCondition) { case BoundaryConditionModel.Absorbing: cellsNeighborhood = GetCellsNeighborhoodForFalseOutsideBc(evolvingCell); break; case BoundaryConditionModel.CounterAbsorbing: cellsNeighborhood = GetCellsNeighborhoodForTrueOutsideBc(evolvingCell); break; case BoundaryConditionModel.Periodic: cellsNeighborhood = GetCellsNeighborhoodForPeriodicalBc(evolvingCell); break; } } CurrentState[evolvingCell.Id].IsAlive = Rule.Table[cellsNeighborhood]; } GenerationCount++; }
private CellsNeighborhood GetCellsNeighborhoodForPeriodicalBc(CellModel cell) { CellsNeighborhood cellsNeighborhood = new CellsNeighborhood(); if (cell.Id == FirstCellId) { cellsNeighborhood.Left = PreviousState[LastCellId].IsAlive; cellsNeighborhood.Center = PreviousState[cell.Id].IsAlive; cellsNeighborhood.Right = PreviousState[cell.Id + 1].IsAlive; } if (cell.Id == LastCellId) { cellsNeighborhood.Left = PreviousState[cell.Id - 1].IsAlive; cellsNeighborhood.Center = PreviousState[cell.Id].IsAlive; cellsNeighborhood.Right = PreviousState[FirstCellId].IsAlive; } return(cellsNeighborhood); }
private CellsNeighborhood GetCellsNeighborhoodForTrueOrFalseOutsideBc(CellModel cell, bool isOutsideAlive) { CellsNeighborhood cellsNeighborhood = new CellsNeighborhood(); if (cell.Id == FirstCellId) { cellsNeighborhood.Left = isOutsideAlive; cellsNeighborhood.Center = PreviousState[cell.Id].IsAlive; cellsNeighborhood.Right = PreviousState[cell.Id + 1].IsAlive; } if (cell.Id == LastCellId) { cellsNeighborhood.Left = PreviousState[cell.Id - 1].IsAlive; cellsNeighborhood.Center = PreviousState[cell.Id].IsAlive; cellsNeighborhood.Right = isOutsideAlive; } return(cellsNeighborhood); }