Пример #1
0
        public void IterateLiveCells_GivenGridWithOneLiveCell_InvokesActionOnce()
        {
            // Arrange
            var grid = new Grid();
            grid.MarkLiveCellAt(Coords.Create(12, 13));
            var numInvocations = 0;

            // Act
            grid.IterateLiveCells((_, __) => numInvocations++);

            // Assert
            Assert.That(numInvocations, Is.EqualTo(1));
        }
Пример #2
0
        public void MarkLiveCellAt_CalledTwiceForTheSameCell_DoesNotAddCellTwice()
        {
            // Arrange
            var grid = new Grid();
            var coords = Coords.Create(12, 13);
            grid.MarkLiveCellAt(coords);
            grid.MarkLiveCellAt(coords);
            var numInvocations = 0;

            // Act
            grid.IterateLiveCells((_, __) => numInvocations++);

            // Assert
            Assert.That(numInvocations, Is.EqualTo(1));
        }
Пример #3
0
        public static Grid NextGeneration(Grid currentGrid)
        {
            var nextGrid = new Grid();

            currentGrid.IterateLiveCells((coords, cellState) =>
            {
                if (CurrentlyAliveCellWillStillBeALiveInTheNextGeneration(currentGrid, coords))
                {
                    nextGrid.MarkLiveCellAt(coords);
                }
                else
                {
                    nextGrid.MarkWasPreviouslyAliveCellAt(coords);
                }

                foreach (var neighbour in coords.Neighbours())
                {
                    if (!currentGrid.IsLiveCellAt(neighbour))
                    {
                        if (CurrentlyDeadCellWillBecomeALiveInTheNextGeneration(currentGrid, neighbour))
                        {
                            if (currentGrid.CellWasPreviouslyAliveAt(neighbour))
                            {
                                nextGrid.MarkZombieCellAt(neighbour);
                            }
                            else
                            {
                                nextGrid.MarkLiveCellAt(neighbour);
                            }
                        }
                    }
                }
            });

            return nextGrid;
        }