Java returning value after recursion (bucket fill tool)

391 views Asked by At

I made a program that generates tiles/pixels. Within this program is a function that is used to fill in gaps and holes, and here is how I intended it to work:

A method is given an integer limit. This method then goes through paint bucket-like recursion where it repaints its tiles to a new tile (integer type 3) and stops when it encounters solid tiles.

As soon as the method fills in more tiles than the limit, the method quits and returns a false statement, telling that the region has a larger area than the given limit. Otherwise, if the recursive method stops all together and has filled in all of the spaces within an enclosed region without breaking this limit, it returns true.

The issue here is that I don't know how to make the program "wait" for the recursive methods to stop working before returning a value. I tried to make a "waiting" variable that ran through a while loop before returning, but that didn't turn out too well.

Here is the source code:

// Method called in other parts of the program. 
private boolean checkNum(int x, int y, int limit){ 

        int num = 0;
        checkNum(x,y,num,limit,count); // I think the recursive methods following this one are carried out after the statement on the next line. That is the cause of the issue.
                return tempBoolean;
}


//Recursive method called be previous one and itself.
private void checkNum(int x, int y, int num, int limit,int count){
        tempBoolean = false;


        if ((grid[x][y] == 3) || (grid[x][y] == 1)) {
            return;
        }




        grid[x][y] = 3;
        System.out.println(num);
        num++;

        if (num > limit) {
            tempBoolean = false; // This system for setting an external variable and then returning it in a different method is probably not the right way to do it.
            return;
        }


        checkNum(x + 1,y,num,limit,count);
        checkNum(x - 1,y,num,limit,count);
        checkNum(x,y + 1,num,limit,count);
        checkNum(x,y - 1,num,limit,count);

}
1

There are 1 answers

0
Codebender On BEST ANSWER

There are a few problems with your code,

  1. I don't think you need recursion at all for this. A loop would do.
  2. the tempBoolean variable is always set to false;
  3. Your num variable falls back to original value after every recursive call, hence the recursion will process more pixels than the limit.
  4. If you think your return tempBoolean; statements executes before the recursive call is finished, NO, that doesn't happen.

To fix it using recursion itself, you something like this instead,

// Method called in other parts of the program. 
private boolean checkNum(int x, int y, int limit){ 

        int num = 0;
        num = checkNum(x,y,num,limit,count); // I think the recursive methods following this one are carried out after the statement on the next line. That is the cause of the issue.

        return (num <= limit);
}


//Recursive method called be previous one and itself.
private int checkNum(int x, int y, int num, int limit,int count){

        if (num > limit) {
            return num;
        }

        if ((grid[x][y] == 3) || (grid[x][y] == 1)) {
            return num;
        }




        grid[x][y] = 3;
        System.out.println(num);
        num++;



        num = checkNum(x + 1,y,num,limit,count);
        num = checkNum(x - 1,y,num,limit,count);
        num = checkNum(x,y + 1,num,limit,count);
        num = checkNum(x,y - 1,num,limit,count);

        return num;
}