import java.util.*; public class MazeMaker4 { 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); // Pass the path into the GUI. maze.setSolutionPath (solutionPath); } static boolean recursivelyFindPath (Maze maze, Coord c, Coord end) { // First add the current cell into the path. solutionPath.add (c); // 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 there aren't any, we're done, but couldn't find a path. return false; } // Take the first one and explore from there. maze.markVisited (validNeighbors[0]); return recursivelyFindPath (maze, validNeighbors[0], end); } ////////////////////////////////////////////////////////////// // // Maze generation code public static void generateMaze (int numRows, int numCols) { maze = new Maze (numRows, numCols); desiredPathLength = numRows * numCols; // Initially, we'll start with the top left cell. Coord start = new Coord (0, 0); maze.markVisited (start); // Generate the maze path recursively. boolean found = recursiveGenerate (start, 1); if (! found) { System.out.println ("Could not create the whole maze"); } // Break a few more walls randomly. breakRandomWalls (maze, 10); maze.display(); } static boolean recursiveGenerate (Coord c, int pathLength) { // Bottom out condition 1: if (pathLength == desiredPathLength) { return true; } // Bottom out condition 1: see if we're stuck. Coord[] validNeighbors = maze.getUnvisitedClosedNeighbors (c); if ((validNeighbors == null) || (validNeighbors.length == 0)) { return false; } // Otherwise, we have some neighbors to explore. // Permute the directions randomly. permute (validNeighbors); for (int i=0; i < validNeighbors.length; i++) { // Try neighbor i. maze.breakWall (c, validNeighbors[i]); maze.markVisited (validNeighbors[i]); boolean ok = recursiveGenerate (validNeighbors[i], pathLength+1); if (ok) { // If neighbor i worked out, great. return true; } // Otherwise, undo assignment to i. maze.fixWall (c, validNeighbors[i]); maze.markUnvisited (validNeighbors[i]); } // end-for // Couldn't make it work. return false; } static void breakRandomWalls (Maze maze, int numWalls) { for (int k=0; k