コード例 #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();
            }
コード例 #2
0
ファイル: Wilsons.cs プロジェクト: RonaldHordijk/Mazes
        public void Build(Grid grid)
        {
            if (grid == null)
            {
                return;
            }

            var unvisited = new CellCollection();

            foreach (Cell cell in grid.GetCells())
            {
                if (cell != null)
                {
                    unvisited.Add(cell);
                }
            }

            unvisited.Remove(unvisited.Sample());

            while (!unvisited.IsEmpty())
            {
                Cell cell = unvisited.Sample();
                var  path = new CellCollection();
                path.Add(cell);

                while (unvisited.Contains(cell))
                {
                    cell = cell.Neighbors.Sample();

                    int position = path.IndexOf(cell);
                    if (position >= 0)
                    {
                        path.RemoveRange(position + 1, path.Count - position - 1);
                    }
                    else
                    {
                        path.Add(cell);
                    }
                }

                for (int i = 0; i < path.Count - 1; i++)
                {
                    path[i].Link(path[i + 1]);
                    unvisited.Remove(path[i]);
                }
            }
        }
コード例 #3
0
        private static void Print(this CellCollection state, Cell topLeft, Cell size)
        {
            const char alive = '#';
            const char dead  = ' ';

            Console.WriteLine("Generation {0} [({1}, {2}), ({3}, {4})] ({5} live cells)",
                              state.Generation,
                              topLeft.X, topLeft.Y,
                              topLeft.X + size.X, topLeft.Y + size.Y,
                              state.Count);

            for (int i = topLeft.Y; i <= topLeft.Y + size.Y; i++)
            {
                for (int o = topLeft.X; o <= topLeft.X + size.X; o++)
                {
                    Console.Write(state.Contains(o, i) ? alive : dead);
                }
                Console.WriteLine();
            }

            Console.WriteLine("Generated in {0} ms.", state.MillisecondsToGenerate);
        }