/*************************************************************************/ // Maze: A class for implementing operations on a maze. The maze is a // character matrix of type CharMatrix. The characters must be of // in the set of characters indicated under "Admissible maze characters" // in the constant definitions below. The topmost and bottommost rows // and the leftmost and rightmost columns must consist exclusively of // WALL characters. // // See CharMatrix class for other restrictions on the input file format. /*************************************************************************/ import java.io.*; public class Maze extends CharMatrix implements MazeInterface { /********************************************************************/ // default constructor /********************************************************************/ public Maze() { super(); } /********************************************************************/ // Constructor that reads a maze from a file. The file must be in // the file format of the CharMatrix class; see comments preceding // the constructor in that class. In addition, the borders of the matrix // must consist exclusively of WALL characters (see constant definitions // above) and all characters must have values from the set // {WALL, OPEN, GOAL, MARKED, PATHMARK}. /********************************************************************/ public Maze(String filename) { super(filename); } /*************************************************************************/ /* Count the empty cells that are reachable from the current location. */ /* FILL IN THE BODY OF THIS FUNCTION */ /*************************************************************************/ public int countCellsFrom (int curRow, int curColumn) { setEntry(curRow, curColumn, MARKED); int total = 1; if (getEntry(curRow, curColumn-1) == OPEN) total += countCellsFrom (curRow, curColumn-1); if (getEntry(curRow, curColumn+1) == OPEN) total += countCellsFrom (curRow, curColumn+1); if (getEntry(curRow-1,curColumn) == OPEN) total += countCellsFrom (curRow-1, curColumn); if (getEntry(curRow+1, curColumn) == OPEN) total += countCellsFrom (curRow+1, curColumn); return total; } /**********************************************************************/ // Find and mark a path from the current location to the to the goal */ /* cell, if there is one. Cells on the path should be marked with */ /* PATHMARK. Other cells visited may be marked with MARKED */ /**********************************************************************/ public boolean findGoal(int curRow, int curColumn) { if (getEntry(curRow, curColumn) == GOAL) return FOUND; setEntry(curRow, curColumn, MARKED); if (getEntry(curRow - 1, curColumn) == OPEN || getEntry(curRow - 1, curColumn) == GOAL) if (findGoal (curRow - 1, curColumn) == FOUND) { setEntry(curRow, curColumn, PATHMARK); return FOUND; } if (getEntry(curRow, curColumn + 1) == OPEN || getEntry(curRow, curColumn + 1) == GOAL) if (findGoal(curRow, curColumn + 1) == FOUND) { setEntry(curRow, curColumn, PATHMARK); return FOUND; } if (getEntry(curRow + 1, curColumn) == OPEN || getEntry(curRow + 1, curColumn) == GOAL) if (findGoal (curRow + 1, curColumn) == FOUND) { setEntry(curRow, curColumn, PATHMARK); return FOUND; } if (getEntry(curRow, curColumn - 1) == OPEN || getEntry(curRow, curColumn - 1) == GOAL) if (findGoal(curRow, curColumn - 1) == FOUND) { setEntry(curRow, curColumn, PATHMARK); return FOUND; } return NOTFOUND; } }