public static IEnumerable <GridPoint2> GenerateMazeWalls <TCell>(IGrid <GridPoint2, TCell> grid) { var walls = grid.CloneStructure <bool>(); //true indicates passable foreach (var point in walls.Points) { walls[point] = point.GetColor(2, 2, 2) == 0; //Debug.Log(point); } var wallPoints = walls .Where(pair => pair.cell) .Select(pair => pair.point); foreach (var point in wallPoints) { yield return(point); } var wallList = new StructList <GridPoint2>(); var newMaizePoint = grid.Points.Where(p => p.GetColor(2, 2, 2) == 3).RandomItem(); var inMaze = new StructList <GridPoint2> { newMaizePoint }; var edges = newMaizePoint .GetVectorNeighbors(RectPoint.OrthogonalDirections) .In(grid); wallList.AddRange(edges); while (wallList.Any()) { var randomWall = wallList.RandomItem(); var faces = GetEdgeFaces(randomWall).Where(grid.Contains); //At least one of the two faces must be in the maze if (faces.Any(point => !inMaze.Contains(point))) { newMaizePoint = faces.First(point => !inMaze.Contains(point)); inMaze.Add(newMaizePoint); walls[randomWall] = true; yield return(randomWall); // Add all edges that are not passages edges = newMaizePoint .GetVectorNeighbors(RectPoint.OrthogonalDirections) .In(grid) .Where(edge => !(walls[edge])); wallList.AddRange(edges); } else { wallList.Remove(randomWall); } } }