Example #1
0
        public void Should_get_neighbours_of_corner()
        {
            var cell = _sut[0, 0];

            var result = _sut.GetNeighbours(cell);

            Assert.That(result.Count() == 3);
            Assert.That(!result.Any(x => x.Equals(cell)));
        }
Example #2
0
        public void GenerateNextState(CellField field)
        {
            var changedCells = new List <Cell>();

            for (int j = 0; j < field.Height; j++)
            {
                for (int i = 0; i < field.Width; i++)
                {
                    var cell            = field[i, j];
                    var neighboursAlive = field.GetNeighbours(cell).Count(x => x.IsAlive);

                    if (cell.IsAlive)
                    {
                        if (neighboursAlive < _neighboursToLiveMin)
                        {
                            changedCells.Add(new Cell(cell.PosX, cell.PosY, false));
                        }
                        else if (neighboursAlive > _neighboursToLiveMax)
                        {
                            changedCells.Add(new Cell(cell.PosX, cell.PosY, false));
                        }
                    }
                    else
                    {
                        if (neighboursAlive == _neighboursToReborn)
                        {
                            changedCells.Add(new Cell(cell.PosX, cell.PosY, true));
                        }
                    }
                }
            }

            foreach (var cell in changedCells)
            {
                field[cell.PosX, cell.PosY].IsAlive = cell.IsAlive;
            }
        }