2017-02-17 3 views
-1

OK also ich bin fertig mit einem Labyrinth-Programm, aber das Problem ist, es dauert nur eine Matrix der Größe 20x20, ich programmierte es eine 30x20 Matrix zu nehmen, aber es gibt mir immer einen Fehler wie folgt:Maze Traversal in Java

Kann mir bitte jemand einen Blick darauf werfen und mir Feedback geben, wo ich versagt habe?

Meine Vermutung ist, das Problem liegt hier in der Methode

private static char[][] loadMaze(String filename) throws Exception

in der MazeSolver Klasse auf der Linie 49:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 

public class MazeSolver { 



    private static Scanner keyboard = new Scanner(System.in); 

    /** 
    * Forces the user to enter a string 
    */ 
    private static String readString(String prompt) { 
     while (true) { 
      System.out.print(prompt); 
      String value = keyboard.nextLine().trim(); 

      if (!value.isEmpty()) { 
       return value; 
      } 

      System.out.println("Error: Please enter a value."); 
     } 
    } 

    /** 
    * Forces the user to enter an integer 
    */ 
    private static int readInt(String prompt) { 
     while (true) { 
      try { 
       System.out.print(prompt); 
       return Integer.parseInt(keyboard.nextLine()); 
      } catch (Exception e) { 
       System.out.println("Error: Please enter a numeric value."); 
      } 
     } 
    } 

    /** 
    * Load the maze from file 
    */ 
    private static char[][] loadMaze(String filename) throws Exception { 
     // Load the maze file, we assume the file is 30 x 20 
     char[][] maze = new char[30][20]; 

     try { 
      Scanner inFile = new Scanner(new File(filename)); 

      for (int row = 0; row < maze.length; row++) { 
       String line = inFile.nextLine(); 

       for (int column = 0; column < maze.length; column++) { 
        maze[row][column] = line.charAt(column); 

        if (maze[row][column] != PATH && maze[row][column] != WALL && maze[row][column] != EXIT) { 
         throw new Exception("Invalid map."); 
        } 
       } 
      } 

      inFile.close(); 
     } catch(FileNotFoundException e) { 
      System.out.println("The file " + filename + " does not exist."); 
      System.exit(0); 
     } catch (Exception e) { 
      throw new Exception("Invalid map."); 
     } 

     return maze; 
    } 

    /** 
    * Forces the user to provide a starting coordinate 
    */ 
    private static Coordinate readStartingCoordinate(char[][] maze) { 
     int startingRow = 0; 
     int startingColumn = 0; 

     while (true) { 
      // Read a valid row 
      startingRow = readInt("Enter starting row: "); 

      while (startingRow < 0 || startingRow >= 30) { 
       System.out.println("Error: Please enter a value from 0 to 29"); 
       startingRow = readInt("Enter starting row: "); 
      } 

      // Read a valid column 
      startingColumn = readInt("Enter starting column: "); 

      while (startingColumn < 0 || startingColumn >= 20) { 
       System.out.println("Error: Please enter a value from 0 to 19"); 
       startingColumn = readInt("Enter starting column: "); 
      } 

      // Make sure that the starting coordinate isn't a '1' (wall) 
      if (maze[startingRow][startingColumn] == WALL) { 
       System.out.println("Error: The starting point you selected is a wall"); 
      } else { 
       break; 
      } 
     } 

     return new Coordinate(startingRow, startingColumn); 
    } 

    /** 
    * Solve the maze recursively 
    */ 


     // Go down 
     if (row + 1 < maze.length && (maze[row + 1][column] == PATH || maze[row + 1][column] == EXIT)) { 
      path.push(new Coordinate(row + 1, column)); 

      if (solveMaze(maze, path)) { 
       return true; 
      } 
     } 

     // Go left 
     if (column - 1 >= 0 && (maze[row][column - 1] == PATH || maze[row][column - 1] == EXIT)) { 
      path.push(new Coordinate(row, column - 1)); 

      if (solveMaze(maze, path)) { 
       return true; 
      } 
     } 

     // Go right 
     if (column + 1 < maze.length && (maze[row][column + 1] == PATH || maze[row][column + 1] == EXIT)) { 
      path.push(new Coordinate(row, column + 1)); 

      if (solveMaze(maze, path)) { 
       return true; 
      } 
     } 

     // If none of the directions worked, we reached a dead end 
     path.pop(); 
     return false; 
    } 

    /** 
    * Remove all visited marks from the maze and put the correct path the 
    * computer to solve it 
    */ 
    private static void cleanMaze(char[][] maze, LinkedList path, Coordinate start) { 
     for (int row = 0; row < maze.length; row++) { 
      for (int column = 0; column < maze.length; column++) { 
       if (maze[row][column] == VISITED) { 
        maze[row][column] = PATH; 
       } 
      } 
     } 

     Coordinate end = path.pop(); 
     maze[end.getRow()][end.getColumn()] = EXIT; 

     while(!path.isEmpty()) { 
      Coordinate coordinate = path.pop(); 
      maze[coordinate.getRow()][coordinate.getColumn()] = ANSWER; 
     } 

     maze[start.getRow()][start.getColumn()] = START; 
    } 

    /** 
    * Print the maze with numbered row and columns 
    */ 
    private static void printMaze(char[][] maze) { 
     System.out.printf("%3s", " "); 

     for (int i = 0; i < maze.length; i++) { 
      System.out.printf("%3d", i); 
     } 

     System.out.println(); 

     for (int row = 0; row < maze.length; row++) { 
      System.out.printf("%-3d", row); 

      for (int column = 0; column < maze.length; column++) { 
       System.out.printf("%3c", maze[row][column]); 
      } 

      System.out.println(); 
     } 

     System.out.println(); 
    } 

    /** 
    * Entry point of the program 
    */ 
    public static void main(String[] args) throws Exception { 
     // Ask for the file to solve and load the maze 
       String filename ="C:\\Users\\TriZam\\Desktop\\MazeFile.txt";     // readString("Enter maze filename: "); 


     char[][] maze = loadMaze(filename); 

     // Ask for starting coordinates 
     printMaze(maze); 
     Coordinate start = readStartingCoordinate(maze); 
     LinkedList path = new LinkedList(); 

     // Solve the maze 
     path.push(start); 
     solveMaze(maze, path); 

     // If map is solved print out results 
     if (solveMaze(maze, path)) { 
      cleanMaze(maze, path, start); 
      printMaze(maze); 
      System.out.println("I am free"); 
     } else { 
      printMaze(maze); 
      System.out.println("Help, I am trapped"); 
     } 
    } 
} 
+0

Verstehst du, dass *** *** DEINE *** Ausnahme geworfen wird? Wenn dein Code ein 30x20 Labyrinth annimmt, aber die Eingabe nur 20x20 ist, erwartest du, dass er sich magisch anpasst? Sie sollten den Stack-Trace der ursprünglichen Ausnahme drucken, um das wirkliche Problem herauszufinden, das wahrscheinlich 'IndexOutOfBoundsException' ist. –

+0

Was enthält die Datei 'C: \ Users \ TriZam \ Desktop \ MazeFile.txt'? – anacron

+0

mein Labyrinth TXT-Datei – user3500147

Antwort

0

loadMaze Iterierten beide Zeile und Spalte über maze.length, so funktioniert nur auf quadratischen Labyrinthe. Sie müssen die Länge in zwei Werte aufteilen: Breite und Höhe.

+0

ich 10 hinzugefügt, um Höhe aber immer noch nichts – user3500147

1

Ich löste es dank Debugging. Obwohl ich das Debuggen nie benutzt habe, weiß ich jetzt mehr darüber, wie man es benutzt, um Probleme zu finden.