/* * Created on Feb 29, 2004 @author kim * * Two dimensional color grid with cells that can be updated. */ import java.awt.*; import objectdraw.*; public class MazeGrid { // constants // maze colors corresponding to: public static final Color FAILED_COLOR = Color.yellow;// Visited, but failed public static final Color VISITED_COLOR = Color.blue; // On current path public static final Color WALL_COLOR = Color.black; // Wall public static final Color OPEN_COLOR = Color.white; // Unvisited cell public static final Color START_COLOR = Color.green; // Start position public static final Color FINISH_COLOR = Color.red; // Finish position protected int numRows, numCols; // num rows and cols in grid protected double rwidth, rheight; // width and ht of each square protected double gleft, gtop; // coords of upper left hand corner of grid protected FilledRect[][] colorRect; // rectangles of grid // post: two dim'l grid drawn at (left,top) with "rows" rows and "cols" // columns. Each square is width x height and initially colored "WALL_COLOR". public MazeGrid(double left, double top, int rows, int cols, double width, double height, DrawingCanvas canvas) { canvas.clear(); // erase canvas rwidth = width; // save parameters needed later rheight = height; gleft = left; gtop = top; numRows = rows; numCols = cols; // create grid as numRows x numCols table of colored rectangles colorRect = new FilledRect[numRows][numCols]; for (int rowNum = 0; rowNum < numRows; rowNum++) { for (int colNum = 0; colNum < numCols; colNum++) { colorRect[rowNum][colNum] = new FilledRect(left + colNum * width, top + rowNum * height, width, height, canvas); colorRect[rowNum][colNum].setColor(WALL_COLOR); new FramedRect(left + colNum * width, top + rowNum * height, width, height, canvas); } } } // pre: pos is legal position in grid // post: rectangle at pos is made into start cell public void setStart(Position pos) { colorRect[pos.getRow()][pos.getCol()].setColor(START_COLOR); } // pre: pos is legal position in grid // post: rectangle at pos is made into finish cell public void setFinish(Position pos) { colorRect[pos.getRow()][pos.getCol()].setColor(FINISH_COLOR); } // pre: pos is legal position in grid // post: rectangle at pos is made into open cell public void setOpen(Position pos) { colorRect[pos.getRow()][pos.getCol()].setColor(OPEN_COLOR); } // pre: pos is legal position in grid // post: rectangle at pos is made into visited cell public void visit(Position pos) { colorRect[pos.getRow()][pos.getCol()].setColor(VISITED_COLOR); } // pre: pos is legal position in grid // post: rectangle at pos is made into "failed" cell public void fail(Position pos) { colorRect[pos.getRow()][pos.getCol()].setColor(FAILED_COLOR); } // pre: pos is legal position in grid // post: returns true iff pos is start cell public boolean isStart(Position pos) { return colorRect[pos.getRow()][pos.getCol()].getColor().equals( START_COLOR); } // pre: pos is legal position in grid // post: returns true iff pos is wall cell public boolean isWall(Position pos) { return colorRect[pos.getRow()][pos.getCol()].getColor().equals( WALL_COLOR); } // pre: pos is legal position in grid // post: returns true iff pos is open cell public boolean isOpen(Position pos) { return colorRect[pos.getRow()][pos.getCol()].getColor().equals( OPEN_COLOR); } // pre: pos is legal position in grid // post: returns true iff pos is currently on path from start cell public boolean isOnPath(Position pos) { return colorRect[pos.getRow()][pos.getCol()].getColor().equals( VISITED_COLOR); } // pre: pos is legal position in grid // post: returns true iff pos was tried but can't get to finish // from there. public boolean isFailed(Position pos) { return colorRect[pos.getRow()][pos.getCol()].getColor().equals( FAILED_COLOR); } // pre: pos is legal position in grid // post: returns true iff pos is finish cell public boolean isFinish(Position pos) { return colorRect[pos.getRow()][pos.getCol()].getColor().equals( FINISH_COLOR); } // returns true iff in Maze and either open or finish cell public boolean canVisit(Position pos) { int rowNum = pos.getRow(); int colNum = pos.getCol(); return ((rowNum > 0 && rowNum < numRows && colNum > 0 && colNum < numCols) && (isOpen(pos) || isFinish(pos))); } }