public IGrid<Cell> On(IGrid<Cell> grid, Cell startAt = null) { if (startAt == null) { startAt = grid.RandomCell(); } var stack = new Stack<Cell>(); stack.Push(startAt); while (stack.Count != 0) { var current = stack.Peek(); var neighbours = current.Neighbours().Where(n => n.Links.Count == 0).ToList(); if (neighbours.Count == 0) { stack.Pop(); } else { var neighbour = neighbours[rand.Next(neighbours.Count)]; current.Link(neighbour); stack.Push(neighbour); } } return grid; }