Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1.         public static int buriedBlocks(Grid temp)
  2.         {
  3.             List<Point> nullblocks = new List<Point>();
  4.             List<Point> usedPositions = new List<Point>();
  5.            
  6.             Point Neighbour = new Point(0,0);
  7.  
  8.             int buriedCount = 0;
  9.  
  10.             //Find all starting points to flood fill from.
  11.             for (int x = 0; x < temp.Width; x++)
  12.             {
  13.                 if (temp.GameGrid[x, 0] == null)
  14.                 {
  15.                     nullblocks.Add(new Point(x, 0));
  16.                     usedPositions.Add(new Point(x,0));
  17.                 }
  18.             }
  19.  
  20.             //For each null block we found check all adjacent positions for more null blocks.
  21.             //Then add them to the list of blocks to check unless we already added that block.
  22.             for (int i = 0; i < nullblocks.Count; i++)
  23.             {                
  24.                     Neighbour.X = nullblocks[i].X - 1;
  25.                     Neighbour.Y = nullblocks[i].Y;
  26.                     //Left neighbour is inside bounds.
  27.                     if (Neighbour.X >= 0 && Neighbour.X < temp.Width)
  28.                     {
  29.                         //If the block is null.
  30.                         if (temp.GameGrid[Neighbour.X, Neighbour.Y] == null)
  31.                         {
  32.                             //If we haven't already added this block.
  33.                             if (!usedPositions.Contains(nullblocks[i]))
  34.                             {
  35.                                 nullblocks.Add(new Point((nullblocks[i].X) - 1, nullblocks[i].Y));
  36.                                 usedPositions.Add(new Point((nullblocks[i].X) - 1, nullblocks[i].Y));
  37.                             }
  38.                         }                      
  39.                     }
  40.                     Neighbour.X = nullblocks[i].X + 1;
  41.                     Neighbour.Y = nullblocks[i].Y;
  42.                     //Right neighbour is inside bounds.
  43.                     if (Neighbour.X >= 0 && Neighbour.X < temp.Width)
  44.                     {
  45.                         //If the block is null.
  46.                         if (temp.GameGrid[Neighbour.X, Neighbour.Y] == null)
  47.                         {
  48.                             //If we haven't already added this block.
  49.                             if (!usedPositions.Contains(nullblocks[i]))
  50.                             {
  51.                                 nullblocks.Add(new Point((nullblocks[i].X) + 1, nullblocks[i].Y));
  52.                                 usedPositions.Add(new Point((nullblocks[i].X) + 1, nullblocks[i].Y));
  53.                             }
  54.                         }                      
  55.                     }
  56.                     Neighbour.X = nullblocks[i].X;
  57.                     Neighbour.Y = nullblocks[i].Y - 1;
  58.                     //Top neighbour is inside bounds.
  59.                     if (Neighbour.Y >= 0 && Neighbour.Y < temp.Height)
  60.                     {
  61.                         //If the block is null.
  62.                         if (temp.GameGrid[Neighbour.X, Neighbour.Y] == null)
  63.                         {
  64.                             //If we haven't already added this block.
  65.                             if (!usedPositions.Contains(nullblocks[i]))
  66.                             {
  67.                                 nullblocks.Add(new Point(nullblocks[i].X, (nullblocks[i].Y) - 1));
  68.                                 usedPositions.Add(new Point(nullblocks[i].X, (nullblocks[i].Y) - 1));
  69.                             }
  70.                         }                        
  71.                     }
  72.                     Neighbour.X = nullblocks[i].X;
  73.                     Neighbour.Y = nullblocks[i].Y + 1;
  74.                     //Bottom neighbour is inside bounds.
  75.                     if (Neighbour.Y >= 0 && Neighbour.Y < temp.Height)
  76.                     {
  77.                         //If the block is null.
  78.                         if (temp.GameGrid[Neighbour.X, Neighbour.Y] == null)
  79.                         {
  80.                             //If we haven't already added this block.
  81.                             if (!usedPositions.Contains(nullblocks[i]))
  82.                             {
  83.                                 nullblocks.Add(new Point(nullblocks[i].X, (nullblocks[i].Y) + 1));
  84.                                 usedPositions.Add(new Point(nullblocks[i].X, (nullblocks[i].Y) + 1));
  85.                             }
  86.                         }                      
  87.                     }
  88.                 }            
  89.                
  90.             for (int x = 0; x < temp.Width; x++)
  91.             {
  92.                 for (int y = 0; y < temp.Height; y++)
  93.                 {
  94.                     for (int k = 0; k < nullblocks.Count; k++)
  95.                     {
  96.                         if (temp.GameGrid[x, y] == null && !nullblocks.Contains(new Point(x, y)))
  97.                         {
  98.                             buriedCount++;
  99.                         }
  100.                     }
  101.  
  102.                 }
  103.             }
  104.  
  105.             return buriedCount;
  106.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement