import java.awt.Color; import java.awt.Graphics; public class Point implements Comparable { /** Represents the x coordinate of the point */ private int x; /** Represents the y coordinate of the point */ private int y; /** Constructs a new point given its coordinates */ public Point(int x, int y) { this.x = x; this.y = y; } /** Compares two points first by their x coordinates * and then by their y coordinates. */ public int compareTo(Point o) { int result = ((Integer)this.x).compareTo(o.x); if (result == 0) { result = ((Integer)this.y).compareTo(o.y); } return result; } /** Returns true if this point is outside the * bounds of the grid and false otherwise. */ public boolean isOOB() { return this.x < 0 || this.y < 0 || this.x >= Maze.WIDTH || this.y >= Maze.HEIGHT; } /** Returns the x coordinate of the point */ public int getX() { return this.x; } /** Returns the y coordinate of the point */ public int getY() { return this.y; } /** Returns a string representation of the point of * the form "(x, y)". */ public String toString() { return "(" + this.x + ", " + this.y + ")"; } /** Returns a new point that is directly above this one. */ public Point getAbove() { return new Point(this.x, this.y - 1); } /** Returns a new point that is directly below this one. */ public Point getBelow() { return new Point(this.x, this.y + 1); } /** Returns a new point that is directly left of this one. */ public Point getLeft() { return new Point(this.x - 1, this.y); } /** Returns a new point that is directly right of this one. */ public Point getRight() { return new Point(this.x + 1, this.y); } /** Returns true if this point is (0, 0) and * false otherwise. */ public boolean isZero() { return this.x == 0 && this.y == 0; } /** Returns true if this point is the bottom * right point of the grid (e.g. the maze goal) * and false otherwise. */ public boolean isGoal() { return this.x == Maze.WIDTH - 1 && this.y == Maze.HEIGHT - 1; } /** Returns true if this point is a wall and false otherwise. */ public boolean isWall() { return Maze.panel.getColor(this.x, this.y).equals(Color.BLACK); } /** Returns true if this point is an unvisited, non-wall part * of the maze and false otherwise. */ public boolean isPassage() { return Maze.panel.getColor(this.x, this.y).equals(Color.WHITE); } /** Sets the color of this point to the argument */ private void setColor(Color c) { Graphics g = Maze.panel.getGraphics(); g.setColor(c); g.fillRect(x, y, 1, 1); } /** Sets the color of this point to the wall color */ public void makeWall() { this.setColor(Color.BLACK); } /** Sets the color of this point to the unvisited * passage color */ public void makePassage() { this.setColor(Color.WHITE); } /** Sets the color of this point to the color of the * solution path. */ public void makeVisited() { this.setColor(Color.GREEN); } /** Sets the color of this point to the color of a dead * end in the path. */ public void makeDeadEnd() { this.setColor(Color.BLUE); } public boolean equals(Point o) { return this.x == o.x && this.y == o.y; } @Override public int hashCode() { int hash = 7; hash = 71 * hash + this.x; hash = 71 * hash + this.y; return hash; } }