2013-04-07 3 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을 넣습니다 position (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; 
    } 
} 

... 그리고 여기가 내가 그것을 실행하는 데 사용하는 기본 방법입니다

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를 구축하려고합니다. org /) – Aubin

+0

문제가 어디 있는지 알고 싶지만 너무 많은 코드 (특히 무작위 부분)는 추적하기가 어렵습니다. – iphonedev7

+0

물론,하지만 우리에게는 더 힘들어! IDE에서 준비가 완료되었습니다. 디버거를 사용하여 먼저 공격해야합니다. –

답변

0

이 코드는 여기에 실행 출력 : // sscce :

당신이 말한 당연하지 무슨
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

죄송합니다. 혼란 스럽습니다 ... 내가 게시 한 코드를 실행 했습니까? 게시 한 출력을 제공해야합니다 ... 많은 코드를 제거하려고하는데 도움이 될 것입니다 ... 답변을 주셔서 감사합니다 :) – iphonedev7

관련 문제