package lect78_impl.model; /** * @overview Represents a position on the board * * @attributes * x Integer X coordinate * y Integer Y coordinate */ public class Position { private int x; private int y; /** * @effects Initializes this as a Position with coordinates (x,y) */ public Position(int x, int y) { this.x = x; this.y = y; } /** * @effects Returns the x coordinate */ public int getX() { return x; } /** * @effects Returns the y coordinate */ public int getY() { return y; } /** * @effects Sets the position to (x,y) */ public void setPosition(int x, int y) { this.x = x; this.y = y; } /** * @effects Calculates distance to another position * @return The distance as double */ public double getDistance(Position other) { int dx = other.x - this.x; int dy = other.y - this.y; return Math.sqrt(dx * dx + dy * dy); } /** * @effects Creates an array of adjacent positions (up, down, left, right) * @return Array of positions */ public Position[] getAdjacent() { return new Position[] { new Position(x+1, y), // Right new Position(x-1, y), // Left new Position(x, y+1), // Down new Position(x, y-1) // Up }; } /** * @effects Checks if this position equals another position */ @Override public boolean equals(Object obj) { if (!(obj instanceof Position)) { return false; } Position other = (Position) obj; return this.x == other.x && this.y == other.y; } /** * @effects Generates a hash code for this position */ @Override public int hashCode() { return x * 31 + y; } /** * @effects Returns string representation of this position */ @Override public String toString() { return "(" + x + "," + y + ")"; } }