public static Position[] Tick(Position[] positions) { if (positions == null || positions.Length == 0) { return(new Position[0]); } // check the dead neighbours of positions if it needs to spawn new cells var newLivePositions = new List <Position>(); foreach (var position in positions) { var deadNeighbours = position.GetNeighbourPositions().Where(p => !positions.Contains(p)); // spawn a new cell if it has 3 neighbours newLivePositions.AddRange(deadNeighbours.Where(deadNeighbour => deadNeighbour.GetNeighbourPositions().Count(positions.Contains) == 3)); } // check all the live cells, deci return((from position in positions let neighbourCount = position.GetNeighbourPositions().Count(positions.Contains) where Cell.IsAlive(neighbourCount) select position) .Concat(newLivePositions) .ToArray()); }
public void DeadCellWithThreeNeighboursShouldBecomeLive() { bool nextState = Cell.IsAlive(3); Assert.That(nextState, Is.True); }
public void LiveCellWithMoreThanThreeNighboursShouldDie(int noOfNeighbours) { bool nextState = Cell.IsAlive(noOfNeighbours); Assert.That(nextState, Is.False); }
public void LiveCellWithTwoOrThreeNeighbourShouldLive(int noOfNeighbours) { bool nextState = Cell.IsAlive(noOfNeighbours); Assert.That(nextState, Is.True); }
public void LiveCellWithLessThanTwoNeighbourShouldDie(int noOfNeighbours) { bool nextState = Cell.IsAlive(noOfNeighbours); Assert.That(nextState, Is.False); }