-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoordinate.java
More file actions
75 lines (63 loc) · 2.27 KB
/
Coordinate.java
File metadata and controls
75 lines (63 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Objects;
public final class Coordinate {
private final int row;
private final int column;
public Coordinate(int row, int column) {
if (row < 0 || column < 0) {
throw new IllegalArgumentException("Row and column must be >= 0.");
}
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public static Coordinate parse(String input, int boardSize) {
if (input == null) {
throw new IllegalArgumentException("Coordinate input must not be null.");
}
String normalized = input.trim().toUpperCase();
if (normalized.matches("^[A-Z]\\d+$")) {
int row = normalized.charAt(0) - 'A';
int column = Integer.parseInt(normalized.substring(1)) - 1;
return validateBounds(new Coordinate(row, column), boardSize);
}
if (normalized.matches("^\\d+\\s*,\\s*\\d+$")) {
String[] parts = normalized.split("\\s*,\\s*");
int row = Integer.parseInt(parts[0]) - 1;
int column = Integer.parseInt(parts[1]) - 1;
return validateBounds(new Coordinate(row, column), boardSize);
}
throw new IllegalArgumentException("Invalid coordinate format. Use A1 or 1,1.");
}
public static Coordinate validateBounds(Coordinate coordinate, int boardSize) {
if (boardSize < 2 || boardSize > 26) {
throw new IllegalArgumentException("Board size must be between 2 and 26.");
}
if (coordinate.row >= boardSize || coordinate.column >= boardSize) {
throw new IllegalArgumentException("Coordinate is outside the board.");
}
return coordinate;
}
public String toHumanReadable() {
return String.valueOf((char) ('A' + row)) + (column + 1);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Coordinate)) {
return false;
}
Coordinate that = (Coordinate) other;
return row == that.row && column == that.column;
}
@Override
public int hashCode() {
return Objects.hash(row, column);
}
}