import java.io.BufferedReader; import java.io.FileReader; import java.util.StringTokenizer; /* * This class models a maze. It can read it from a file, identify * the start and finish, as well as the paths, and be viewed as a * String, &c */ public class Maze { private boolean maze[][]; // 2D maze; true=path, false=wall // Start position private int start_row; private int start_col; // Finish position private int finish_row; private int finish_col; // Models a cell, or (row, col) pair within the maze. This is basically // here so that we can return both coordinates at once. public static class Cell { // The row and the column of some cell (path or wall) within the maze private int row; private int col; // Returns the column in which this cell lives public int getCol() { // Insert your code here (and delete this comment) } // Returns the row in which this cell lives public int getRow() { // Insert your code here (and delete this comment) } // "true" if two cells have the same (row, col) pair, "false" otherwise public boolean isEqual(Object obj) { // Insert your code here (and delete this comment) } // Returns String representation of a Cell. public String toString() { return "(" + row + "," + col + ")"; } // A very simple constructor public Cell(int row, int col) { this.row = row; this.col = col; } } // This gets thrown if we detect a corrupt input file public class InvalidMazeFileException extends Exception { private int lineNum; // The current line (what we just read) of input // Returns the current line of the input file public int getLineNumber() { return lineNum; } // String representation of the exception: basically, the line number public String toString() { String s = super.toString(); s = s + " at line: " + lineNum; return s; } // A very simple constructor public InvalidMazeFileException(String message, int lineNumber) { super(message); this.lineNum = lineNumber; } } // This class modesl the maze: a 2D array containing walls and paths public Maze(String filename) throws InvalidMazeFileException { int lineNum = 0; int numCols = 0; int numRows = 0; finish_col = -1; finish_row = -1; BufferedReader mazeFile = null; StringTokenizer lineTokenizer = null; String token = null; String line = null; try { mazeFile = new BufferedReader(new FileReader(filename)); while(lineNum < 4) { lineNum++; line = mazeFile.readLine(); lineTokenizer = new StringTokenizer(line); token = lineTokenizer.nextToken(); if(token.equalsIgnoreCase("Rows:")) { numRows = Integer.parseInt(lineTokenizer.nextToken()); } else if(token.equalsIgnoreCase("Cols:")) { numCols = Integer.parseInt(lineTokenizer.nextToken()); } else if(token.equalsIgnoreCase("Start:")) { lineTokenizer = new StringTokenizer(line, "(,)"); lineTokenizer.nextToken(); start_row = Integer.parseInt(lineTokenizer.nextToken()); start_col = Integer.parseInt(lineTokenizer.nextToken()); } else if(token.equalsIgnoreCase("Finish:")) { lineTokenizer = new StringTokenizer(line, "(,)"); lineTokenizer.nextToken(); finish_row = Integer.parseInt(lineTokenizer.nextToken()); finish_col = Integer.parseInt(lineTokenizer.nextToken()); } else { throw new InvalidMazeFileException("Invalid Maze File", lineNum); } } if((numRows <= 0) || (numCols <= 0)) { throw new InvalidMazeFileException("Invalid Maze File", lineNum); } maze = new boolean [numRows][numCols]; for(int row = 0; row < numRows; row++) { for(int col = 0; col < numCols; col++) { maze[row][col] = false; } } //Read in each line of the file int row, col; lineNum++; while((line = mazeFile.readLine()) != null) { //Ignore empty lines if(!line.equals("")) { //Get the row and column out of the line lineTokenizer = new StringTokenizer(line, "(,)"); row = Integer.parseInt(lineTokenizer.nextToken()); col = Integer.parseInt(lineTokenizer.nextToken()); maze[row][col] = true; } lineNum++; } } catch(Exception e) { throw new InvalidMazeFileException("Invalid Maze File", lineNum); } } // Returns the coordinates of the finish block public Cell getFinish() { // Insert your code here (and delete this comment) } // Returns the coordinates of the start block public Cell getStart() { // Insert your code here (and delete this comment) } // "true" if the cell contains the coordinates of "finish", false otherwise // Note: Returns false if out of bounds public boolean isFinish(Cell cell) { // Insert your code here (and delete this comment) } // "true" if the cell contains is a path (not a wall), false otherwise // Note: Returns false if out of bounds // Note: paths are "true", walls are "false" within the maze array public boolean isPath(Cell cell) { // Insert your code here (and delete this comment) } // "true" if the cell contains the coordinates of "start", false otherwise // Note: Returns false if out of bounds public boolean isStart(Cell cell) { // Insert your code here (and delete this comment) } // Make a String representation of the Maze. X for wall, space for path, // S for start, F for finish. // See the assignment doucmentation for sample output public String toString() { // Insert your code here (and delete this comment) } // Used by the constructor to add a path private void makePath(int row, int col) { // if (row, col) is not within the maze, do nothing if((row < 0) || (row >= maze.length) || (col < 0) || (col >= maze[0].length)) { return; } maze[row][col] = true; } }