import java.util.*; public class MazeMaker5 { static int desiredPathLength; static Maze maze; public static void main (String[] argv) { generateMaze (5, 5); if (maze != null) { // We will seek a path from the top-left to the bottom-right corner. Coord start = new Coord (0,0); Coord end = new Coord (4,4); solveMaze (maze, start, end); maze.display (); } else { System.out.println ("Maze creation did not work"); } } // A path is a list of cells, i.e., a list of Coord instances. static LinkedList solutionPath; static void solveMaze (Maze maze, Coord start, Coord end) { // We'll mark visited cells as we go along our path. Initially: maze.markAllUnvisited (); // Mark the start cell as visited. maze.markVisited (start); // Create the list. solutionPath = new LinkedList (); // Recursively find the path and fill in coord's into the list. recursivelyFindPath (maze, start, end); // The start node gets added last. Why? solutionPath.addFirst (start); // Pass the path into the GUI. maze.setSolutionPath (solutionPath); } static boolean recursivelyFindPath (Maze maze, Coord c, Coord end) { // If we've reached the end, we're done. if ( (c.row == end.row) && (c.col == end.col) ) { return true; } // Otherwise, let's find a neighbor to explore. Coord[] validNeighbors = maze.getUnvisitedOpenNeighbors (c); if (validNeighbors == null) { // If we couldn't find any neighbors to explore, we're stuck. return false; } // Try each neighbor, as many as needed. for (int i=0; i