コード例 #1
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]);
                }
            }
        }
コード例 #2
0
ファイル: BinaryTree.cs プロジェクト: RonaldHordijk/Mazes
        private static void ProcessCell(Cell cell)
        {
            var neighbors = new CellCollection();

            if (cell.North != null)
            {
                neighbors.Add(cell.North);
            }

            if (cell.East != null)
            {
                neighbors.Add(cell.East);
            }

            if (neighbors.IsEmpty())
            {
                return;
            }

            cell.Link(neighbors.Sample());
        }