コード例 #1
0
        private void SeedGrid()
        {
            Random seedCoordinates = new Random();

            for (int x = 0; x < Height; x++)
            {
                for (int y = 0; y < Width; y++)
                {
                    int cellIsAlive = seedCoordinates.Next(2);
                    if (cellIsAlive == 1)
                    {
                        _grid[x, y] = new Alive();
                    }
                    else
                    {
                        _grid[x, y] = new Dead();
                    }
                }
            }
        }
コード例 #2
0
        public void Advance()
        {
            for (int x = 0; x < Height; x++)
            {
                for (int y = 0; y < Width; y++)
                {
                    _grid[x, y].NumberOfLiveNeighbours = GetCountOfLiveNeighbours(x, y);
                }
            }

            for (int x = 0; x < Height; x++)
            {
                for (int y = 0; y < Width; y++)
                {
                    var cell = _grid[x, y];
                    if (cell is Alive)
                    {
                        if (cell.NumberOfLiveNeighbours < 2)
                        {
                            _grid[x, y] = new Dead();
                        }
                        if (cell.NumberOfLiveNeighbours == 2 || cell.NumberOfLiveNeighbours == 3)
                        {
                            _grid[x, y] = new Alive();
                        }
                        if (cell.NumberOfLiveNeighbours > 3)
                        {
                            _grid[x, y] = new Dead();
                        }
                    }

                    if (cell is Dead)
                    {
                        if (cell.NumberOfLiveNeighbours == 3)
                        {
                            _grid[x, y] = new Alive();
                        }
                    }
                }
            }
        }