-2

I am making a game like Lumines Remastered (https://www.youtube.com/watch?v=8aJqNGjLtaY&t=358s) and I was pretty much successful in it. But, I got stuck in one place in clearing the pieces.

I have a 2d Array grid of 16x10 and each individual pieces are stored in it.

I was successful in finding all the matching pieces using the flood fill algorithm and storing every same color matching pieces in a separate list. Now, the condition is I only want to delete the pieces that are in combination of squares with others and not the individual ones.

For example:- Like This

Like this. 1 is in square combination and I only want to delete them.

But, Second Image

2 in this image is also a valid square (so, 1 & 2 combined should get deleted). I quite can't seem to figure out how to check for square combination. Any suggestions and ideas would be great.

2 Answers2

0

You could probably loop over all of the cells, and check whether they are the bottom corner of a 2x2 square of the same colour and if so, mark all the cells in that square for deletion. After the loop finishes, you delete/empty all the marked cells.

This will also work for larger groups of cells, since they are made up of (overlapping) 2x2 squares.

While looping, make sure you stop short of the last row and column in the grid, to avoid going out of bounds when trying to check the next row/column.

Vera
  • 644
  • 5
  • 10
0

I ended up taking @Vera suggestion and looped over the entire grid and checking if the blocks adjacent to the current block are of the same color and forming a square in 2x2 config. It works for larger groups of cells.

for (int j = 0; j < height; j++)
    {
        for (int i = 0; i < width; i++)
        {
            
            if (grid[i, j].tag == grid[i + 1, j].tag && grid[i, j].tag == grid[i, j + 1].tag && grid[i, j].tag == grid[i + 1, j + 1].tag)
            {
                Debug.Log("It's a square piece in 2x2 config");
            }

        }
    }