Esempio n. 1
0
        public void IterateLiveCells_GivenGridWithTwoLiveCells_InvokesActionTwice()
        {
            // Arrange
            var grid = new Grid();
            grid.MarkLiveCellAt(Coords.Create(12, 13));
            grid.MarkLiveCellAt(Coords.Create(13, 13));
            var numInvocations = 0;

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

            // Assert
            Assert.That(numInvocations, Is.EqualTo(2));
        }
Esempio n. 2
0
        public void AnyDeadCellWithExactlyTwoLiveNeighboursStaysDead()
        {
            // Arrange
            var grid = new Grid();
            var cellCoords = Coords.Create(1, 1);
            grid.MarkLiveCellAt(cellCoords.Below());
            grid.MarkLiveCellAt(cellCoords.BelowLeft());

            // Act
            var actual = Rules.CurrentlyDeadCellWillBecomeALiveInTheNextGeneration(grid, cellCoords);

            // Assert
            Assert.That(actual, Is.False);
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        public void AnyLiveCellWithFourLiveNeighboursLivesOnToTheNextGeneration()
        {
            // Arrange
            var grid = new Grid();
            var cellCoords = Coords.Create(1, 1);
            grid.MarkLiveCellAt(cellCoords);
            grid.MarkLiveCellAt(cellCoords.Above());
            grid.MarkLiveCellAt(cellCoords.Below());
            grid.MarkLiveCellAt(cellCoords.BelowLeft());
            grid.MarkLiveCellAt(cellCoords.BelowRight());

            // Act
            var actual = Rules.CurrentlyAliveCellWillStillBeALiveInTheNextGeneration(grid, cellCoords);

            // Assert
            Assert.That(actual, Is.False);
        }
Esempio n. 5
0
        public void IsLiveCellAt_GivenCoordsWhereThereIsALiveCell_ReturnsTrue()
        {
            // Arrange
            var grid = new Grid();
            var coords = Coords.Create(12, 13);
            grid.MarkLiveCellAt(coords);

            // Act
            var actual = grid.IsLiveCellAt(coords);

            // Assert
            Assert.That(actual, Is.True);
        }
Esempio n. 6
0
        public void AnyLiveCellWithNoLiveNeighboursDies()
        {
            // Arrange
            var grid = new Grid();
            var cellCoords = Coords.Create(1, 1);
            grid.MarkLiveCellAt(cellCoords);

            // Act
            var actual = Rules.CurrentlyAliveCellWillStillBeALiveInTheNextGeneration(grid, cellCoords);

            // Assert
            Assert.That(actual, Is.False);
        }
Esempio n. 7
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;
        }