Repository navigation
Expand file tree
/
Copy pathMove.java
More file actions
70 lines (61 loc) · 1.97 KB
/
Copy pathMove.java
File metadata and controls
70 lines (61 loc) · 1.97 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
package engine;
public class Move {
public final int fromSq;
public final int toSq;
public final char promotion; // 'q', 'r', 'b', 'n' or ' ' for no promotion
public Move(int fromSq, int toSq) {
this.fromSq = fromSq;
this.toSq = toSq;
this.promotion = ' ';
}
public Move(int fromSq, int toSq, char promotion) {
this.fromSq = fromSq;
this.toSq = toSq;
this.promotion = promotion;
}
/**
* Creates a Move object from a standard UCI string (e.g., "e2e4", "a7a8q").
*/
public static Move fromString(String uciMove) {
if (uciMove == null || uciMove.length() < 4) {
return null;
}
int from = (uciMove.charAt(0) - 'a') + (uciMove.charAt(1) - '1') * 8;
int to = (uciMove.charAt(2) - 'a') + (uciMove.charAt(3) - '1') * 8;
char promo = ' ';
if (uciMove.length() == 5) {
promo = Character.toLowerCase(uciMove.charAt(4));
}
return new Move(from, to, promo);
}
/**
* Converts the move back into a standard UCI-compliant string.
*/
@Override
public String toString() {
String moveStr = squareToCoord(fromSq) + squareToCoord(toSq);
if (promotion != ' ') {
moveStr += promotion;
}
return moveStr;
}
private static String squareToCoord(int sq) {
char file = (char) ('a' + (sq % 8));
char rank = (char) ('1' + (sq / 8));
return "" + file + rank;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Move move = (Move) obj;
return fromSq == move.fromSq && toSq == move.toSq && promotion == move.promotion;
}
@Override
public int hashCode() {
int result = fromSq;
result = 31 * result + toSq;
result = 31 * result + promotion;
return result;
}
}