Beispiel #1
0
        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());
        }
Beispiel #2
0
        public void DeadCellWithThreeNeighboursShouldBecomeLive()
        {
            bool nextState = Cell.IsAlive(3);

            Assert.That(nextState, Is.True);
        }
Beispiel #3
0
        public void LiveCellWithMoreThanThreeNighboursShouldDie(int noOfNeighbours)
        {
            bool nextState = Cell.IsAlive(noOfNeighbours);

            Assert.That(nextState, Is.False);
        }
Beispiel #4
0
        public void LiveCellWithTwoOrThreeNeighbourShouldLive(int noOfNeighbours)
        {
            bool nextState = Cell.IsAlive(noOfNeighbours);

            Assert.That(nextState, Is.True);
        }
Beispiel #5
0
        public void LiveCellWithLessThanTwoNeighbourShouldDie(int noOfNeighbours)
        {
            bool nextState = Cell.IsAlive(noOfNeighbours);

            Assert.That(nextState, Is.False);
        }