gabec94 0 Junior Poster in Training

So I'm practicing recursion and trying to write a 'floodfill' program. The program creates a picture array with a bounded region (one of the boundaries should be the edge of the array), prints the array, flood fills a certain cell a certain color, and then prints the array a second time to check the results.

I'm pretty sure that everything works, except the fact that each space isn't changed when it should be. Any help would be greatly appreciated, thanks!

package Recursion;
public class Flood_Fill {
    public static void main(String[] args) {
        int color1 = 0;
        int color2 = 1;
        int[][] picture = new int[10][10];
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                picture[i][j] = color1;
            }
        }
        //pre-entered code
        picture[2][5] = color2;
        picture[3][5] = color2;
        picture[4][5] = color2;
        picture[5][5] = color2;
        picture[6][5] = color2;
        picture[7][5] = color2;
        picture[2][1] = color2;
        picture[2][2] = color2;
        picture[2][3] = color2;
        picture[2][4] = color2;
        picture[2][5] = color2;
        picture[3][5] = color2;
        picture[4][5] = color2;
        picture[5][5] = color2;
        picture[6][5] = color2;
        picture[2][1] = color2;
        picture[3][1] = color2;
        picture[4][1] = color2;
        picture[5][1] = color2;
        picture[6][1] = color2;
        picture[7][1] = color2;
        picture[7][3] = color2;
        picture[7][2] = color2;
        printBoard(picture);
        
        //test flood fill
        floodFill(picture, 5, 3, color2);
    }
    public static void floodFill(int[][] picture, int row, int col, int newColor) {
        System.out.println("3");
        floodFill(picture, row, col, newColor, picture[row][col]);
    }
    
    private static void floodFill(int[][] picture, int row, int col, int newColor, int oldColor) {
        if (row > picture.length + 1 || row < 0 || col > picture.length + 1 || col < 0) {
            // testing code System.out.println("2");
            return;
        } 
        if (picture[row][col] == newColor) {
            // testing code System.out.println("1");
            return;
        }
        else {
            picture[row][col] = newColor;
            if (row + 1 < 10 || col + 1 < 10 || row - 1 < 10 || col - 1 < 10) {
                if (row + 1 >= 0 || col + 1 >= 0 || row - 1 >= 0 || col - 1 >= 0) {
                    // testing code System.out.println("3");
                    floodFill(picture, row + 1, col, newColor, oldColor);
                    row = row + 1;
                    floodFill(picture, row - 1, col, newColor, oldColor);
                    row = row - 1;
                    floodFill(picture, row, col - 1, newColor, oldColor);
                    col = col - 1;
                    floodFill(picture, row, col + 1, newColor, oldColor);
                    col = col + 1;
                }
            }
        }
    }

    private static void printBoard(int[][] picture) {
        for (int a = 0; a < 10; a++) {
            for (int b = 0; b < 10; b++) {
                if (picture[a][b] == 0){
                    System.out.print("[0]");
                } else {
                    System.out.print("[1]");
                }
            }
            System.out.println("");
        }
        System.out.println("");
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.