-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.cpp
More file actions
308 lines (265 loc) · 10.7 KB
/
Copy pathevaluation.cpp
File metadata and controls
308 lines (265 loc) · 10.7 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#pragma once
#include "board.h"
#include <algorithm>
#define OTHER(side) ((side) ^ 1)
#define PCOLOR(p) ((p) & 1)
#define FLIP(sq) ((sq) ^ 56)
const U64 FileABB = 0x0101010101010101ULL;
const U64 FileHBB = 0x8080808080808080ULL;
inline U64 fileMask(int sq) { return FileABB << (sq & 7); }
// Returns the file of the square and the two adjacent files
inline U64 adjacentFiles(int sq) {
U64 f = fileMask(sq);
return ((f & ~FileABB) >> 1) | ((f & ~FileHBB) << 1);
}
int mg_pawn_table[64] = {
0, 0, 0, 0, 0, 0, 0, 0,
98, 134, 61, 95, 68, 126, 34, -11,
-6, 7, 26, 31, 65, 56, 25, -20,
-14, 13, 6, 21, 23, 12, 17, -23,
-27, -2, -5, 12, 17, 6, 10, -25,
-26, -4, -4, -10, 3, 3, 33, -12,
-35, -1, -20, -23, -15, 24, 38, -22,
0, 0, 0, 0, 0, 0, 0, 0,
};
int eg_pawn_table[64] = {
0, 0, 0, 0, 0, 0, 0, 0,
178, 173, 158, 134, 147, 132, 165, 187,
94, 100, 85, 67, 56, 53, 82, 84,
32, 24, 13, 5, -2, 4, 17, 17,
13, 9, -3, -7, -7, -8, 3, -1,
4, 7, -6, 1, 0, -5, -1, -8,
13, 8, 8, 10, 13, 0, 2, -7,
0, 0, 0, 0, 0, 0, 0, 0,
};
int mg_knight_table[64] = {
-167, -89, -34, -49, 61, -97, -15, -107,
-73, -41, 72, 36, 23, 62, 7, -17,
-47, 60, 37, 65, 84, 129, 73, 44,
-9, 17, 19, 53, 37, 69, 18, 22,
-13, 4, 16, 13, 28, 19, 21, -8,
-23, -9, 12, 10, 19, 17, 25, -16,
-29, -53, -12, -3, -1, 18, -14, -19,
-105, -21, -58, -33, -17, -28, -19, -23,
};
int eg_knight_table[64] = {
-58, -38, -13, -28, -31, -27, -63, -99,
-25, -8, -25, -2, -9, -25, -24, -52,
-24, -20, 10, 9, -1, -9, -19, -41,
-17, 3, 22, 22, 22, 11, 8, -18,
-18, -6, 16, 25, 16, 17, 4, -18,
-23, -3, -1, 15, 10, -3, -20, -22,
-42, -20, -10, -5, -2, -20, -23, -44,
-29, -51, -23, -15, -22, -18, -50, -64,
};
int mg_bishop_table[64] = {
-29, 4, -82, -37, -25, -42, 7, -8,
-26, 16, -18, -13, 30, 59, 18, -47,
-16, 37, 43, 40, 35, 50, 37, -2,
-4, 5, 19, 50, 37, 37, 7, -2,
-6, 13, 13, 26, 34, 12, 10, 4,
0, 15, 15, 15, 14, 27, 18, 10,
4, 15, 16, 0, 7, 21, 33, 1,
-33, -3, -14, -21, -13, -12, -39, -21,
};
int eg_bishop_table[64] = {
-14, -21, -11, -8, -7, -9, -17, -24,
-8, -4, 7, -12, -3, -13, -4, -14,
2, -8, 0, -1, -2, 6, 0, 4,
-3, 9, 12, 9, 14, 10, 3, 2,
-6, 3, 13, 19, 7, 10, -3, -9,
-12, -3, 8, 10, 13, 3, -7, -15,
-14, -18, -7, -1, 4, -9, -15, -27,
-23, -9, -23, -5, -9, -16, -5, -17,
};
int mg_rook_table[64] = {
32, 42, 32, 51, 63, 9, 31, 43,
27, 32, 58, 62, 80, 67, 26, 44,
-5, 19, 26, 36, 17, 45, 61, 16,
-24, -11, 7, 26, 24, 35, -8, -20,
-36, -26, -12, -1, 9, -7, 6, -23,
-45, -25, -16, -17, 3, 0, -5, -33,
-44, -16, -20, -9, -1, 11, -6, -71,
-19, -13, 1, 17, 16, 7, -37, -26,
};
int eg_rook_table[64] = {
13, 10, 18, 15, 12, 12, 8, 5,
11, 13, 13, 11, -3, 3, 8, 3,
7, 7, 7, 5, 4, -3, -5, -3,
4, 3, 13, 1, 2, 1, -1, 2,
3, 5, 8, 4, -5, -6, -8, -11,
-4, 0, -5, -1, -7, -12, -8, -16,
-6, -6, 0, 2, -9, -9, -11, -3,
-9, 2, 3, -1, -5, -13, 4, -20,
};
int mg_queen_table[64] = {
-28, 0, 29, 12, 59, 44, 43, 45,
-24, -39, -5, 1, -16, 57, 28, 54,
-13, -17, 7, 8, 29, 56, 47, 57,
-27, -27, -16, -16, -1, 17, -2, 1,
-9, -26, -9, -10, -2, -4, 3, -3,
-14, 2, -11, -2, -5, 2, 14, 5,
-35, -8, 11, 2, 8, 15, -3, 1,
-1, -18, -9, 10, -15, -25, -31, -50,
};
int eg_queen_table[64] = {
-9, 22, 22, 27, 27, 19, 10, 20,
-17, 20, 32, 41, 58, 25, 30, 0,
-20, 6, 9, 49, 47, 35, 19, 9,
3, 22, 24, 45, 57, 40, 57, 36,
-18, 28, 19, 47, 31, 34, 39, 23,
-16, -27, 15, 6, 9, 17, 10, 5,
-22, -23, -30, -16, -16, -23, -36, -32,
-33, -28, -22, -43, -5, -32, -20, -41,
};
int mg_king_table[64] = {
-65, 23, 16, -15, -56, -34, 2, 13,
29, -1, -20, -7, -8, -4, -38, -29,
-9, 24, 2, -16, -20, 6, 22, -22,
-17, -20, -12, -27, -30, -25, -14, -36,
-49, -1, -27, -39, -46, -44, -33, -51,
-14, -14, -22, -46, -44, -30, -15, -27,
1, 7, -8, -64, -43, -16, 9, 8,
-15, 36, 12, -54, 8, -28, 24, 14,
};
int eg_king_table[64] = {
-74, -35, -18, -18, -11, 15, 4, -17,
-12, 17, 14, 17, 17, 38, 23, 11,
10, 17, 23, 15, 20, 45, 44, 13,
-8, 22, 24, 27, 26, 33, 26, 3,
-18, -4, 21, 24, 27, 23, 9, -11,
-19, -3, 11, 21, 23, 16, 7, -9,
-27, -11, 4, 13, 14, 4, -5, -17,
-53, -34, -21, -11, -28, -14, -24, -43
};
int mg_value[6] = { 0, 1025, 477, 365, 337, 82 };
int eg_value[6] = { 0, 936, 512, 297, 281, 94 };
int* mg_tables[6] = { mg_king_table, mg_queen_table, mg_rook_table, mg_bishop_table, mg_knight_table, mg_pawn_table };
int* eg_tables[6] = { eg_king_table, eg_queen_table, eg_rook_table, eg_bishop_table, eg_knight_table, eg_pawn_table };
int midGameTables[12][64];
int endGameTables[12][64];
int gamePhaseInc[12] = { 0,0,4,4,2,2,1,1,1,1,0,0 };
void Board::initTables()
{
for (int p = 0; p < 6; p++) {
for (int i = 0; i < 64; i++) {
midGameTables[p][i] = mg_value[p] + mg_tables[p][i];
midGameTables[p * 2 + 1][i] = mg_value[p] + mg_tables[p][FLIP(i)];
endGameTables[p][i] = eg_value[p] + eg_tables[p][i];
endGameTables[p * 2 + 1][i] = eg_value[p] + eg_tables[p][FLIP(i)];
}
}
}
const int ISOLATED_PAWN_PENALTY = -10;
const int DOUBLED_PAWN_PENALTY = -15;
const int PASSED_PAWN_BONUS[8] = { 0, 5, 10, 20, 35, 60, 100, 200 };
const int MOBILITY_BONUS = 5;
const int SEMI_OPEN_FILE_PENALTY = -10;
const int OPEN_FILE_PENALTY = -20;
const int BISHOP_PAIR_BONUS = 40;
const int ROOK_OPEN_FILE_BONUS = 25;
const int ROOK_SEMI_OPEN_FILE_BONUS = 15;
const int PAWN_SHIELD_BONUS = 10;
int Board::eval()
{
int midGame[2]{};
int endGame[2]{};
int gamePhase = 0;
U64 whitePawns = bb[wPawn];
U64 blackPawns = bb[bPawn];
U64 allPieces = occupied;
if (piecesCount[wBishop] >= 2) {
midGame[White] += BISHOP_PAIR_BONUS;
endGame[White] += BISHOP_PAIR_BONUS;
}
if (piecesCount[bBishop] >= 2) {
midGame[Black] += BISHOP_PAIR_BONUS;
endGame[Black] += BISHOP_PAIR_BONUS;
}
for (int i = 0; i < 12; i++) {
U64 pieces = bb[i];
int pieceType = i >> 1; // 0=K, 1=Q, 2=R, 3=B, 4=N, 5=P
int side = PCOLOR(i);
while (pieces) {
int pos = bitScanForwardWithReset(pieces);
midGame[side] += midGameTables[i][pos];
endGame[side] += endGameTables[i][pos];
gamePhase += gamePhaseInc[i];
int bonus = 0;
if (pieceType == 5) {
U64 myPawns = (side == White) ? whitePawns : blackPawns;
U64 oppPawns = (side == White) ? blackPawns : whitePawns;
U64 fMask = fileMask(pos);
U64 adjMask = adjacentFiles(pos);
if (popCount(myPawns & fMask) > 1) bonus += DOUBLED_PAWN_PENALTY;
if ((myPawns & adjMask) == 0) bonus += ISOLATED_PAWN_PENALTY;
U64 passedMask = (fMask | adjMask);
if (side == White) passedMask &= ((1ULL << pos) - 1);
else passedMask &= ~((1ULL << (pos + 1)) - 1);
if ((passedMask & oppPawns) == 0) {
int advancedRank = (side == White) ? (6 - (pos / 8)) : ((pos / 8) - 1);
if (advancedRank >= 0 && advancedRank < 8)
bonus += PASSED_PAWN_BONUS[advancedRank];
}
}
else if (pieceType == 2) {
U64 fMask = fileMask(pos);
U64 myPawns = (side == White) ? whitePawns : blackPawns;
if ((myPawns & fMask) == 0) {
bonus += ROOK_SEMI_OPEN_FILE_BONUS;
U64 oppPawns = (side == White) ? blackPawns : whitePawns;
if ((oppPawns & fMask) == 0) {
bonus += (ROOK_OPEN_FILE_BONUS - ROOK_SEMI_OPEN_FILE_BONUS);
}
}
U64 attacks = rookAttack(pos, allPieces);
U64 friendly = (side == White) ? bb[Whites] : bb[Blacks];
attacks &= ~friendly;
bonus += popCount(attacks) * MOBILITY_BONUS;
}
else if (pieceType == 0) {
U64 myPawns = (side == White) ? whitePawns : blackPawns;
U64 kingZone = 0;
if (side == White) {
if (pos < 56) {
kingZone |= (1ULL << (pos + 8));
if ((pos % 8) != 0) kingZone |= (1ULL << (pos + 7));
if ((pos % 8) != 7) kingZone |= (1ULL << (pos + 9));
}
}
else {
if (pos > 7) {
kingZone |= (1ULL << (pos - 8));
if ((pos % 8) != 0) kingZone |= (1ULL << (pos - 9));
if ((pos % 8) != 7) kingZone |= (1ULL << (pos - 7));
}
}
bonus += popCount(kingZone & myPawns) * PAWN_SHIELD_BONUS;
U64 fMask = fileMask(pos);
U64 oppPawns = (side == White) ? blackPawns : whitePawns;
if ((myPawns & fMask) == 0)
bonus += (oppPawns & fMask) ? SEMI_OPEN_FILE_PENALTY : OPEN_FILE_PENALTY;
}
else {
U64 attacks = 0;
U64 friendly = (side == White) ? bb[Whites] : bb[Blacks];
if (pieceType == 4)
attacks = knightAttacks[pos];
else if (pieceType == 3)
attacks = bishopAttack(pos, allPieces);
else if (pieceType == 1)
attacks = bishopAttack(pos, allPieces) | rookAttack(pos, allPieces);
attacks &= ~friendly;
bonus += popCount(attacks) * MOBILITY_BONUS;
}
midGame[side] += bonus;
endGame[side] += bonus;
}
}
int midGameScore = midGame[whiteTurn] - midGame[OTHER(whiteTurn)];
int endGameScore = endGame[whiteTurn] - endGame[OTHER(whiteTurn)];
int midGamePhase = gamePhase;
if (midGamePhase > 24) midGamePhase = 24;
int endGamePhase = 24 - midGamePhase;
return (midGameScore * midGamePhase + endGameScore * endGamePhase) / 24;
}