2016-02-06 120 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

您必须将函数调用的结果存储在主变量的第一行中,以存储变量迷宫。既然你没有这样做,迷宫当然是空的。

相关问题