Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 =
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; LinkedListsolutionPath; 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 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 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 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
import java.util.*; public class MazeByHand { public static void main (String[] argv) { Maze maze = new Maze (5, 5); // INSERT YOUR CODE HERE. maze.display (); } }
In-Class Exercise 8: Download Maze.java (which you don't have to read) and modify MazeByHand.java to draw a maze path from (3,4) to (1,1). Do this by breaking walls. You can type in each wall-break separately using calls to breakwall).Then, when that's working, create a solution path and display it In-Class Exercise 8: Download Maze.java (which you don't have to read) and modify MazeByHand.java to draw a maze path from (3,4) to (1,1). Do this by breaking walls. You can type in each wall-break separately using calls to breakwall).Then, when that's working, create a solution path and display it
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started