import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import java.util.*; public class Maze { static final int OPEN = 0; static final int CLOSED = 1; static final int UNVISITED = 0; static final int VISITED = 1; static final int NORTH = 1; static final int SOUTH = 2; static final int EAST = 3; static final int WEST = 4; int numRows = -1, numCols = -1; int startX, startY; int maxPathLength; // Convention: topleft is (0,0). Rows count downward, // columns go left to right. Thus maze[r][c] is r-th row from top, // c-th col from left. int[][][] maze; LinkedList solutionPath; public Maze (int numRows, int numCols) { this.numRows = numRows; this.numCols = numCols; // Make the array of walls. maze = new int [numRows][numCols][5]; // Initialize to closed. for (int x=0; x 0) && (maze[x][y][NORTH] == CLOSED) && (maze[x-1][y][0] == UNVISITED)) { possibleNeighbors[numValid] = new Coord (x-1,y); numValid ++; } // SOUTH if ((x < numRows-1) && (maze[x][y][SOUTH] == CLOSED) && (maze[x+1][y][0] == UNVISITED)) { possibleNeighbors[numValid] = new Coord (x+1,y); numValid ++; } // EAST if ((y < numCols-1) && (maze[x][y][EAST] == CLOSED) && (maze[x][y+1][0] == UNVISITED)) { possibleNeighbors[numValid] = new Coord (x,y+1); numValid ++; } // WEST if ((y > 0) && (maze[x][y][WEST] == CLOSED) && (maze[x][y-1][0] == UNVISITED)) { possibleNeighbors[numValid] = new Coord (x,y-1); numValid ++; } if (numValid == 0) { return null; } // Now compact the array. Coord[] validNeighbors = new Coord [numValid]; for (int i=0; i solutionPath) { this.solutionPath = solutionPath; } public Coord[] getClosedNeighbors (Coord c) { Coord[] possibleNeighbors = new Coord [4]; int numValid = 0; int x = c.row; int y = c.col; // See if North is valid. if ( (x > 0) && (maze[x][y][NORTH] == CLOSED) ) { possibleNeighbors[numValid] = new Coord (x-1,y); numValid ++; } // SOUTH if ( (x < numRows-1) && (maze[x][y][SOUTH] == CLOSED) ) { possibleNeighbors[numValid] = new Coord (x+1,y); numValid ++; } // EAST if ( (y < numCols-1) && (maze[x][y][EAST] == CLOSED) ) { possibleNeighbors[numValid] = new Coord (x,y+1); numValid ++; } // WEST if ( (y > 0) && (maze[x][y][WEST] == CLOSED) ) { possibleNeighbors[numValid] = new Coord (x,y-1); numValid ++; } if (numValid == 0) { return null; } // Now compact the array. Coord[] validNeighbors = new Coord [numValid]; for (int i=0; i 0) && (maze[x][y][NORTH] == OPEN) && (maze[x-1][y][0] == UNVISITED)) { possibleNeighbors[numValid] = new Coord (x-1,y); numValid ++; } // SOUTH if ((x < numRows-1) && (maze[x][y][SOUTH] == OPEN) && (maze[x+1][y][0] == UNVISITED)) { possibleNeighbors[numValid] = new Coord (x+1,y); numValid ++; } // EAST if ((y < numCols-1) && (maze[x][y][EAST] == OPEN) && (maze[x][y+1][0] == UNVISITED)) { possibleNeighbors[numValid] = new Coord (x,y+1); numValid ++; } // WEST if ((y > 0) && (maze[x][y][WEST] == OPEN) && (maze[x][y-1][0] == UNVISITED)) { possibleNeighbors[numValid] = new Coord (x,y-1); numValid ++; } if (numValid == 0) { return null; } // Now compact the array. Coord[] validNeighbors = new Coord [numValid]; for (int i=0; i solutionPath; public void paintComponent (Graphics g) { int numRows = maze.length; int numCols = maze[0].length; Dimension D = this.getSize(); int cellHeight = D.height / numRows; int cellWidth = D.width / numCols; int xSeg = cellWidth / 4; int ySeg = cellHeight / 4; for (int i=0; i