import java.io.*; import java.util.*; public class MazeTester { protected Maze maze; protected BackTrack backTrack; protected Position start, finish; /** * Reads in the maze information. */ public void readMaze() { final String PROMPT = "\n\nPlease enter the path for the file whose first line contains the " + "number of rows and columns,\nwhose 2nd line the start row and column, " + "whose 3rd line the finish row and column, and then the maze, row-by-row: "; BufferedReader keyboardReader = new BufferedReader ( new InputStreamReader (System.in)); String fileName; BufferedReader fileReader; byte[ ][ ] grid = null; boolean fileFound = false; while (!fileFound) { try { System.out.print (PROMPT); fileName = keyboardReader.readLine(); fileReader = new BufferedReader (new FileReader (fileName)); fileFound = true; StringTokenizer tokens; tokens = new StringTokenizer (fileReader.readLine()); int rows = Integer.parseInt (tokens.nextToken()), columns = Integer.parseInt (tokens.nextToken()); grid = new byte [rows][columns]; tokens = new StringTokenizer (fileReader.readLine()); start = new Position (Integer.parseInt (tokens.nextToken()), Integer.parseInt (tokens.nextToken())); tokens = new StringTokenizer (fileReader.readLine()); finish = new Position (Integer.parseInt (tokens.nextToken()), Integer.parseInt (tokens.nextToken())); for (int i = 0; i < rows; i++) { tokens = new StringTokenizer (fileReader.readLine()); for (int j = 0; j < columns; j++) grid [i][j] = (byte)Integer.parseInt (tokens.nextToken()); } // reading in maze entries fileReader.close(); } // try catch (IOException e) { System.out.print (e); } // catch IOException } // while maze = new Maze (grid, finish); backTrack = new BackTrack (maze); } // method readMaze /** * Prints the results of the maze search. */ public void printResults() { final String INITIAL_STATE = "\nThe initial state is as follows (0 = WALL, 1 = CORRIDOR):\n"; final String START_INVALID = "The start position is invalid."; final String FINISH_INVALID = "The finish position is invalid."; final String FINAL_STATE = "The final state is as follows (2 = DEAD END, 9 = PATH):\n"; final String SUCCESS = "\n\nA solution has been found:"; final String FAILURE = "\n\nThere is no solution:"; System.out.println (INITIAL_STATE + maze); if (!maze.isOK (start)) System.out.println (START_INVALID); else if (!maze.isOK (finish)) System.out.println (FINISH_INVALID); else { maze.markAsPossible (start); if (maze.isGoal (start) || backTrack.tryToReachGoal (start)) System.out.println (SUCCESS); else { maze.markAsDeadEnd (start); System.out.println (FAILURE); } // failure System.out.println (FINAL_STATE + maze); } // start and finish are valid } // method printResults } // class MazeTester