-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eval.cpp
388 lines (335 loc) · 9.83 KB
/
Eval.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "Eval.h"
namespace Eval {
/* the values of the pieces */
const int piece_value[6] = {
100, 300, 300, 500, 900, 0
};
/* The "pcsq" arrays are piece/square tables. They're values
added to the material value of the piece based on the
location of the piece. */
const int pawn_pcsq[64] = {
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 15, 20, 20, 15, 10, 5,
4, 8, 12, 16, 16, 12, 8, 4,
3, 6, 9, 12, 12, 9, 6, 3,
2, 4, 6, 8, 8, 6, 4, 2,
1, 2, 3, -10, -10, 3, 2, 1,
0, 0, 0, -40, -40, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
const int knight_pcsq[64] = {
-10, -10, -10, -10, -10, -10, -10, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, -30, -10, -10, -10, -10, -30, -10
};
const int bishop_pcsq[64] = {
-10, -10, -10, -10, -10, -10, -10, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, -10, -20, -10, -10, -20, -10, -10
};
const int king_pcsq[64] = {
-40, -40, -40, -40, -40, -40, -40, -40,
-40, -40, -40, -40, -40, -40, -40, -40,
-40, -40, -40, -40, -40, -40, -40, -40,
-40, -40, -40, -40, -40, -40, -40, -40,
-40, -40, -40, -40, -40, -40, -40, -40,
-40, -40, -40, -40, -40, -40, -40, -40,
-20, -20, -20, -20, -20, -20, -20, -20,
0, 20, 40, -20, 0, -20, 40, 20
};
const int king_endgame_pcsq[64] = {
0, 10, 20, 30, 30, 20, 10, 0,
10, 20, 30, 40, 40, 30, 20, 10,
20, 30, 40, 50, 50, 40, 30, 20,
30, 40, 50, 60, 60, 50, 40, 30,
30, 40, 50, 60, 60, 50, 40, 30,
20, 30, 40, 50, 50, 40, 30, 20,
10, 20, 30, 40, 40, 30, 20, 10,
0, 10, 20, 30, 30, 20, 10, 0
};
/* The flip array is used to calculate the piece/square
values for black pieces. The piece/square value of a
white pawn is pawn_pcsq[sq] and the value of a black
pawn is pawn_pcsq[flip[sq]] */
const int flip[64] = {
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
};
/* pawn_rank[x][y] is the rank of the least advanced pawn of color x on file
y - 1. There are "buffer files" on the left and right to avoid special-case
logic later. If there's no pawn on a rank, we pretend the pawn is
impossibly far advanced (0 for white and 7 for black). This makes it easy to
test for pawns on a rank and it simplifies some pawn evaluation code. */
int pawn_rank[2][10];
int piece_mat[2]; /* the value of a side's pieces */
int pawn_mat[2]; /* the value of a side's pawns */
Position pos;
int eval(Position p)
{
// Copy p into global position to avoid passing position as an argument multiple times.
pos = p;
int i;
int f; /* file */
int score[2]; /* each side's score */
/* this is the first pass: set up pawn_rank, piece_mat, and pawn_mat. */
for (i = 0; i < 10; ++i) {
pawn_rank[white][i] = 7;
pawn_rank[black][i] = 0;
}
piece_mat[white] = 0;
piece_mat[black] = 0;
pawn_mat[white] = 0;
pawn_mat[black] = 0;
for (i = 0; i < 64; ++i) {
if (pos.squares[i].color == none)
continue;
if (pos.squares[i].ptype == pawn) {
pawn_mat[pos.squares[i].color] += piece_value[pawn];
f = COL(i) + 1; /* add 1 because of the extra file in the array */
if (pos.squares[i].color == white) {
if (pawn_rank[white][f] > ROW(i))
pawn_rank[white][f] = ROW(i);
}
else {
if (pawn_rank[black][f] < ROW(i))
pawn_rank[black][f] = ROW(i);
}
}
else
piece_mat[pos.squares[i].color] += piece_value[pos.squares[i].ptype];
}
/* this is the second pass: evaluate each piece */
score[white] = piece_mat[white] + pawn_mat[white];
score[black] = piece_mat[black] + pawn_mat[black];
for (i = 0; i < 64; ++i) {
if (pos.squares[i].color == none)
continue;
if (pos.squares[i].color == white) {
switch (pos.squares[i].ptype) {
case pawn:
score[white] += eval_white_pawn(i);
break;
case knight:
score[white] += knight_pcsq[flip[i]];
break;
case bishop:
score[white] += bishop_pcsq[flip[i]];
break;
case rook:
if (pawn_rank[white][COL(i) + 1] == 7) {
if (pawn_rank[black][COL(i) + 1] == 0)
score[white] += ROOK_OPEN_FILE_BONUS;
else
score[white] += ROOK_SEMI_OPEN_FILE_BONUS;
}
if (ROW(i) == 6)
score[white] += ROOK_ON_SEVENTH_BONUS;
break;
case king:
if (piece_mat[black] <= 1200)
score[white] += king_endgame_pcsq[flip[i]];
else
score[white] += eval_white_king(i);
break;
}
}
else {
switch (pos.squares[i].ptype) {
case pawn:
score[black] += eval_black_pawn(i);
break;
case knight:
score[black] += knight_pcsq[i];
break;
case bishop:
score[black] += bishop_pcsq[i];
break;
case rook:
if (pawn_rank[black][COL(i) + 1] == 0) {
if (pawn_rank[white][COL(i) + 1] == 7)
score[black] += ROOK_OPEN_FILE_BONUS;
else
score[black] += ROOK_SEMI_OPEN_FILE_BONUS;
}
if (ROW(i) == 1)
score[black] += ROOK_ON_SEVENTH_BONUS;
break;
case king:
if (piece_mat[white] <= 1200)
score[black] += king_endgame_pcsq[i];
else
score[black] += eval_black_king(i);
break;
}
}
}
/* the score[] array is set, now return the score relative
to the side to move */
if (pos.side == white)
return score[white] - score[black];
return score[black] - score[white];
}
int eval_white_pawn(int sq)
{
int r; /* the value to return */
int f; /* the pawn's file */
r = 0;
f = COL(sq) + 1;
r += pawn_pcsq[flip[sq]];
/* if there's a pawn behind this one, it's doubled */
if (pawn_rank[white][f] < ROW(sq))
r -= DOUBLED_PAWN_PENALTY;
/* if there aren't any friendly pawns on either side of
this one, it's isolated */
if ((pawn_rank[white][f - 1] == 7) &&
(pawn_rank[white][f + 1] == 7))
r -= ISOLATED_PAWN_PENALTY;
/* if it's not isolated, it might be backwards */
else if ((pawn_rank[white][f - 1] > ROW(sq)) &&
(pawn_rank[white][f + 1] > ROW(sq)))
r -= BACKWARDS_PAWN_PENALTY;
/* add a bonus if the pawn is passed */
if ((pawn_rank[black][f - 1] <= ROW(sq)) &&
(pawn_rank[black][f] <= ROW(sq)) &&
(pawn_rank[black][f + 1] <= ROW(sq)))
r += (ROW(sq)) * PASSED_PAWN_BONUS;
return r;
}
int eval_black_pawn(int sq)
{
int r; /* the value to return */
int f; /* the pawn's file */
r = 0;
f = COL(sq) + 1;
r += pawn_pcsq[sq];
/* if there's a pawn behind this one, it's doubled */
if (pawn_rank[black][f] > ROW(sq))
r -= DOUBLED_PAWN_PENALTY;
/* if there aren't any friendly pawns on either side of
this one, it's isolated */
if ((pawn_rank[black][f - 1] == 0) &&
(pawn_rank[black][f + 1] == 0))
r -= ISOLATED_PAWN_PENALTY;
/* if it's not isolated, it might be backwards */
else if ((pawn_rank[black][f - 1] < ROW(sq)) &&
(pawn_rank[black][f + 1] < ROW(sq)))
r -= BACKWARDS_PAWN_PENALTY;
/* add a bonus if the pawn is passed */
if ((pawn_rank[white][f - 1] >= ROW(sq)) &&
(pawn_rank[white][f] >= ROW(sq)) &&
(pawn_rank[white][f + 1] >= ROW(sq)))
r += (7 - ROW(sq)) * PASSED_PAWN_BONUS;
return r;
}
int eval_white_king(int sq)
{
int r; /* the value to return */
int i;
r = king_pcsq[flip[sq]];
/* if the king is castled, use a special function to evaluate the
pawns on the appropriate side */
if (COL(sq) < 3) {
r += eval_wkp(1);
r += eval_wkp(2);
r += eval_wkp(3) / 2; /* problems with pawns on the c & f files
are not as severe */
}
else if (COL(sq) > 4) {
r += eval_wkp(8);
r += eval_wkp(7);
r += eval_wkp(6) / 2;
}
/* otherwise, just assess a penalty if there are open files near
the king */
else {
for (i = COL(sq); i <= COL(sq) + 2; ++i)
if ((pawn_rank[white][i] == 7) &&
(pawn_rank[black][i] == 0))
r -= 10;
}
/* scale the king safety value according to the opponent's material;
the premise is that your king safety can only be bad if the
opponent has enough pieces to attack you */
r *= piece_mat[black];
r /= 3100;
return r;
}
/* eval_lkp(f) evaluates the white King Pawn on file f */
int eval_wkp(int f)
{
int r = 0;
if (pawn_rank[white][f] == 1); /* pawn hasn't moved */
else if (pawn_rank[white][f] == 2)
r -= 10; /* pawn moved one square */
else if (pawn_rank[white][f] != 7)
r -= 20; /* pawn moved more than one square */
else
r -= 25; /* no pawn on this file */
if (pawn_rank[black][f] == 0)
r -= 15; /* no enemy pawn */
else if (pawn_rank[black][f] == 2)
r -= 10; /* enemy pawn on the 3rd rank */
else if (pawn_rank[black][f] == 3)
r -= 5; /* enemy pawn on the 4th rank */
return r;
}
int eval_black_king(int sq)
{
int r;
int i;
r = king_pcsq[sq];
if (COL(sq) < 3) {
r += eval_bkp(1);
r += eval_bkp(2);
r += eval_bkp(3) / 2;
}
else if (COL(sq) > 4) {
r += eval_bkp(8);
r += eval_bkp(7);
r += eval_bkp(6) / 2;
}
else {
for (i = COL(sq); i <= COL(sq) + 2; ++i)
if ((pawn_rank[white][i] == 7) &&
(pawn_rank[black][i] == 0))
r -= 10;
}
r *= piece_mat[white];
r /= 3100;
return r;
}
int eval_bkp(int f)
{
int r = 0;
if (pawn_rank[black][f] == 6);
else if (pawn_rank[black][f] == 5)
r -= 10;
else if (pawn_rank[black][f] != 0)
r -= 20;
else
r -= 25;
if (pawn_rank[white][f] == 7)
r -= 15;
else if (pawn_rank[white][f] == 5)
r -= 10;
else if (pawn_rank[white][f] == 4)
r -= 5;
return r;
}
} // namespace Eval