Ejemplo n.º 1
0
        private static void ProcessCell(CellCollection run, Cell cell)
        {
            if ((run == null) || (cell == null))
            {
                return;
            }

            run.Add(cell);

            bool atEasterBoundary   = cell.East == null;
            bool atNorthernBoundary = cell.North == null;

            bool shouldCloseOut = atEasterBoundary ||
                                  (!atNorthernBoundary && rnd.Next(2) == 0);

            if (shouldCloseOut)
            {
                Cell member = run.Sample();
                if (member.North != null)
                {
                    member.Link(member.North);
                }

                run.Clear();
            }
            else
            {
                cell.Link(cell.East);
            }
        }
Ejemplo n.º 2
0
        public void Build(Grid grid)
        {
            if (grid == null)
            {
                return;
            }

            int  unvisitedCount = grid.UsedCells() - 1;
            int  switchPoint    = 2 * grid.UsedCells() / 3;
            Cell current        = grid.RandomCell();

            // AldousBorder for first third
            while (unvisitedCount > switchPoint)
            {
                var  neighbors = current.Neighbors;
                Cell neighbour = neighbors.Sample();

                if (neighbour.Links.IsEmpty())
                {
                    current.Link(neighbour);
                    unvisitedCount--;
                }

                current = neighbour;
            }

            // and finish with Wilson
            var unvisited = new CellCollection();

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

            while (!unvisited.IsEmpty())
            {
                Cell        cell = unvisited.Sample();
                List <Cell> path = new List <Cell>();
                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]);
                }
            }
        }