0

I have been looking over this flood fill implementation for some time now and keep running into the dreaded stack overflow. I am dropping pieces randomly on a 12x10 grid and calling the checkMatches method after each random piece drop to check for groups of three or more, hence the flood fill use.

EDIT: See comment

public void checkMatches(int x, int y, int type)
{
    if (x < 0 || x >= PIECES_WIDE || y < 0 || y >= PIECES_TALL || type == 0)
        return;
    if (grid[x][y].getType() != type)
        return;

    int checkL = x;
    while (checkL >= 0 && grid[checkL][y].getType() == type)
    {
        grid[checkL][y].setDestroy(true);
        numMatches++;
        checkL--;
    }
    checkL++;

    int checkR = x;
    while (checkR < PIECES_WIDE - 1 && grid[checkR][y].getType() == type)
    {
        grid[checkR][y].setDestroy(true);
        numMatches++;
        checkR++;
    }
    checkR--;

    for (int i = checkL; i <= checkR; i++)
    {
        if (y > 0 && grid[i][y - 1].getType() == type)
            checkMatches(i, y - 1, type);
        if (y < PIECES_TALL - 1 && grid[i][y + 1].getType() == type)
            checkMatches(i, y + 1, type);
    }

}

Then the relevant code to call the method and destroy the pieces if there have been three matched pieces:

    checkMatches(x, y, type);
    if (numMatches >= 3)
    {
        for (int i = 0; i < PIECES_WIDE; i++)
        {
            for (int j = 0; j < PIECES_TALL; j++)
            {
                if (grid[i][j].isDestroy())
                    destroyPiece(grid[i][j]);
            }
        }
    } else
    {
        numMatches = 0;
        for (int i = 0; i < PIECES_WIDE; i++)
        {
            for (int j = 0; j < PIECES_TALL; j++)
            {
                grid[i][j].setDestroy(false);
            }
        }
    }

My eyes and brain hurt. I know that the recursion is causing the overflow, but I also know that this implementation is possible in some form. Therefore I'm doing something wrong. Thanks in advance.

  • 1
    It's a little strange that you are using a loop to iterate on the X-axis but using recursion to iterate on the Y-axis. Any reason for this? – nitegazer2003 Sep 22 '13 at 06:52
  • your problem is that you are checking piece(y=5), then you go on left for piece(y=4), then you go lef... and you also go right to piece(y=5) and inspect it again. mark the already inspected pieces... – Absurd-Mind Sep 22 '13 at 06:53
  • I decided to try a non-recursive, non-floodfill approach and came up with one that seems to work. For anybody interested, check out http://stackoverflow.com/questions/18945890/detecting-groups-without-recursion-or-stacks-queues I appreciate all of the responses. – odaymichael Sep 22 '13 at 17:35

1 Answers1

0

You need to put a mark on the pieces that you have already found matching. Then you can make a loop to extend your matchings, until you notice that no more pieces have been marked. Then you can stop.

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64