-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMove.java
More file actions
196 lines (162 loc) · 6.78 KB
/
Move.java
File metadata and controls
196 lines (162 loc) · 6.78 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package org.lichess.compression.game;
final class Move implements Comparable<Move> {
public static final int NORMAL = 0;
public static final int EN_PASSANT = 1;
public static final int CASTLING = 2;
public int type;
public Role role;
public int from;
public boolean capture;
public int to;
public Role promotion;
private int score;
void set(Board board, int type, Role role, int from, boolean capture, int to, Role promotion) {
// Overwrite the current move. This is like a constructor, but reuses
// an existing object.
this.type = type;
this.role = role;
this.from = from;
this.capture = capture;
this.to = to;
this.promotion = promotion;
// Assign higher scores to moves that are more likely to be played.
//
// Scores must be unique for every move in the position, because
// move ordering should never depend on implementation details of the
// generator.
long defendingPawns =
Bitboard.pawnAttacks(board.turn, to) &
board.pawns &
board.them();
int moveValue = pieceValue(board, role, to) - pieceValue(board, role, from);
this.score =
(promotion == null ? 0 : promotion.index << 26) +
(capture ? 1 << 25 : 0) +
((defendingPawns == 0 ? 6 : (5 - role.index)) << 22) +
(512 + moveValue << 12) +
(to << 6) +
from;
}
public int compareTo(Move other) {
return other.score - this.score;
}
public String san(MoveList legals) {
switch (this.type) {
case Move.NORMAL:
case Move.EN_PASSANT:
StringBuilder builder = new StringBuilder(6);
builder.append(this.role.symbol);
// From.
if (role != Role.PAWN) {
boolean file = false, rank = false;
long others = 0;
for (int i = 0; i < legals.size(); i++) {
Move other = legals.get(i);
if (other.role == this.role && other.to == this.to && other.from != this.from) {
others |= 1L << other.from;
}
}
if (others != 0) {
if ((others & Bitboard.RANKS[Square.rank(this.from)]) != 0) file = true;
if ((others & Bitboard.FILES[Square.file(this.from)]) != 0) rank = true;
else file = true;
}
if (file) builder.append((char) (Square.file(this.from) + 'a'));
if (rank) builder.append((char) (Square.rank(this.from) + '1'));
} else if (this.capture) {
builder.append((char) (Square.file(this.from) + 'a'));
}
// Capture.
if (this.capture) builder.append('x');
// To.
builder.append((char) (Square.file(this.to) + 'a'));
builder.append((char) (Square.rank(this.to) + '1'));
// Promotion.
if (this.promotion != null) {
builder.append('=');
builder.append(this.promotion.symbol);
}
return builder.toString();
case Move.CASTLING:
return this.from < this.to ? "O-O" : "O-O-O";
}
return "--";
}
public String uci() {
int to = this.to;
// Select the king target square instead.
if (this.type == CASTLING) {
to = Square.combine(this.to < this.from ? Square.C1 : Square.G1, this.from);
}
StringBuilder builder = new StringBuilder(this.promotion == null ? 4 : 5);
builder.append((char) (Square.file(this.from) + 'a'));
builder.append((char) (Square.rank(this.from) + '1'));
builder.append((char) (Square.file(to) + 'a'));
builder.append((char) (Square.rank(to) + '1'));
if (this.promotion != null) builder.append(this.promotion.symbol.toLowerCase());
return builder.toString();
}
public boolean isZeroing() {
return this.capture || this.role == Role.PAWN;
}
public boolean isIrreversible() {
return this.isZeroing() || this.type == CASTLING;
}
// Piece-Square table with some manual tweaks (breaking symmetry).
//
// Original table taken from:
// https://github.com/flok99/feeks/blob/f02e4897555ac08497a5fea43f241bad30f2ecff/psq.py#L8-L67
private static int pieceValue(Board board, Role role, int square) {
return PSQT[role.index][board.turn ? Square.mirror(square) : square];
}
private static final int PSQT[][] = {
{ 0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 21, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-31,-31, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0 },
{ -50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 11, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50 },
{ -20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20 },
{ 0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0 },
{ -20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20 },
{ -30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-20,-30,-30,-40,-40,-30,-30,-20,
-10,-20,-20,-20,-20,-20,-20,-10,
20, 20, 0, 0, 0, 0, 20, 20,
0, 30, 10, 0, 0, 10, 30, 0 }
};
}