|
using System.Collections.Generic;
namespace gameOfLife.Framework
{
/// <summary>
/// Singleton implementation of ITransitive.
/// Only exposed is the ITransitive interface of this class.
/// </summary>
public class PatternOfCells : ITransitive
{
private IList<Cell> cells = new List<Cell>();
// threadsafe inialization
private static ITransitive instance = new PatternOfCells();
private PatternOfCells()
{
}
/// <summary>
/// Singleton Instance
/// </summary>
public static ITransitive Instance
{
get
{
// do not need double lock here
return instance;
}
}
/// <summary>
/// Apply a set of transitions to this Transitive.
/// Dead cells should be removed after every set of transition chain
/// Domain Meaning - A Transitive is something which uses transitions to change its pattern
/// </summary>
/// <param name="transitions">list of ITransition implementations</param>
public void ApplyTransitions(params ITransition[] transitions)
{
foreach (ITransition transition in transitions)
{
transition.Apply(this);
}
ClearDeadCells();
}
/// <summary>
/// Current set of alive (and dead) cells in the pattern.
/// </summary>
public IList<Cell> Cells
{
get
{
return cells;
}
set
{
cells = value;
}
}
private void ClearDeadCells()
{
for (int i = 0; i < Cells.Count; i++)
{
if (!Cells[i].IsAlive)
{
Cells.RemoveAt(i--); // decrement i so that there is no miss of current item
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
is a poor software developer and thinker. Presently working on a theory of "complementary perception". It's a work in progress.