-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovepick.cpp
More file actions
185 lines (156 loc) · 6.3 KB
/
movepick.cpp
File metadata and controls
185 lines (156 loc) · 6.3 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
#include "movepick.hpp"
namespace movepick
{
// 1. SEE
// Return type changed to chess::Square for consistency
chess::Square select_least_valuable(chess::Board &board, chess::Bitboard bb)
{
if (bb.empty())
return chess::Square::NO_SQ; // Use NO_SQ sentinel for no square
int least_value = 5000;
chess::Square least_valuable_square = chess::Square::NO_SQ;
while (bb)
{
int idx = bb.pop(); // idx is int index of bitboard square
chess::Square sq(idx);
int value = piece_value(board.at<chess::PieceType>(sq));
if (value < least_value)
{
least_value = value;
least_valuable_square = sq;
}
}
return least_valuable_square;
}
int SEE(chess::Board &board, chess::Move move)
{
constexpr int max_gain_size = MAX_PLY + 1; // To avoid overflow in gain[d + 1]
std::array<int, max_gain_size> gain{};
int d = 0;
chess::Color side = board.sideToMove();
chess::Bitboard occ = board.occ();
chess::Square target_sq = move.to();
// Initial gain setup:
if (move.typeOf() & chess::Move::PROMOTION)
{
gain[0] = piece_value(move.promotionType());
if (board.isCapture(move))
gain[0] += piece_value(board.at<chess::PieceType>(target_sq));
}
else if (move.typeOf() == chess::Move::ENPASSANT)
{
// We'll treat it normally below, so just assign captured pawn value now:
gain[0] = piece_value(chess::PieceType::PAWN);
}
else
{
gain[0] = piece_value(board.at<chess::PieceType>(target_sq));
}
chess::Square from_sq = move.from();
// Remove the moving piece from occupancy:
occ.clear(from_sq.index());
// Special case for en passant: remove the captured pawn square here:
if (move.typeOf() == chess::Move::ENPASSANT)
{
chess::Square ep_captured_sq(target_sq.file(), from_sq.rank()); // square behind target
occ.clear(ep_captured_sq.index());
}
chess::Square next_attacker_sq = from_sq;
while (++d < MAX_PLY)
{
side = ~side;
// Find all attackers of target square from side to move:
chess::Bitboard attackers = chess::attacks::attackers(board, side, target_sq) & occ;
// Include x-ray attacks for sliding pieces:
chess::Bitboard rook_attacks = chess::attacks::rook(target_sq, occ);
chess::Bitboard bishop_attacks = chess::attacks::bishop(target_sq, occ);
attackers |= rook_attacks & (board.pieces(chess::PieceType::ROOK, side) | board.pieces(chess::PieceType::QUEEN, side));
attackers |= bishop_attacks & (board.pieces(chess::PieceType::BISHOP, side) | board.pieces(chess::PieceType::QUEEN, side));
if (attackers.empty())
break;
next_attacker_sq = select_least_valuable(board, attackers);
if (next_attacker_sq == chess::Square::NO_SQ)
break;
int captured_value = piece_value(board.at<chess::PieceType>(next_attacker_sq));
gain[d] = -gain[d - 1] + captured_value;
if (std::max(-gain[d - 1], gain[d]) < 0)
break;
occ.clear(next_attacker_sq.index());
}
while (--d > 0)
{
gain[d] = std::max(-gain[d + 1], gain[d]);
}
return gain[0];
}
// 2. Killer moves
chess::Move killerMoves[MAX_PLY][2] = {};
void updateKillerMoves(chess::Move m, int ply)
{
if (killerMoves[ply][0] != m)
{
killerMoves[ply][1] = killerMoves[ply][0];
killerMoves[ply][0] = m;
}
}
// 3. History heuristic
int historyHeuristic[64][64] = {}; // from-square to to-square
void updateHistoryHeuristic(chess::Move m, int depth)
{
historyHeuristic[m.from().index()][m.to().index()] += depth * depth;
}
void orderMoves(chess::Board &board, chess::Movelist &moves, chess::Move ttMove, chess::Move pvMove, int ply)
{
for (auto &move : moves)
{
if (move == ttMove)
move.setScore(10000);
else if (move == pvMove)
move.setScore(9000);
else if (board.isCapture(move))
move.setScore(SEE(board, move));
else if (move == killerMoves[ply][0])
move.setScore(8500);
else if (move == killerMoves[ply][1])
move.setScore(8000);
else
move.setScore(historyHeuristic[move.from().index()][move.to().index()]);
}
// Sort moves by score in descending order
std::stable_sort(moves.begin(), moves.end(),
[](const chess::Move &a, const chess::Move &b)
{ return a.score() > b.score(); });
}
void qOrderMoves(chess::Board &board, chess::Movelist &moves)
{
// clang-format off
int16_t promotion_table[4][8] = {
{-58, -38, -13, -28, -31, -27, -63, -99}, // N
{-14, -21, -11, -8, -7, -9, -17, -24}, // B
{ 13, 10, 18, 15, 12, 12, 8, 5}, // R
{ -9, 22, 22, 27, 27, 19, 10, 20}, // Q
};
// clang-format on
for (auto &move : moves)
{
if (board.isCapture(move)) // Simple MVV-LVA
{
move.setScore(SEE(board, move));
}
else if (move.typeOf() & chess::Move::PROMOTION)
{
int promoValue = piece_value(move.promotionType());
int captureValue = board.isCapture(move) ? piece_value(board.at<chess::PieceType>(move.to())) : 0;
move.setScore(promoValue + captureValue + promotion_table[move.promotionType() - 1][move.to().file()]);
}
else
{
move.setScore(historyHeuristic[move.from().index()][move.to().index()]);
}
}
// Sort moves by score in descending order
std::stable_sort(moves.begin(), moves.end(),
[](const chess::Move &a, const chess::Move &b)
{ return a.score() > b.score(); });
}
}