-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
289 lines (232 loc) · 7.65 KB
/
Copy pathsearch.cpp
File metadata and controls
289 lines (232 loc) · 7.65 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <chrono>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <random>
#include "board.h"
#include "transpositionTable.h"
HashEntry transTable[TABLE_SIZE];
U64 pieceArr[12][64];
U64 castleArr[16];
U64 epArr[8];
U64 blackMove;
const int MATE_SCORE = -1000000;
const int DRAW_SCORE = 0;
const int MAX_PLY = 128;
const int INF = 1000000000;
const int MAX_DEPTH = 64;
Move killerMoves[MAX_PLY][2];
int historyHeuristic[2][64][64];
// MVV-LVA: Most Valuable Victim - Least Valuable Attacker
// (Victim: P, N, B, R, Q, K) x (Attacker: P, N, B, R, Q, K)
int mvv_lva[6][6] = {
{0, 0, 0, 0, 0, 0}, // Victim K (invalid)
{50, 51, 52, 53, 54, 55}, // Victim Q
{40, 41, 42, 43, 44, 45}, // Victim R
{30, 31, 32, 33, 34, 35}, // Victim B
{20, 21, 22, 23, 24, 25}, // Victim N
{10, 11, 12, 13, 14, 15}, // Victim P
};
void Board::fillZobristArrs()
{
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution<uint64_t> distribution(0, UINT64_MAX);
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 64; j++) {
pieceArr[i][j] = distribution(generator);
}
}
castleArr[0] = 0;
for (int i = 1; i < 16; i++) {
castleArr[i] = distribution(generator);
}
for (int i = 0; i < 8; i++) {
epArr[i] = distribution(generator);
}
blackMove = distribution(generator);
std::memset(transTable, 0, sizeof(HashEntry) * TABLE_SIZE);
}
U64 Board::ZobristKey()
{
U64 key = 0;
for (int i = 0; i < 12; i++) {
U64 pieces = bb[i];
while (pieces) {
int pos = bitScanForwardWithReset(pieces);
key ^= pieceArr[i][pos];
}
}
if (!whiteTurn) {
key ^= blackMove;
}
key ^= castleArr[castlingRights];
if (enPassantSquare) {
key ^= epArr[enPassantSquare % 8];
}
return key;
}
void pickMove(MoveList& moveList, int startIndex) {
int bestIndex = startIndex;
for (int i = startIndex + 1; i < moveList.count; i++) {
if (moveList.scores[i] > moveList.scores[bestIndex]) {
bestIndex = i;
}
}
std::swap(moveList.moves[startIndex], moveList.moves[bestIndex]);
std::swap(moveList.scores[startIndex], moveList.scores[bestIndex]);
}
void Board::scoreMoves(MoveList& moveList, int ply, Move ttMove)
{
for (int i = 0; i < moveList.count; i++) {
Move move = moveList.moves[i];
int score = 0;
if (move == ttMove) {
score = 2000000;
}
else if (move.isCapture()) {
int victim = move.getCapturedPiece() >> 1;
int attacker = move.getPiece() >> 1;
score = 1000000 + mvv_lva[victim][attacker];
}
else if (move.isPromotion() && move.getSpecialMove() == QUEEN_PROM) {
score = 950000;
}
else {
if (killerMoves[ply][0] == move)
score = 900000;
else if (killerMoves[ply][1] == move)
score = 800000;
else
score = historyHeuristic[move.getPieceColor()][move.getTo()][move.getFrom()];
}
moveList.scores[i] = score;
}
}
int Board::QuiescenceSearch(int alpha, int beta)
{
int standPat = eval();
if (standPat >= beta) return beta;
int BIG_DELTA = 1200; // queen value
if (standPat < alpha - BIG_DELTA) {
return alpha;
}
if (alpha < standPat) alpha = standPat;
MoveList moveList(218);
generateLegalMoves(moveList, true);
scoreMoves(moveList, 0);
PosInfo posInfo(castlingRights, enPassantSquare, halfMoveClock);
for (int i = 0; i < moveList.count; i++) {
pickMove(moveList, i);
Move move = moveList.moves[i];
makeMove(move);
int score = -QuiescenceSearch(-beta, -alpha);
unMakeMove(move, posInfo);
if (score >= beta) return beta;
if (score > alpha) alpha = score;
}
return alpha;
}
int Board::alphaBeta(int alpha, int beta, int depthLeft, int ply)
{
if (ply >= MAX_PLY) return eval();
if (ply > 0 && (threeFoldRepetitionRule() || fiftyMoveRule())) {
return DRAW_SCORE;
}
U64 key = currentHash;
HashEntry* ttEntry = &transTable[key & (TABLE_SIZE - 1)];
bool ttHit = (ttEntry->key == key);
Move ttMove;
if (ttHit) {
ttMove = ttEntry->bestMove;
if (ttEntry->depth >= depthLeft && ply > 0) {
if (ttEntry->flags == HASH_EXACT) return ttEntry->score;
if (ttEntry->flags == HASH_ALPHA && ttEntry->score <= alpha) return alpha;
if (ttEntry->flags == HASH_BETA && ttEntry->score >= beta) return beta;
}
}
if (depthLeft <= 0) return QuiescenceSearch(alpha, beta);
MoveList moveList(218);
generateLegalMoves(moveList);
if (moveList.count == 0) {
if (isCheck()) return MATE_SCORE + ply;
return DRAW_SCORE;
}
scoreMoves(moveList, ply, (ttHit) ? ttMove: Move());
if (ttHit && !ttMove.isNone()) {
for (int i = 0; i < moveList.count; i++) {
if (moveList.moves[i] == ttMove) {
moveList.scores[i] = 2000000;
break;
}
}
}
PosInfo posInfo(castlingRights, enPassantSquare, halfMoveClock);
int bestScore = -INF;
int startAlpha = alpha;
Move bestMoveFound;
for (int i = 0; i < moveList.count; i++) {
pickMove(moveList, i);
Move move = moveList.moves[i];
makeMove(move);
int score;
if (i == 0) {
score = -alphaBeta(-beta, -alpha, depthLeft - 1, ply + 1);
}
else {
score = -alphaBeta(-alpha - 1, -alpha, depthLeft - 1, ply + 1);
if (score > alpha && score < beta) {
score = -alphaBeta(-beta, -alpha, depthLeft - 1, ply + 1);
}
}
unMakeMove(move, posInfo);
if (score > bestScore) {
bestScore = score;
bestMoveFound = move;
}
if (score >= beta) {
ttEntry->key = key;
ttEntry->score = beta;
ttEntry->depth = depthLeft;
ttEntry->flags = HASH_BETA;
ttEntry->bestMove = move;
if (!move.isCapture()) {
killerMoves[ply][1] = killerMoves[ply][0];
killerMoves[ply][0] = move;
historyHeuristic[move.getPieceColor()][move.getTo()][move.getFrom()] += depthLeft * depthLeft;
}
return beta;
}
if (score > alpha) alpha = score;
}
ttEntry->key = key;
ttEntry->score = alpha;
ttEntry->depth = depthLeft;
ttEntry->flags = (alpha > startAlpha) ? HASH_EXACT : HASH_ALPHA;
ttEntry->bestMove = bestMoveFound;
return alpha;
}
Move Board::searchPosition()
{
float timeLimit = 1500.0f;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Move bestMove;
int alpha = -INF;
int beta = INF;
std::memset(killerMoves, 0, sizeof(killerMoves));
std::memset(historyHeuristic, 0, sizeof(historyHeuristic));
for (int depth = 1; depth <= MAX_DEPTH; depth++) {
int score = alphaBeta(alpha, beta, depth, 0);
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count() >= timeLimit) {
break;
}
U64 key = currentHash;
HashEntry* ttEntry = &transTable[key & (TABLE_SIZE - 1)];
if (ttEntry->key == key && !ttEntry->bestMove.isNone()) {
bestMove = ttEntry->bestMove;
std::cout << "Depth: " << depth << " | Score: " << score << " | Best Move: " << bestMove.getStr() << "\n";
}
}
return bestMove;
}