Search code examples
cstackflood-fill

Flood Fill and Stack Implementation Not working


So I have two structs that work together. This is a game that works like a bubble pop game.

When a balloon pops in the grid I want to use flood fill to remove the balloons an then have it using a stack so I can undo the process later.


Solution

  • Used a pre-sized stack array to keep things simple. When you want your data back, just call pop_balloon until the stack is empty (use the function).

    The following code is untested and pulled out of thin air, but it should show you want you need:

    
    Balloon balloonStack[MAX_NUM_BALLOONS_POSSBILE];
    int balloonStackIndex = 0;
    
    bool balloonStackIsEmpty()
    {
      return balloonStackIndex == 0;
    }
    
    void balloonPush(Balloon balloon)
    {
      assert(balloonStackIndex < MAX_NUM_BALLOONS_POSSBILE);
      balloonStack[balloonStackIndex++] = balloon;
    }
    
    Balloon balloonPop()
    {
      assert(balloonStackIndex > 0);
      return balloonStack[balloonStackIndex--]
    }
    
    int balloon_pop(BBoardPtr b, int r, int c) {
    
        if(b->board[r][c].color != None /*&& r >= 0 && c >= 0 && r rows && c cols*/) {
            return 0;
        }
    
        balloonPush(b->board[r][c]);
        b->board[r][c].color = None;
        b->board[r][c].is_popped = 1;
        //...