来自递归函数的StackOverflowError

时间:2016-04-10 10:40:16

标签: java recursion stack-overflow

我试图用java中的游戏Pengo制造迷宫发生器,但我遇到了一些麻烦。

我有一个班级" Cellule"这意味着英语中的Cell(对不起,我是法语,我没想过要问你们)

myrealpath()

然后我有一个班级" Plateau"这意味着包含我的单元格选项卡的板以及初始化和生成迷宫的所有功能

public class Cellule{
    int x;
    int y;
    int val;

    public Cellule(int x,int y,int val)
    {
        this.x=x;
        this.y=y;
        this.val=val;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

}

导致问题的递归函数是public class Plateau { Cellule[][] tab; int m,n; //doivent etre des nombres impaires/ must be odd numbers Cellule curPosVer; Cellule curPosGen; int verifX; int verifY; public Plateau(int m, int n){ if(m%2==1 && n%2==1){ this.tab = new Cellule[m][n]; this.m=m; this.n=n; this.curPosGen=new Cellule(m-1,0,9); this.curPosVer=new Cellule(m-1,0,9); this.verifX=0; this.verifY=0; } else{ System.out.println("Les nombre m et n doivent etre des nombres impaires"); } } void initPlateau(){ for(int i=0;i<this.m;i++){ for(int j=0;j<this.n;j++){ this.tab[i][j]=new Cellule(i,j,1); } } this.tab[m-1][0].setVal(0); } void affichePlateau(){ System.out.println("Test"); for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ System.out.print(this.tab[i][j].getVal()+" "); } System.out.println(""); } } /*void pathFinder(){ //cherche un chemin à partir de curCellVer if(this.tab[this.curPosVer.getX()-2][this.curPosVer.getY()].getVal()==0){ if(this.tab[this.curPosVer.getX()][this.curPosVer.getY()+2].getVal()==0){ } } }*/ void pathGen(){ //genere un chemin à partir de curPosGen/ generate a path from curPosGen int dir; while((this.curPosGen.getX()+2<=m-1)&&(this.tab[this.curPosGen.getX()+2][this.curPosGen.getY()].getVal()==1) || (this.curPosGen.getX()-2>=0)&&(this.tab[this.curPosGen.getX()-2][this.curPosGen.getY()].getVal()==1) || (this.curPosGen.getY()+2<=n-1)&&(this.tab[this.curPosGen.getX()][this.curPosGen.getY()+2].getVal()==1) || (this.curPosGen.getY()-2>=0)&&(this.tab[this.curPosGen.getX()][this.curPosGen.getY()-2].getVal()==1)){ dir=(int)(Math.random()*4); switch(dir){ case 0://Nord { if(this.curPosGen.getX()-2>=0){ if(this.tab[this.curPosGen.getX()-2][this.curPosGen.getY()].getVal()==1){ this.tab[this.curPosGen.getX()-1][this.curPosGen.getY()].setVal(0); this.tab[this.curPosGen.getX()-2][this.curPosGen.getY()].setVal(0); this.curPosGen.setX(this.curPosGen.getX()-2); } } } break; case 1://Est { if(this.curPosGen.getY()+2<=this.n){ if(this.tab[this.curPosGen.getX()][this.curPosGen.getY()+2].getVal()==1){ this.tab[this.curPosGen.getX()][this.curPosGen.getY()+1].setVal(0); this.tab[this.curPosGen.getX()][this.curPosGen.getY()+2].setVal(0); this.curPosGen.setY(this.curPosGen.getY()+2); } } } break; case 2://Sud { if(this.curPosGen.getX()+2<=this.m-1){ if(this.tab[this.curPosGen.getX()+2][this.curPosGen.getY()].getVal()==1){ this.tab[this.curPosGen.getX()+1][this.curPosGen.getY()].setVal(0); this.tab[this.curPosGen.getX()+2][this.curPosGen.getY()].setVal(0); this.curPosGen.setX(this.curPosGen.getX()+2); } } } break; case 3://Ouest { if(this.curPosGen.getY()-2>0){ if(this.tab[this.curPosGen.getX()][this.curPosGen.getY()-2].getVal()==1){ this.tab[this.curPosGen.getX()][this.curPosGen.getY()-1].setVal(0); this.tab[this.curPosGen.getX()][this.curPosGen.getY()-2].setVal(0); this.curPosGen.setY(this.curPosGen.getY()-2); } } } break; } this.pathGen(); } } }

最后,我的主要看起来像这样

pathGen()

它给我这样的事情大约1/10次

java errors

如果你能帮助我,那真是太棒了!非常感谢

2 个答案:

答案 0 :(得分:0)

我认为我没有得到您的代码的完整含义,但正如异常所述,您通过递归调用pathGen超出了调用堆栈的容量。

为什么在这种情况下需要递归?您已经定义了以while子句

结尾的条件
 while((this.curPosGen.getX()+2<=m-1)&&(this.tab[this.curPosGen.getX()+2][this.curPosGen.getY()].getVal()==1)  ||
      (this.curPosGen.getX()-2>=0)&&(this.tab[this.curPosGen.getX()-2][this.curPosGen.getY()].getVal()==1)  ||
      (this.curPosGen.getY()+2<=n-1)&&(this.tab[this.curPosGen.getX()][this.curPosGen.getY()+2].getVal()==1)  ||
      (this.curPosGen.getY()-2>=0)&&(this.tab[this.curPosGen.getX()][this.curPosGen.getY()-2].getVal()==1)){

继续迭代,而不调用另一个pathGen()。

答案 1 :(得分:0)

您收到stackoverflow,因为方法void pathGen()未正确开发......

让我们来看看:

void pathGen(){ //genere un chemin à partir de curPosGen/ generate a path from curPosGen
    int dir;
while (...) {
    dir = (int) (Math.random() * 4);
    switch (dir) {
    case 0:// Nord
        ...
        break;
    case 1:// Est
        .dir..
        break;
    case 2:// Sud
        ...
        break;
    case 3:// Ouest
        ...
        break;
    }
    this.pathGen();
}

如您所见,在方法结束时您有: this.pathGen();正在一次又一次地对你的方法进行深度递归调用,所以应用程序进入该方法并进行某种无限递归,抛出异常

删除它并重新设计让你进入while循环的逻辑