Пример #1
0
        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]);
                }
            }
        }