来自递归函数的 StackOverflowError

StackOverflowError from recursive function

我正尝试在 java 中使用 Pengo 游戏制作迷宫生成器,但遇到了一些麻烦。

我有一个 class "Cellule" 的英文意思是 Cell(对不起,我是法国人,我没想过要问你们问题)

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;
    }

}

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

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()

最后我的主图是这样的

public class TestMain {
public static void main(String args[]){
    Plateau p=new Plateau(13,15);
    p.initPlateau();
    p.affichePlateau();
    p.pathGen();
    p.affichePlateau();
}
}

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

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

我认为我没有理解您的代码的完整含义,但如异常所述,您递归调用 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()。

您收到 Whosebug 因为方法 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 循环中的逻辑