-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.cpp
More file actions
296 lines (252 loc) · 8.99 KB
/
evaluation.cpp
File metadata and controls
296 lines (252 loc) · 8.99 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
#include "position.h"
////////////////
// evaluation //
////////////////
// e, p, n, b, r, q, k, P, N, B, R, Q, K
int piece_values[] = { 0, -100, -350, -350, -525, -1000, -10000, 100, 350, 350, 525, 1000, 10000 };
int pawn_table[] = {
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, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0,
};
int knight_table[] = {
-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, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50,
};
int bishop_table[] = {
-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,
};
int rook_table[] = {
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,
};
int queen_table[] = {
-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,
};
int king_table[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 5, 5, 5, 5, 0, 0,
0, 5, 5, 10, 10, 5, 5, 0,
0, 5, 10, 20, 20, 10, 5, 0,
0, 5, 10, 20, 20, 10, 5, 0,
0, 0, 5, 10, 10, 5, 0, 0,
0, 5, 5, -5, -5, 0, 5, 0,
0, 0, 5, 0,-15, 0, 10, 0,
};
int mirror_square[] = {
56, 57, 58, 59, 60, 61, 62, 63,
48, 49, 50, 51, 52, 53, 54, 55,
40, 41, 42, 43, 44, 45, 46, 47,
32, 33, 34, 35, 36, 37, 38, 39,
24, 25, 26, 27, 28, 29, 30, 31,
16, 17, 18, 19, 20, 21, 22, 23,
8, 9, 10, 11, 12, 13, 14, 15,
0, 1, 2, 3, 4, 5, 6, 7,
};
// map square in 0..127 -> 0..63
inline int get_square_in_64(int square) {
return (square >> 4) * 8 + (square & 7);
}
// store least advanced pawn on each file for each side
// 0 and 9 files are used as a buffer to help with isolated
// and backward pawn calculations.
int pawn_rank[2][10];
// scores for pawn structures
#define doubled_pawn_penalty 10
#define isolated_pawn_penalty 20
#define backwards_pawn_penalty 8
#define passed_pawn_bonus 20
#define rook_semi_open_file_bonus 10
#define rook_open_file_bonus 15
#define rook_on_seventh_bonus 20
// evaluate white pawn structures
int eval_white_pawn(int square, int rank, int file) {
int score = 0;
// increment file (remember I used 0..9 files)
file += 1;
// if this pawn is not the least advanced one, its doubled
if (pawn_rank[white][file] > rank)
score -= doubled_pawn_penalty;
// if no friendly pawns on adjacent files its isolated
if ((pawn_rank[white][file - 1] == 0) && (pawn_rank[white][file + 1] == 0))
score -= isolated_pawn_penalty;
// if pawn not isolated it might be backwards
else if ((pawn_rank[white][file - 1] < rank) && (pawn_rank[white][file + 1] < rank))
score -= backwards_pawn_penalty;
// give bonus if pawn is passed
if ((pawn_rank[black][file - 1] >= rank) &&
(pawn_rank[black][file] >= rank) &&
(pawn_rank[black][file + 1] >= rank))
score += (7 - rank) * passed_pawn_bonus;
return score;
}
// evaluate white pawn structures
int eval_black_pawn(int square, int rank, int file) {
int score = 0;
// increment file (remember I used 0..9 files)
file += 1;
// if this pawn is not the least advanced one, its doubled
if (pawn_rank[black][file] < rank)
score -= doubled_pawn_penalty;
// if no friendly pawns on adjacent files its isolated
if ((pawn_rank[black][file - 1] == 7) && (pawn_rank[black][file + 1] == 7))
score -= isolated_pawn_penalty;
// if pawn not isolated it might be backwards
else if ((pawn_rank[black][file - 1] > rank) && (pawn_rank[black][file + 1] > rank))
score -= backwards_pawn_penalty;
// give bonus if pawn is passed
if ((pawn_rank[white][file - 1] <= rank) &&
(pawn_rank[white][file] <= rank) &&
(pawn_rank[white][file + 1] <= rank))
score += rank * passed_pawn_bonus;
return score;
}
// evaluate whites rook
int evaluate_white_rook(int rank, int file) {
int score = 0;
// check if there is black pawn on same file as the rook
if (pawn_rank[black][file + 1] == 7) {
// check if there is also white pawn on same file as the rook
if (pawn_rank[white][file + 1] == 0)
score += rook_open_file_bonus;
else
score += rook_semi_open_file_bonus;
}
// check if rook is on seventh rank
if (rank == 1)
score += rook_on_seventh_bonus;
return score;
}
// evaluate blacks rook
int evaluate_black_rook(int rank, int file) {
int score = 0;
// check if there is white pawn on same file as the rook
if (pawn_rank[white][file + 1] == 0) {
// check if there is also black pawn on same file as the rook
if (pawn_rank[black][file + 1] == 7)
score += rook_open_file_bonus;
else
score += rook_semi_open_file_bonus;
}
// check if rook is on second rank
if (rank == 6)
score += rook_on_seventh_bonus;
return score;
}
/* evaluate position based on material, piece-square tables and
basic pawn structures */
int evaluate_position(Position &pos) {
int file, rank;
int score = 0;
int square;
int piece;
/* first store least advanced pawns on every file */
// init each file to max advancement for each side
for (int i = 0; i < 10; i++) {
pawn_rank[white][i] = 0;
pawn_rank[black][i] = 7;
}
// store the pawns
for (rank = 1; rank < 7; rank++) {
for (file = 0; file < 8; file++) {
square = rank * 16 + file;
piece = pos.board[square];
if (piece == e)
continue;
if (piece == P && pawn_rank[white][file + 1] < rank)
pawn_rank[white][file + 1] = rank;
else if (piece == p && pawn_rank[black][file + 1] > rank)
pawn_rank[black][file + 1] = rank;
}
}
/* evaluate each pawn or piece */
for (rank = 0; rank < 8; rank++) {
for (file = 0; file < 8; file++) {
// square as 0..127
square = rank * 16 + file;
if (pos.board[square] != e) {
piece = pos.board[square];
// sum the material
score += piece_values[piece];
switch (piece) {
case P:
score += pawn_table[get_square_in_64(square)];
score += eval_white_pawn(square, rank, file);
//cout << score << endl;
break;
case N:
score += knight_table[get_square_in_64(square)];
break;
case B:
score += bishop_table[get_square_in_64(square)];
break;
case R:
score += rook_table[get_square_in_64(square)];
score += evaluate_white_rook(rank, file);
break;
case Q:
score += queen_table[get_square_in_64(square)];
break;
case K:
score += king_table[get_square_in_64(square)];
break;
case p:
score -= pawn_table[mirror_square[get_square_in_64(square)]];
score -= eval_black_pawn(square, rank, file);
//cout << score << endl;
break;
case n:
score -= knight_table[mirror_square[get_square_in_64(square)]];
break;
case b:
score -= bishop_table[mirror_square[get_square_in_64(square)]];
break;
case r:
score -= rook_table[mirror_square[get_square_in_64(square)]];
score -= evaluate_black_rook(rank, file);
break;
case q:
score -= queen_table[mirror_square[get_square_in_64(square)]];
break;
case k:
score -= king_table[mirror_square[get_square_in_64(square)]];
break;
default:
break;
}
}
}
}
return pos.side == white ? score : -score;
}