1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void Color_Grid::paintRegion(int row, int col, char oldColor, char newColor){
//make sure everything stays in bounds
if(row < 0 || row >= rows){ return; }//go back if out of bounds because there is nothing to change anyway
if(col < 0 || col >= cols){ return; }//go back if out of bounds because there is nothing to change anyway
//Base Cases
if(grid[row][col] != oldColor){ return; }//if the character is not the old color, there is nothing to do
//if the character IS the old color, change it to the new color
grid[row][col] = newColor;
return;//POSSIBLE PROBLEM RETURN STATEMENT!!
//Check up, down, left, and right from current location
paintRegion(row-1,col,oldColor,newColor);//Check above location
paintRegion(row+1,col,oldColor,newColor);//Check below location
paintRegion(row,col-1,oldColor,newColor);//Check left of location
paintRegion(row,col+1,oldColor,newColor);//Check right of location
}
|