2012-03-19 78 views
1
import java.util.*; 

public class MazeGenerator 
{ 
public void init() 
{ 
    String Maze[][] = new String [20][20]; 

    for (int i =0; i <20; i++) { 
     for (int j = 0; j < 20; j++) { 
      Maze[i][j] = "#"; 
     } 
    } 

    generate(Maze); 

    for (int i =0; i <20; i++) { 
     for (int j = 0; j < 20; j++) { 
      System.out.print(" " + Maze[i][j]); 
     } 
     System.out.println(""); 
    } 
} 

public void generate (String Maze[][]) 
{ 
    Stack <String> CellStack = new Stack<String>(); 
    int TotalCells = Maze.length * Maze.length; 
    int x = 10, y = 10; 

    String CurrentCell = Maze[x][y]; 
    Maze[x][y] = "-"; 
    CellStack.push(CurrentCell); 
    int VisitedCell = 1; 

    boolean EastT, WestT, NorthT, SouthT; 

    while(VisitedCell < TotalCells) 
    { 
     String EAST = Maze[x+1][y]; 
     String WEST = Maze[x-1][y]; 
     String NORTH = Maze[x][y+1]; 
     String SOUTH = Maze[x][y-1]; 

     if(EAST == "#") 
      EastT = true; 
     else 
      EastT = false; 

     if(WEST == "#") 
      WestT = true; 
     else 
      WestT = false; 

     if(NORTH == "#") 
      NorthT = true; 
     else 
      NorthT = false; 

     if(SOUTH == "#") 
      SouthT = true; 
     else 
      SouthT = false;  

     if(WestT == true || EastT == true || NorthT == true || SouthT == true) 
     { 
      double Random = (int) (Math.random() * 4) + 1; 

      switch ((int) Random) 
      { 
       case 1: 
       if(EastT == true){ 
        CurrentCell = EAST; 
        break; 
       } 
       else 
        break; 

       case 2: 
       if(WestT == true){ 
        CurrentCell = WEST; 
        break; 
       } 
       else 
        break; 

       case 3: 
       if(NorthT == true){ 
        CurrentCell = NORTH; 
        break; 
       } 
       else 
        break; 

       case 4: 
       if(SouthT == true){ 
        CurrentCell = SOUTH; 
        break; 
       } 
       else 
        break; 
      } 
      CurrentCell = "-"; 
      CellStack.push(CurrentCell); 
      VisitedCell++; 
     } 
     else 
     { 
      CurrentCell = CellStack.pop(); 
     } 
    } 
} 
} 

當我打印出來,我得到一個迷宮,其中有所有「#」的(在第一個位置有一個「 - 」),這意味着迷宮沒有創建正確的方式。但我不明白爲什麼它不起作用。我認爲它可能與CurrentCell變量有關,但我不確定。任何人都可以幫我找出我的錯誤,我一直在試圖找到它,但無濟於事。非常感激!麻煩創建一個DFS迷宮

+0

把'String Maze [] [] = new String [20] [20]'改成'char Maze [] [] = new char [20] [20]':那麼你可以使用'... == '#''而不是'... equals(「#」)' – 2012-04-11 13:48:54

+0

注意命名約定......命名變量時,第一個單詞不是大寫,後面的單詞是。另外,除非明確聲明爲'final'(指的是'NORTH','SOUTH','EAST'和'WEST'變量),否則不要大寫變量名的每個字母。 – fireshadow52 2012-04-11 14:05:20

回答

1

確定。這不能完全解決你的程序,但它修復代碼中的問題:

你爲if(EAST == "#")(或類似命令)做了很多的測試。但是,你不能用字符串使用==,因爲這樣比較它們的引用。您必須使用.equals()。所以,你會使用:if(EAST.equals("#"))

我也看不到你在哪裏改變Maze [] []數組的內容。這似乎只是你正在編輯一個堆棧,而忽視你最終在最後打印陣列。

風格調整:

  1. 如果你有布爾變量(姑且稱之爲var),你不必使用if(var == true),而是使用if(var)

  2. 請勿使用if/else語句分配布爾變量。例如:

    if(WEST == "#") 
        WestT = true; 
    else 
        WestT = false; 
    

    可以簡單求到: WestT = WEST.equals("#");

0

有幾個問題與您的實現。您最好的問題是使用CurrentCell變量。它只是一個字符串變量,你正在改變一個變量的值,但從來不碰迷宮細胞的真實價值,除了在(10,10)的細胞。

建議:創建一個Cell對象來表示具有屬性x,y座標,字符「#」或「 - 」的迷宮單元作爲迷宮單元格值,以及布爾型「visited」來指示它是否具有被訪問過。更新當前單元格的x,y座標。每次移動後設置當前單元格值。