Exemplo n.º 1
0
        private static Board CalculateNextGeneration(Board board)
        {
            var rows                = board.Grid.Count;
            var columns             = board.Grid[0].Length;
            var nextGenerationBoard = BoardFactory.Create(rows, columns, 0);

            // Loop through every cell
            for (var row = 1; row < rows - 1; row++)
            {
                for (var column = 1; column < columns - 1; column++)
                {
                    // find your alive neighbors
                    var aliveNeighbors = 0;
                    for (var i = -1; i <= 1; i++)
                    {
                        for (var j = -1; j <= 1; j++)
                        {
                            aliveNeighbors += board[row + i, column + j] ? 1 : 0;
                        }
                    }

                    var currentCell = board[row, column];

                    // The cell needs to be subtracted
                    // from its neighbors as it was
                    // counted before
                    aliveNeighbors -= currentCell ? 1 : 0;

                    // Implementing the Rules of Life

                    // Cell is lonely and dies
                    if (currentCell && aliveNeighbors < 2)
                    {
                        nextGenerationBoard[row, column] = false;
                    }

                    // Cell dies due to over population
                    else if (currentCell == aliveNeighbors > 3)
                    {
                        nextGenerationBoard[row, column] = false;
                    }

                    // A new cell is born
                    else if (currentCell == false && aliveNeighbors == 3)
                    {
                        nextGenerationBoard[row, column] = true;
                    }
                    // All other cells stay the same
                    else
                    {
                        nextGenerationBoard[row, column] = currentCell;
                    }
                }
            }

            return(nextGenerationBoard);
        }
Exemplo n.º 2
0
        public static Game Create(int rows, int columns, int minGenerationsAlive, double maxCoverage = 1)
        {
            //Console.WriteLine($"Start Create Game witch alive at minimum {minGenerationsAlive} generations");

            var random = new Random();

            while (true)
            {
                var coverage = random.NextDouble() * maxCoverage;
                var board    = BoardFactory.Create(rows, columns, coverage);
                var game     = new Game(board);
                game.Play(minGenerationsAlive);

                if (game.Generation >= minGenerationsAlive)
                {
                    return(game);
                }
            }
        }