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);
}
There are a few problems with your code,
tempBoolean
variable is always set tofalse;
num
variable falls back to original value after every recursive call, hence the recursion will process more pixels than thelimit
.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,