2013-04-07 66 views
-1

我正在使用遞歸回溯算法創建迷宮生成器。不過,我的問題是,我每次運行程序時它提供了以下結果:使用回溯遞歸算法生成迷宮時出現錯誤

1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 
0 0 0 0 0 0 0 1 1 1 
0 0 0 0 0 0 1 1 1 1 
0 0 0 0 0 1 1 1 1 1 
0 0 0 0 1 1 1 1 1 1 
0 0 0 1 1 1 1 1 1 1 
0 0 1 1 1 1 1 1 1 1 
0 1 1 1 1 1 1 1 1 1 
0 1 1 1 1 1 1 1 1 1 

在我的代碼,一點背景: 構造填充以0迷宮int數組,並在年底把一個1位置(0,0),然後調用backtrackGenerateMaze()方法隨機生成一個迷宮。我認爲這個問題可能是因爲我沒有牆壁,這可能會讓奇怪的事情發生。另外,對於方向:0 =向上,1 =向右,2 =向下,& 3 =向左。希望有所幫助。

這裏是我的代碼(對不起了這麼久,我一直在嘗試使用意見進行調試):

public class Maze { 

    private int width, length; 
    private int[][] maze; 

    public Maze(int rows, int columns) { 
     width = columns; 
     length = rows; 

     maze = new int[rows][columns]; 
     for (int i = 0; i < rows; i++) { 
      for (int j = 0; j < columns; j++) { 
       maze[i][j] = 0; 
      } 
     } 
     maze[0][0] = 1; 

     backtrackGenerateMaze(rows - 1, columns - 1, 1); 
    } 

    /* 
    * THE PROBLEM MUST BE HERE IN THE backtrackGenerateMaze() METHOD 
    */ 
    private void backtrackGenerateMaze(int rows, int columns, int moveLength) { 
     if (rows == 0 && columns == 0) { 
      System.out.println("rows = " + rows); 
      System.out.println("length = " + length); 
      System.out.println("columns = " + columns); 
      System.out.println("width = " + width); 
      return; 
     } 

     int[] randDirs = generateRandomDirections(); 

     for (int dir : randDirs) { 
      System.out.println("dir == " + dir); 
      System.out.println("rows == " + rows); 
      System.out.println("columns == " + columns + "\n"); 
      if (dir == 0) { 
       System.out.println("rows - moveLength == " + (rows - moveLength)); 
       System.out.println("valid(rows - moveLength, columns) == " + (valid(rows - moveLength, columns))); 
       System.out.println("isInRange(0, length, rows - moveLength, false) == " + (isInRange(0, length, rows - moveLength, false))); 

       if (valid(rows - moveLength, columns) 
        && isInRange(0, length, rows - moveLength, false) 
        && maze[rows - moveLength][columns] != 1) { 
        System.out.println("IF 0 is TRUE"); 
        for (int i = 1; i <= moveLength; i++) { 
         maze[rows - moveLength][columns] = 1; 
        } 
        maze[rows][columns] = 1; 
        System.out.println("HERE 0"); 
        backtrackGenerateMaze(rows - moveLength, columns, moveLength); 
       } 

       // System.out.println("RETURN DIR 0"); 
       // return; 
      } else if (dir == 1) { 
       System.out.println("columns + moveLength = " + (columns + moveLength)); 
       System.out.println("valid(rows, columns + moveLength) = " + (valid(rows, columns + moveLength))); 
       System.out.println(); 

       if (valid(rows, columns + moveLength)) { 
        System.out.println("VALID 1 is TRUE"); 
        if (isInRange(0, width, columns + moveLength, false)) { 
         System.out.println("isInRange() 1 is TRUE"); 
         if (maze[rows][columns + moveLength] != 1) { 
          System.out.println("square != 1 is TRUE"); 
          System.out.println("IF 1 is TRUE"); 
          for (int i = 1; i <= moveLength; i++) { 
           maze[rows][columns + moveLength] = 1; 
          } 
          maze[rows][columns] = 1; 
          System.out.println("HERE 1"); 
          backtrackGenerateMaze(rows, columns + moveLength, moveLength); 
         } 
        } 
       } 

       System.out.println("RETURN DIR 1"); 
       return; 
      } else if (dir == 2) { 
       if (valid(rows + moveLength, columns) 
        && isInRange(0, length, rows + moveLength, false) 
        && maze[rows + moveLength][columns] != 1) { 
        System.out.println("IF 2 is TRUE"); 
        for (int i = 1; i <= moveLength; i++) { 
         maze[rows + moveLength][columns] = 1; 
        } 
        maze[rows][columns] = 1; 
        System.out.println("HERE 2"); 
        backtrackGenerateMaze(rows + moveLength, columns, moveLength); 
       } 

       System.out.println("RETURN DIR 2"); 
       return; 
      } else if (dir == 3) { 
       if (valid(rows, columns - moveLength) 
        && isInRange(0, width, columns - moveLength, false) 
        && maze[rows][columns - moveLength] != 1) { 
        System.out.println("IF 3 is TRUE"); 
        for (int i = 1; i <= moveLength; i++) { 
         maze[rows][columns - moveLength] = 1; 
        } 
        maze[rows][columns] = 1; 
        System.out.println("HERE 3"); 
        backtrackGenerateMaze(rows + moveLength, columns - moveLength, moveLength); 
       } 

       System.out.println("RETURN DIR 3"); 
       return; 
      } 
     } 
     System.out.println("--------------------"); 
    } 

    public int[] generateRandomDirections() { 
     ArrayList<Integer> rands = new ArrayList<>(); 
     for (int i = 0; i < 4; i++) { 
      rands.add(i); 
     } 
     Collections.shuffle(rands); 

     int[] ret = new int[4]; 
     for (int i = 0; i < rands.size(); i++) { 
      ret[i] = rands.get(i); 
     } 
     return ret; 
    } 

    private boolean valid(int row, int column) { 
     return isInRange(0, maze.length - 1, row, true) 
       && isInRange(0, maze[0].length - 1, column, true); 
    } 

    private boolean isInRange(int start, int end, int toCheck, 
      boolean inclusive) { 
     if (inclusive) { 
      return (toCheck >= start && toCheck <= end); 
     } 
     return (toCheck > start && toCheck < end); 
    } 

    @Override 
    public String toString() { 
     String ret = ""; 
     for (int i = 0; i < maze.length; i++) { 
      for (int j = 0; j < maze[0].length; j++) { 
       ret += maze[i][j] + " "; 
      } 
      ret += "\n"; 
     } 
     return ret; 
    } 
} 

...這是Main方法我用它來運行它:

public class MazeGame { 

    private static ArrayList<Maze> mazes = new ArrayList<>(); 
    private static final int MAZE_SIZE = 10, NUM_MAZES = 1; 

    public static void main(String[] args) { 
     Maze temp; 
     for (int i = 0; i < NUM_MAZES; i++) { 
      temp = new Maze(MAZE_SIZE, MAZE_SIZE); 
      System.out.println(temp); 
      mazes.add(temp); 
     } 
    } 
} 

任何幫助,非常感謝。

+0

嘗試建立一個[短,自Containd,正確的示例](HTTP:// SSCCE。 org /) – Aubin 2013-04-07 18:11:37

+0

如果我知道問題出在哪裏,但是會有太多的代碼很難追蹤(尤其是隨機部分)。 – iphonedev7 2013-04-07 18:14:31

+0

當然,但對我們來說更難!您已經將所有這些設置在您的IDE中準備就緒。恐怕你必須先用調試器攻擊它。 – 2013-04-07 18:17:13

回答

0

此代碼,運行在這裏,輸出:

dir == 2 
rows == 9 
columns == 9 

RETURN DIR 2 
1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 

你說的絕對不是什麼...

+0

對不起,我很困惑......你是否運行了我發佈的代碼,因爲它應該會給我發佈的輸出......我即將刪除大量代碼,這應該有所幫助......感謝回答tho :) – iphonedev7 2013-04-07 18:25:09