user3793935
user3793935

Reputation: 489

My recursive method doesn't stop after it reaches the end of the method

I would like to generate some random coordinates, from a start location in the north(x=10,y=0), west(x=0,y=10), east(x=20,y=10) or south(x=10,y=20). (for a random dungeongenerator, just for a hopefully funny project ;^) )

private void generateDungeon(int x, int y, int groesse) {

    //System.out.println("groesse: " +groesse);
    int tempX = this.neueKoordinaten.nextInt(2);
    int tempY = this.neueKoordinaten.nextInt(2);

    if(tempX == 0 && tempY== 0) {
        this.generateDungeon(x, y, groesse);
    }
    if(tempY==1) {  
        y = y + (1 * this.yFaktor);
    }else if(tempX == 1) {
        x = x + (1 * this.xFaktor);
    }

    this.aktuelleKoordinaten = new Point(x,y);
    //System.out.println("aktuelleKoordinaten: " +this.aktuelleKoordinaten);
    this.koordinatenListe.add(this.aktuelleKoordinaten);

    groesse++;

    if(groesse <= 4) {
        System.out.println("???" + groesse);
        this.generateDungeon(x, y,groesse);
    }   
    System.out.println("test");
}

I thought, that after my "groesse" (size) reaches a certain point (4 in this case), my method will stop calling itself, but it doesn't.

For some reasons it keep jumping back to the if with tempY==1 after it reaches the end point of the method (the testoutput).
Sometimes for like 20 times, sometimes only once or twice.
I really don't understand this behaviour.

What am I doing wrong?

Upvotes: 0

Views: 136

Answers (2)

Deltharis
Deltharis

Reputation: 2373

If I'm right in thinking that this.neueKoordinaten.nextInt(2) generates a random int between 0 and 1... Than you have 25% to hit

if(tempX == 0 && tempY== 0) {
    this.generateDungeon(x, y, groesse);
}

snippet each time your method runs. It runs 4 or 5 times (depends on whether you start groesse with 0 or 1) when it doesn't hit that. If it does however, this line is your new point of start, it will run it those 4 or 5 times (probably less, depending on groesse value when that happens) before continuing with what it was doing, resulting in unpredictable amount of executions.

Judging by comments, what you did want was just to reroll again, not start the entire function again:

while(tempX == 0 && tempY == 0){
    tempX = this.neueKoordinaten.nextInt(2);
    tempY = this.neueKoordinaten.nextInt(2);
}

Or, if you prefer to restart again indicate, that this branch is finished:

if(tempX == 0 && tempY== 0) {
    this.generateDungeon(x, y, groesse);
    return;
}

Upvotes: 3

Jessica Bunyan
Jessica Bunyan

Reputation: 66

Hard to see the complete story from this snippet but it looks as though your first if statement is passing and you're calling generateDungeon without incrementing groesse. This would explain why it looks like the code is "jumping back" to the following line if(tempY==1) because that is the first line after the method call.

I'd suggest debugging through the method step by step, keeping track of groesse and also the values of tempx/tempy

Upvotes: 2

Related Questions