Exemplo n.º 1
0
        public void Should_ReturnNull_When_OutOfBoundsCellRetrieved()
        {
            // Arrange
            Generation generation = new Generation(universeSize: 2);

            // Act
            Cell cell = generation.GetCell(row: 3, column: 3);

            // Assert
            Assert.IsNull(cell);
        }
Exemplo n.º 2
0
        public void Should_MakeCellAlive_When_DeadCellToggled()
        {
            // Arrange
            Generation initialGeneration = new Generation(universeSize: 2);

            // Act
            initialGeneration.ToggleCellLife(0, 0);

            // Assert
            Assert.AreEqual(initialGeneration.GetCell(0, 0).Alive, true);
        }
Exemplo n.º 3
0
        public void Should_LetCellLive_When_CellHassTwoOrThreeLiveNeighbours()
        {
            // Arrange
            Generation initialGeneration = new Generation(universeSize: 5);
            initialGeneration.ToggleCellLife(0, 0);
            initialGeneration.ToggleCellLife(0, 1);
            initialGeneration.ToggleCellLife(0, 2);

            // Act
            EvolutionEngine engine = new EvolutionEngine(initialGeneration);
            engine.EvolveGeneration();

            // Assert
            Assert.AreEqual(initialGeneration.GetCell(0, 1).Alive, true);
        }
Exemplo n.º 4
0
        public void Should_KillCell_When_CellHasFewerThanTwoNeighbors()
        {
            // Arrange
            Generation initialGeneration = new Generation(universeSize: 5);
            initialGeneration.ToggleCellLife(0, 0);
            initialGeneration.ToggleCellLife(0, 1);

            // Act
            EvolutionEngine engine = new EvolutionEngine(initialGeneration);
            engine.EvolveGeneration();

            // Assert
            Assert.AreEqual(initialGeneration.GetCell(0, 0).Alive, false);
            Assert.AreEqual(initialGeneration.GetCell(0, 1).Alive, false);
        }
Exemplo n.º 5
0
        public void Should_SetAllCellsToDead_When_Initialised()
        {
            // Arrange
            Generation generation;

            // Act
            generation = new Generation(universeSize: 5);

            // Assert
            for (int row = 0; row < generation.UniverseSize; row++)
            {
                for (int column = 0; column < generation.UniverseSize; column++)
                {
                    Assert.AreEqual(generation.GetCell(row, column).Alive, false);
                }
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Initialises a new instance of the LifeEngine with a specified initial generation.
 /// </summary>
 /// <param name="initialGeneration">The initial generation to start from.</param>
 public EvolutionEngine(Generation initialGeneration)
 {
     CurrentGeneration = initialGeneration;
     CurrentGenerationNumber = 1;
 }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the number of neighbor cells that are alive for a particular cell.
        /// </summary>
        /// <param name="generation">The generation.</param>
        /// <param name="cell">The cell whose living neighbour are being counted.</param>
        /// <returns>Number of alive neighbours for the specified cell.</returns>
        private int GetNumberOfAliveNeighbors(Generation generation, Cell cell)
        {
            int numberOfAliveNeighbours = 0;

            List<Cell> neighboringCells = new List<Cell>
            {
                generation.GetCell(cell.Row - 1, cell.Column - 1),
                generation.GetCell(cell.Row - 1, cell.Column + 1),
                generation.GetCell(cell.Row, cell.Column + 1),
                generation.GetCell(cell.Row + 1, cell.Column + 1),
                generation.GetCell(cell.Row + 1, cell.Column),
                generation.GetCell(cell.Row + 1, cell.Column - 1),
                generation.GetCell(cell.Row, cell.Column - 1),
                generation.GetCell(cell.Row - 1, cell.Column)
            };

            neighboringCells.ForEach(
                neighboringCell => numberOfAliveNeighbours +=
                    (neighboringCell != null && neighboringCell.Alive) ? 1 : 0
            );

            return numberOfAliveNeighbours;
        }