Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int buriedBlocks(Grid temp)
- {
- List<Point> nullblocks = new List<Point>();
- List<Point> usedPositions = new List<Point>();
- Point Neighbour = new Point(0,0);
- int buriedCount = 0;
- //Find all starting points to flood fill from.
- for (int x = 0; x < temp.Width; x++)
- {
- if (temp.GameGrid[x, 0] == null)
- {
- nullblocks.Add(new Point(x, 0));
- usedPositions.Add(new Point(x,0));
- }
- }
- //For each null block we found check all adjacent positions for more null blocks.
- //Then add them to the list of blocks to check unless we already added that block.
- for (int i = 0; i < nullblocks.Count; i++)
- {
- Neighbour.X = nullblocks[i].X - 1;
- Neighbour.Y = nullblocks[i].Y;
- //Left neighbour is inside bounds.
- if (Neighbour.X >= 0 && Neighbour.X < temp.Width)
- {
- //If the block is null.
- if (temp.GameGrid[Neighbour.X, Neighbour.Y] == null)
- {
- //If we haven't already added this block.
- if (!usedPositions.Contains(nullblocks[i]))
- {
- nullblocks.Add(new Point((nullblocks[i].X) - 1, nullblocks[i].Y));
- usedPositions.Add(new Point((nullblocks[i].X) - 1, nullblocks[i].Y));
- }
- }
- }
- Neighbour.X = nullblocks[i].X + 1;
- Neighbour.Y = nullblocks[i].Y;
- //Right neighbour is inside bounds.
- if (Neighbour.X >= 0 && Neighbour.X < temp.Width)
- {
- //If the block is null.
- if (temp.GameGrid[Neighbour.X, Neighbour.Y] == null)
- {
- //If we haven't already added this block.
- if (!usedPositions.Contains(nullblocks[i]))
- {
- nullblocks.Add(new Point((nullblocks[i].X) + 1, nullblocks[i].Y));
- usedPositions.Add(new Point((nullblocks[i].X) + 1, nullblocks[i].Y));
- }
- }
- }
- Neighbour.X = nullblocks[i].X;
- Neighbour.Y = nullblocks[i].Y - 1;
- //Top neighbour is inside bounds.
- if (Neighbour.Y >= 0 && Neighbour.Y < temp.Height)
- {
- //If the block is null.
- if (temp.GameGrid[Neighbour.X, Neighbour.Y] == null)
- {
- //If we haven't already added this block.
- if (!usedPositions.Contains(nullblocks[i]))
- {
- nullblocks.Add(new Point(nullblocks[i].X, (nullblocks[i].Y) - 1));
- usedPositions.Add(new Point(nullblocks[i].X, (nullblocks[i].Y) - 1));
- }
- }
- }
- Neighbour.X = nullblocks[i].X;
- Neighbour.Y = nullblocks[i].Y + 1;
- //Bottom neighbour is inside bounds.
- if (Neighbour.Y >= 0 && Neighbour.Y < temp.Height)
- {
- //If the block is null.
- if (temp.GameGrid[Neighbour.X, Neighbour.Y] == null)
- {
- //If we haven't already added this block.
- if (!usedPositions.Contains(nullblocks[i]))
- {
- nullblocks.Add(new Point(nullblocks[i].X, (nullblocks[i].Y) + 1));
- usedPositions.Add(new Point(nullblocks[i].X, (nullblocks[i].Y) + 1));
- }
- }
- }
- }
- for (int x = 0; x < temp.Width; x++)
- {
- for (int y = 0; y < temp.Height; y++)
- {
- for (int k = 0; k < nullblocks.Count; k++)
- {
- if (temp.GameGrid[x, y] == null && !nullblocks.Contains(new Point(x, y)))
- {
- buriedCount++;
- }
- }
- }
- }
- return buriedCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement