예제 #1
0
            // Steps forward the specified amount of generations.
            public void Step(uint steps = 1)
            {
                _stopwatch.Restart();

                for (uint step = 0; step < steps; step++)
                {
                    Generation++;

                    // Variable to act as a snapshot of the current state while we make changes.
                    var oldState = new CellCollection(this);

                    // Variable to hold the cells that we will check.
                    var checkCells = new List <Cell>(oldState);

                    // Adds all dead cells neighboring alive cells to the cells that we will check.
                    checkCells.AddRange(
                        from cell in oldState
                        from neighbor in GetNeighbors(cell.X, cell.Y)
                        where !checkCells.Contains(neighbor)
                        select neighbor);

                    foreach (var cell in checkCells)
                    {
                        byte neighbors = oldState.GetAliveNeighborsCount(cell.X, cell.Y);

                        /*
                         * Checks if the current cell is alive or not.
                         *
                         * If so, if the cell has less than 2, or more than 3 alive neighbors,
                         * the cell will be killed.
                         *
                         * If not, if the cell has 3 alive neighbors, the cell will be brought to life.
                         */
                        if (oldState.Contains(cell.X, cell.Y))
                        {
                            if (neighbors < 2 || neighbors > 3)
                            {
                                Remove(cell);
                            }
                        }
                        else
                        {
                            if (neighbors == 3)
                            {
                                Add(cell);
                            }
                        }
                    }
                }

                _stopwatch.Stop();
            }