MAZE Project Write a Maze class with the following requirements: Takes any two-d
ID: 3725009 • Letter: M
Question
MAZE Project Write a Maze class with the following requirements: Takes any two-dimensional array that represents a maze: 0s (zeros) for the walls, and 1s for the available paths. A zip file has been uploaded to the project folder for you to complete. There will be only one constructor that takes a two-dimensional array A method named “displayMaze” should print the maze (bird-view) that shows the walls and the available paths, A method named “displayPath” should print the maze (bird-view) that shows the walls and the available paths,and the route for the solution until the current location (for example the location of the mouse and the path it took from start point to current location A method named “takeStep” that takes one step each time when it is called and displays the maze again A method named “findExit” runs and finds the solution all the way to the exit and displays the maze showing the suggested path Assume that entry point of the maze is always the last row and the first column and the first move direction will be to the north. Therefore, with the constructor, set the location and direction properly. Ignore checking exit for the first 3 steps. Test program is given for your testing The program should work with any Maze including different size of maze. Sample display of the maze Sample output after taking some steps. “~” represents the path towards solution, “@” represents a mouse
Explanation / Answer
The Cell Class
package maze;
/** A class to represent cells in a maze. Cells can be blocked,
* open, or visited. In addition, entrances and exits in a maze
* are represented by a special cell type.
*/
public class Cell {
/* Class variables */
private static final char OPEN = 'o';
private static final char VISITED = 'v';
private static final char CLOSED = '#';
private static final char ENTRANCE = 'e';
private static final char EXIT = 'x';
/* Member variables. */
private int row;
private int column;
private char state;
/* Constructor */
public Cell(int row,int column,char code) {
this.row = row;
this.column = column;
state = code;
}
/* Accessor methods */
public boolean isOpen() { return state == OPEN || state == ENTRANCE || state == EXIT; }
public boolean isEntrance() { return state == ENTRANCE; }
public boolean isExit() { return state == EXIT; }
public int getRow() { return row; }
public int getColumn() { return column; }
public char toChar() {
return state;
}
public String toString() {
return "(" + row + "," + column + ":" + toChar() + ")";
}
/* Mutator methods */
public void markVisited() {
if(state == OPEN)
state = VISITED;
}
public void clearVisited() {
if(state == VISITED)
state = OPEN;
}
/* Test program to test the Cell class. */
public static void main(String[] args) {
Cell firstCell = new Cell(1,1,'o');
System.out.println("Before visit the cell is: " + firstCell.toString());
firstCell.markVisited();
System.out.println("After visit the cell is: " + firstCell.toString());
System.out.println();
Cell secondCell = new Cell(3,1,'#');
System.out.println("Before visit the cell is: " + secondCell.toString());
firstCell.markVisited();
System.out.println("After visit the cell is: " + secondCell.toString());
}
}
Two dimensional arrays maze class
public class Maze {
/* Member variables */
private Cell[][] grid;
/* Construct a maze by reading it from a file. */
public Maze(String fileName) throws Exception {}
/* Accessor methods */
// Find and return an entrance cell
public Cell findEntrance() {}
// Find a neighbor of the given cell that is open
// and return a reference to that neighbor cell. If
// you can find no such neighbor, return null.
public Cell findOpenNeighbor(Cell fromCell) {}
// Print the contents of the Maze to System.out
public void print() {}
/* Mutator methods */
// Reset all visited cells to open
public void clearVisited() {}
// Test program to test the Maze class
public static void main(String args[]) throws Exception {}
}
The Path class
public class Path {
/* Member variables */
private ArrayList<Cell> cells; // Holds the cells along the path
/* Construct an initially empty path. */
public Path() {}
/* Accessor methods */
// Return the last cell on the path, or null if the path is empty.
public Cell lastCell() {}
// Print every cell on the path
public void print() {}
/* Mutator methods */
// Add the given cell to the end of the path
public void addCell(Cell toAdd) {}
// Remove the last cell from the path
public void removeCell() {}
// Test program to test the Path class
public static void main(String args[]) {}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.