60 lines
1.4 KiB
Java
60 lines
1.4 KiB
Java
public class Point {
|
|
private int x;
|
|
private int y;
|
|
|
|
public Point (int x, int y) {
|
|
this.setX(x);
|
|
this.setY(y);
|
|
}
|
|
public Point (String str) {
|
|
if (Point.isValidSyntax(str)) {
|
|
this.setX(str.charAt(0));
|
|
this.setY(Integer.parseInt(str.substring(1)));
|
|
} else {
|
|
throw new IllegalArgumentException("String ist keine gültige Koordinate");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return (char) ('A' + this.x) + String.valueOf(this.y);
|
|
}
|
|
|
|
public int getX() {
|
|
return x;
|
|
}
|
|
|
|
public int getY() {
|
|
return y;
|
|
}
|
|
|
|
public void setX(int x) {
|
|
this.x = x;
|
|
}
|
|
|
|
public void setY(int y) {
|
|
this.y = y;
|
|
}
|
|
public void setX(char c) {
|
|
this.x = c - 'A';
|
|
}
|
|
|
|
public static boolean isValidSyntax(String str) {
|
|
return str.matches("^[A-Z]\\d+$");
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (o == null || this.getClass() != o.getClass()) return false;
|
|
|
|
Point p = (Point)o;
|
|
return p.getX() == this.getX() && p.getY() == this.getY();
|
|
}
|
|
|
|
public boolean neighbours(Point other) {
|
|
if (other == null) return false;
|
|
return (int)Math.abs(this.getX() - other.getX()) <= 1 && (int)Math.abs(this.getY() - other.getY()) <= 1;
|
|
}
|
|
}
|