public Grid<Cell> On(Grid<Cell> grid) { var current = grid.RandomCell(); while (current != null) { var unvisitedNeighbours = current.Neighbours().Where(n => n.Links.Count == 0).ToList(); if (unvisitedNeighbours.Any()) { var neighbour = unvisitedNeighbours[rand.Next(unvisitedNeighbours.Count())]; current.Link(neighbour); current = neighbour; } else { current = null; foreach (var cell in grid.Cells()) { var visitedNeighbours = cell.Neighbours().Where(n => n.Links.Any()).ToList(); if (cell.Links.Count == 0 && visitedNeighbours.Any()) { current = cell; var neighbour = visitedNeighbours[rand.Next(visitedNeighbours.Count())]; current.Link(neighbour); break; } } } } return grid; }
public Grid<Cell> On(Grid<Cell> grid) { var unvisited = new List<Cell>(); unvisited.AddRange(grid.Cells()); var first = unvisited[rand.Next(unvisited.Count)]; unvisited.Remove(first); Debug.Print($"{first.Row},{first.Column}"); while (unvisited.Count > 0) { var cell = unvisited[rand.Next(unvisited.Count)]; Debug.Print($"cell:{cell.Row},{cell.Column}"); var path = new List<Cell>() { cell }; while (unvisited.Contains(cell)) { cell = cell.Neighbours()[rand.Next(cell.Neighbours().Count)]; Debug.Print($"neighbour cell:{cell.Row},{cell.Column}"); var position = path.IndexOf(cell); if (position > -1) { //add one to position as zero based. path = path.GetRange(0, position + 1); } else { path.Add(cell); } } for (int i = 0; i <= path.Count - 2; i++) { path[i].Link(path[i + 1]); unvisited.Remove(path[i]); } } return grid; }