/// <summary> /// Links a specific node to a specified neighbor if it exists. /// </summary> private static void LinkNode(Cell cell, int xOffset, int yOffset, LocationType locationName, int height, int width) { int newX = cell.X + xOffset; int newY = cell.Y + yOffset; if (newX < 0 || newX >= width || newY < 0 || newY >= height) { return; } var link = MapCells.FirstOrDefault(c => c.X == newX && c.Y == newY); if (link == null) { return; } switch (locationName) { case LocationType.AboveLeft: cell.CellAboveLeft = link; break; case LocationType.Above: cell.CellAbove = link; break; case LocationType.AboveRight: cell.CellAboveRight = link; break; case LocationType.Left: cell.CellLeft = link; break; case LocationType.Right: cell.CellRight = link; break; case LocationType.BelowLeft: cell.CellBelowLeft = link; break; case LocationType.Below: cell.CellBelow = link; break; case LocationType.BelowRight: cell.CellBelowRight = link; break; } }
/// <summary> /// Recursive searching of neighbors for an area. /// </summary> private static void FloodfFill(Cell start, ref HashSet<Cell> visited, int index) { start.AreaIndex = index; visited.Add(start); var neighbors = start.Neighbors.Where(n => n.AreaIndex == 0).ToList(); HashSet<Cell> set = visited; neighbors.RemoveAll(set.Contains); foreach (Cell neighbor in neighbors) { if (!set.Contains(neighbor)) { FloodfFill(neighbor, ref visited, index); } } }