import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author geoffreytowell * Holds a maze. */ public class Maze { public static final char START = 's'; public static final char FINISH = 'e'; public static final char WALL = '*'; public static final char PATH = ' '; /** * The internal representation of the maze */ char[][] mazeRep; Coordinate[] moves = { new Coordinate(-1, -1), new Coordinate(1, -1), new Coordinate(1, 1), new Coordinate(-1, 1) }; /** * Maze constructor. This takes the name of a file containing a maze and reads it into * the internal represenation. * @param fileName the name of the file containing the maze * @throws FileNotFoundException * @throws ImproperMazeException */ public Maze(String fileName) throws FileNotFoundException, ImproperMazeException { // first read the file into an array list to get the dimensions of the maze ArrayList tmaz = new ArrayList<>(); Scanner fscanner = new Scanner(new File(fileName)); int wid=-1; while (fscanner.hasNextLine()) { String lin = fscanner.nextLine(); if (wid>0 && lin.length()!=wid) { fscanner.close(); throw new ImproperMazeException("Maze must be a rectangle current width="+wid+" new line width="+lin.length()); } if (wid<0) wid=lin.length(); tmaz.add(lin); } fscanner.close(); // with the file read complete, fill in the internal representation mazeRep = new char[tmaz.size()][wid]; for (int i=0; i0) fn = args[0]; Maze m; try { m = new Maze(fn); } catch (FileNotFoundException fnf) { System.err.println(fnf + " " + fn); return; } catch (ImproperMazeException ime) { System.err.println(ime); return; } } }