r/javahelp • • Dec 06 '25

need help to code recursive division in java to create maze

helloo

i am a student in first year of computer science, and for my semester project i have to create a 2d game with mazes. i have to write an algorithm that creates mazes using the recursive division, and i have written this :

int [][] createMaze(int width , int height , int difficulty){


    int[][] mazeToBe = new int[height][width];

    //remplir le maze
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            mazeToBe[y][x] = 0;
        }
    }

    int random = RandomGenerator.rng.nextInt(height);
    int random2 = RandomGenerator.rng.nextInt(width);

    for (int i = 0; i  <=random2; i++) {
        //faire la ligne
        mazeToBe[random][i] = 1;

        //faire un trou
        mazeToBe[random][random2] = 0;

        int [] randoms = new int [i];
        for (int j = 0; j <= width; j++){
            randoms[j] = RandomGenerator.rng.nextInt(j);
            mazeToBe[random][j] = 1;
            mazeToBe[random][randoms[j]] = 0;
        }
    }

    printMaze(mazeToBe, new DiscreteCoordinates(0,0), new DiscreteCoordinates(width, height));
    return mazeToBe;
}

now, i am pretty sure i did something wrong, but i can't say where. can someone help me ?

1 Upvotes

10 comments sorted by

View all comments

1

u/aqua_regis Dec 06 '25

Where is the recursion in your recursive division algorithm?

Do you even understand what recursion - or the recursive division algorithm mean?

1

u/uhmmmpp Dec 06 '25

hi, im gonna be honest, i understand how the recursive division works but i really have difficulty to put it into code :(

1

u/aqua_regis Dec 06 '25

Generally, recursion means that the recursive method calls itself with a subset of the data until a base case is reached.

You have no recursive calls.

In your case, it would mean that you create a method that at a random position in the rectangle passed in, it would place a wall - either horizontally, or vertically with exactly one opening. Then, you'd split the coordinates according to the wall position into two parts and each part calls the same wall creating method again with the subset (respective rectangle) of the data.

The base case is when the resulting rectangle is exactly 1 x 1 units. There, you don't make a recursive call anymore.