2016-02-06 41 views
0

程序應該遞歸解決迷宮問題。 readMazeFile將文件的內容讀入數組,然後solveMaze函數使用該數組來解決迷宮問題。但在我的主要功能沒有過去如果(迷宮!=空)似乎並沒有運行。我將其包括在內以擺脫空指針異常。迷宮=空嗎?我不這麼認爲,但是是idk。感謝您的幫助提前。爲什麼我的函數不能在我的程序(java)中運行?

public class solving { 
    static char maze[][]; 
    static int startingrow; 
    static int startingcol; 

    public static void main(String[] args) throws FileNotFoundException { 
     readMazeFile("maze0.txt"); 
     if (maze != null) { 
      System.out.print(maze[1][1]); 

      if (solveMaze(startingrow, startingcol)) 
       System.out.print("Solved!"); 
      else 
       System.out.print("There is no solution to this maze."); 
     } 
    } 

    static boolean solveMaze(int row, int col) { 
     // name each movement to make coding easier to understand with the recursion. 
     char right = maze[row][col + 1]; 
     char left = maze[row][col - 1]; 
     char up = maze[row - 1][col]; 
     char down = maze[row + 1][col]; 
     char markSpot = 'M'; 
     char unmarkSpot = ' '; 

     // Base case is at the end of the maze 
     if (right == 'E' || left == 'E' || up == 'E' || down == 'E') { 
      return true; 
     } 

     // What to do if there is an empty space when it moves 
     if (right == ' ') { 
      right = markSpot; 
      if (solveMaze(row, col + 1)) { 
       return true; 
      } else { 
       right = unmarkSpot; 
      } 
     } 

     if (down == ' ') { 
      down = markSpot; 
      if (solveMaze(row + 1, col)) { 
       return true; 
      } else { 
       up = unmarkSpot; 
      } 
     } 

     if (left == ' ') { 
      left = markSpot; 
      if (solveMaze(row, col - 1)) { 
       return true; 
      } else { 
       left = unmarkSpot; 
      } 
     } 

     if (up == ' ') { 
      up = markSpot; 
      if (solveMaze(row - 1, col)) { 
       return true; 
      } else { 
       up = unmarkSpot; 
      } 
     } 
     return false; 
    } 

    static char[][] readMazeFile(String mazeFile) throws FileNotFoundException { 
     Scanner input = new Scanner(new File(mazeFile)); 

     // Find the height and width 
     int height = input.nextInt(); 
     int width = input.nextInt(); 
     int finalHeight = (2 * height) + 1; 
     int finalWidth = (2 * width) + 1; 

     // Create the array and put data from the file in it 
     char maze[][] = new char[finalHeight][finalWidth]; 
     input.nextLine(); 

     for (int row = 0; row < finalHeight; row++) { 
      String fileLine = input.nextLine(); 
      for (int col = 0; col < finalWidth; col++) { 
       char nextChar = fileLine.charAt(col); 
       maze[row][col] = nextChar; 
      } 
     } 

     // Find the starting point 
     for (int r = 0; r < finalHeight; r++) { 
      for (int c = 0; c < finalWidth; c++) { 
       if (maze[r][c] == 'S') { 
        int startingrow = r; 
        int startingcol = c; 
        //System.out.print(startingrow); 
        //System.out.print(startingcol); 
       } 
      } 
     } 

     return maze; 
    } 
} 
+0

看起來這將是空給我。你爲什麼認爲它不會爲空? – bradimus

+0

'readMazeFile'不能返回null(好),所以沒有點試圖做一個null檢查。它只是增加了噪音。但是,因爲你沒有存儲返回值,無論如何,你只是檢查陰影'maze',這是從來沒有實例化。 –

回答

3

readMazeFile陰影您使用的是有條件的靜態變量maze變量。

或者:

  • 指定的readMazeFile結果。
  • 不要聲明中readMazeFilemaze變量(除去char類型說明符)。返回它變得沒有必要。
0

您必須將函數調用的結果存儲在主變量的第一行中,以存儲變量迷宮。既然你沒有這樣做,迷宮當然是空的。

相關問題