import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; import java.lang.Thread; public class MazeTest { public static void main(String[] args) { System.out.println("Please enter the file name: "); Scanner keyboard = new Scanner(System.in); FileReader freader; BufferedReader inputFile; Maze maze = new Maze(); //tries to get the file input by user try { freader = new FileReader(keyboard.nextLine()); } //if file can't be found, catch error catch(FileNotFoundException e) { System.out.println(e); return; } System.out.print("Data being input"); int tempI = 0; while(tempI < 3) { try { Thread.sleep(500); System.out.print("."); tempI++; } catch(Exception e) { System.out.println(e); } } System.out.println("\n"); try { Thread.sleep(250); } catch(Exception e) { System.out.println(e); } //create a buffered reader with the file found inputFile = new BufferedReader(freader); //-------------------------------------------------------------------------------------------------------------// //-------------------------------------------------------------------------------------------------------------// //------------------------------------tries to read user's file------------------------------------------------// //-------------------------------------------------------------------------------------------------------------// //-------------------------------------------------------------------------------------------------------------// try { //temp value used to read in the input file String strTemp = inputFile.readLine(); //tokenizes the first line which holds the dimensions //ex: 5, 8 StringTokenizer dimensionsTok = new StringTokenizer(strTemp, ","); //tries to parse the tokens into integers try { maze = new Maze(Integer.parseInt(dimensionsTok.nextToken().trim()), Integer.parseInt(dimensionsTok.nextToken().trim())); } //if unable to parse, catch the error and prompt user catch(NumberFormatException e) { System.out.println("Error: Improper dimensions entered.\nPlease check your input file."); System.out.println("Dimensions should be entered as follows ==> rowDim, colDim"); return; } //checks to make sure the user hasn't entered any extra, useless data if(dimensionsTok.hasMoreTokens()) { System.out.println("Error: Improper dimensions entered.\nPlease check your input file."); System.out.println("Dimensions should be entered as follows ==> rowDim, colDim"); return; } //---------------------------------------------------------------------------------------------------------// //---------------------------------------------------------------------------------------------------------// //--------------------------------begins manipulating maze data--------------------------------------------// //---------------------------------------------------------------------------------------------------------// //---------------------------------------------------------------------------------------------------------// //instantiates the incrementer for the row int i = -1; //moves to the next line in the file String strTemp1 = null; String strTemp2 = ""; String strTemp3 = inputFile.readLine(); while(strTemp2 != null) { if(strTemp3 != null) { maze.findNodes(strTemp3, i + 1); } //System.err.println(strTemp3); if(i >= 0) { maze.findLinks(strTemp1, strTemp2, strTemp3, i); } strTemp1 = strTemp2; strTemp2 = strTemp3; strTemp3 = inputFile.readLine(); i++; } maze.copyNewNodeList(); //---------------------------------------------------------------------------------------------------------// //---------------------------------------------------------------------------------------------------------// //---------------------------------done manipulating maze data---------------------------------------------// //---------------------------------------------------------------------------------------------------------// //---------------------------------------------------------------------------------------------------------// } //-------------------------------------------------------------------------------------------------------------// //-------------------------------------------------------------------------------------------------------------// //-----------------------------------------done reading file---------------------------------------------------// //-------------------------------------------------------------------------------------------------------------// //-------------------------------------------------------------------------------------------------------------// //if error with handling file, caught and reported catch(IOException e) { System.out.println(e); return; } //maze.initVertices(numOfNodes + 2); System.out.println("Maze entered and recorded into a graph:"); maze.printMaze(); try { Thread.sleep(800); } catch(Exception e) { System.out.println(e); } boolean loop = true; while(loop) { System.out.println("\nWould you like to find a path with depth-first(d) or breadth-first search(b) [or press x to exit]?"); char response = keyboard.nextLine().toUpperCase().charAt(0); if(response == 'D') { System.out.println(maze.depthFirstSearch()); loop = false; } else if(response == 'B') { System.out.println(maze.breadthFirstSearch()); loop = false; } else if(response == 'X') { System.out.println("Maybe next time!"); loop = false; } else { System.out.println("Sorry, an incorrect response was entered. Care to try again?"); loop = true; } } System.out.println("Path:"); System.out.println(maze.getPath().printStack()); //System.err.println("Test"); } }