2017-10-25 20 views
0

シンプルな迷路ジェネレータを作成しようとしていましたが、壁を開いたときにスタックを使用してスタックを使用すると、私が新しい方向に新しい壁を開くことができなくなるまで、私はそこにある別の壁を開くことができます。java.lang、OutOfMemoryError:スタックを使用しているときのJavaヒープスペース

私は最大のヒープサイズを増やそうとしましたが、それは助けにはならなかったのです。 私はそれをポップアウトすると、オブジェクトへの参照を削除する方法はありますか?ポップはそれをやっていないようだから。

問題は、この機能である:ここ

private void Build(){ 
    int sX,sY; 
    int DIR; 
    int nextCell[]; 
    sX= rand.nextInt(w-1) + 0; 
    sY= rand.nextInt(h-1) + 0; 
    Stack<Cell> c=new Stack<Cell>(); 
    c.push(Tiles[sX][sY]); 

    while(!c.isEmpty()){ 
     DIR=chooseDIR(c.peek().x,c.peek().y); 
     if(DIR!=-1){ 
      Tiles[c.peek().x][c.peek().y].openWall(DIR); 
      nextCell=openNear(DIR,c.peek().x,c.peek().y); 
      c.push(Tiles[nextCell[0]][nextCell[1]]); 
     }else c.pop(); 
    } 
} 

コードは次のとおりです。

public class MazeGenerator { 

private int w; 
private int h; 
private Cell Tiles[][]; 

private Random rand = new Random(); 

MazeGenerator(int w,int h){ 
    this.w=w; 
    this.h=h; 
    Tiles=new Cell[w][h]; 
    for(int i=0;i<w;i++){ 
     for(int j=0;j<h;j++){ 
      this.Tiles[i][j]=new Cell(i,j); 
     } 
    } 
    this.Build(); 
} 
private void Build(){ 
    int sX,sY; 
    int DIR; 
    int nextCell[]; 
    sX= rand.nextInt(w-1) + 0; 
    sY= rand.nextInt(h-1) + 0; 
    Stack<Cell> c=new Stack<Cell>(); 
    c.push(Tiles[sX][sY]); 

    while(!c.isEmpty()){ 
     DIR=chooseDIR(c.peek().x,c.peek().y); 
     if(DIR!=-1){ 
      Tiles[c.peek().x][c.peek().y].openWall(DIR); 
      nextCell=openNear(DIR,c.peek().x,c.peek().y); 
      c.push(Tiles[nextCell[0]][nextCell[1]]); 
     }else c.pop(); 
    } 
} 
private int[] openNear(int DIR,int x,int y){ 
    int nextCell[]={0,0}; 
    switch(DIR){ 
    case 0: 
     Tiles[x][y+1].SouthWall=0;  
     nextCell[0]=x; 
     nextCell[1]=y-1; 
     break; 
    case 1: 
     Tiles[x][y-1].NorthWall=0; 
     nextCell[0]=x; 
     nextCell[1]=y+1; 
     break; 
    case 2: 
     Tiles[x-1][y].WestWall=0; 
     nextCell[0]=x+1; 
     nextCell[1]=y; 
     break; 
    case 3: 
     Tiles[x+1][y].EastWall=0; 
     nextCell[0]=x-1; 
     nextCell[1]=y; 
     break; 
     } 
    return nextCell; 

    } 
private int chooseDIR(int x,int y){ 
    int opend=0; 
    int rndWall=0; 
    if(!((y-1>0&&Tiles[x][y-1].isClosed()) || (y+1<h-1&&Tiles[x][y+1].isClosed()) || (x+1<w-1&&Tiles[x+1][y].isClosed()) || (x-1>0&&Tiles[x-1][y].isClosed()))) 
{return -1;} 
    while(opend==0){ 

      rndWall = rand.nextInt(3) + 0; 

     switch(rndWall){ 
     case 0: 
      if(y-1>0&&Tiles[x][y-1].isClosed()){ 
       //Tiles[x][y].NorthWall=0; 
      opend=1; 
      } 
      break; 
     case 1: 
      if(y+1<h-1&&Tiles[x][y+1].isClosed()){ 
       // Tiles[x][y].SouthWall=0; 
      opend=1; 
      } 
      break; 
     case 2: 
      if(x+1<w-1&&Tiles[x+1][y].isClosed()){ 
       // Tiles[x][y].EastWall=0; 
      opend=1; 
      } 
      break; 
     case 3: 
      if(x-1>0&&Tiles[x-1][y].isClosed()){ 
       // Tiles[x][y].WestWall=0; 
      opend=1; 
      } 
      break; 

      } 

     } 
    return rndWall; 
} 
public void ShowMaze(){ 
    for (int i = 0; i < h; i++) { 

     for (int j = 0; j < w; j++) { 
      System.out.print((Tiles[j][i]).NorthWall == 1 ? "+---" : "+ "); 
     } 
     System.out.println("+"); 

     for (int j = 0; j < w; j++) { 
      System.out.print((Tiles[j][i]).WestWall == 1 ? "| " : " "); 
     } 
     if(Tiles[i][w-1].EastWall==1) 
     System.out.println("|"); 
    } 

    for (int j = 0; j < w; j++) { 
     if(Tiles[h-1][j].SouthWall==1) 
     System.out.print("+---"); 
    } 
    System.out.println("+"); 
} 
public static void main(String[] args) { 

    MazeGenerator MG = new MazeGenerator(10,10); 
    MG.ShowMaze(); 
} 

}

public class Cell { 

int x=0; 
int y=0; 
int NorthWall=1; 
int SouthWall=1; 
int EastWall=1; 
int WestWall=1; 


Cell(int x,int y){ 
    this.x=x; 
    this.y=y; 
} 
boolean isClosed(){ 
    if (this.EastWall*this.NorthWall*this.WestWall*this.SouthWall==1) 
       return true; 
    else return false; 
    } 
void openWall(int DIR){ 




    switch(DIR){ 
    case 0: 

     this.NorthWall=0; 


     break; 
    case 1: 

     this.SouthWall=0; 


     break; 
    case 2: 

     this.EastWall=0; 


     break; 
    case 3: 

     this.WestWall=0; 


     break; 
     } 



} 

}

答えて

0

私はそれが動作するようになった気にしませんもう助けの必要はありません。

関連する問題