-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.cpp
319 lines (284 loc) · 11.2 KB
/
evaluate.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
//
// evaluate.cpp
// Chess Engine
//
// Created by Blake Johnson on 2/21/19.
// Copyright © 2019 Blake Johnson. All rights reserved.
//
#include "evaluate.hpp"
value pieceValue[numOfPieceType] = {draw, pawnVal, knightVal, bishopVal, rookVal, queenVal};
square oppSq[numOfSq];
int gamePhaseCutoff;
int pSqTble[numOfPieceType][numOfSq] =
{
/*
{ a1, b1, c1, d1, e1, f1, g1, h1 },
{ a2, b2, c2, d2, e2, f2, g2, h2 },
{ a3, b3, c3, d3, e3, f3, g3, h3 },
{ a4, b4, c4, d4, e4, f4, g4, h4 },
{ a5, b5, c5, d5, e5, f5, g5, h5 },
{ a6, b6, c6, d6, e6, f6, g6, h6 },
{ a7, b7, c7, d7, e7, f7, g7, h7 },
{ a8, b8, c8, d8, e8, f8, g8, h8 },
*/
{//Pawns
0, 0, 0, 0, 0, 0, 0, 0 ,
5, 5, 10, -50, -50, -25, 5, 0 ,
5, 0, 0, 35, 45, 25, 0, 3 ,
0, 0, 0, 50, 50, 45, 0, 0 ,
5, 5, 5, 30, 30, 15, 5, 5 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
50, 50, 50, 50, 50, 50, 50, 50 ,
0, 0, 0, 0, 0, 0, 0, 0
},
{//Knights
-50, -40, -30, -30, -30, -30, -40, -50 ,
-40, -30, 0, 5, 5, 0, -40, -40 ,
-40, 10, 35, 30, 30, 35, 10, -40 ,
-10, 10, 30, 50, 50, 30, 0, -40 ,
-10, 25, 30, 50, 50, 30, 0, -40 ,
20, 35, 35, 35, 35, 35, 0, -40 ,
10, 10, 10, 10, 10, 10, -30, -40 ,
-20, -20, -20, -20, -20, -20, -40, -50
},
{//Bishops
-10, -50, -50, -50, -50, -50, -10, 0 ,
-50, 30, 0, 30, 30, 30, 35, 0 ,
-50, 30, 40, 20, 20, 40, 10, -30 ,
-50, 10, 40, 50, 50, 40, 10, -40 ,
-50, 10, 40, 50, 50, 40, 10, -40 ,
-50, 30, 50, 40, 40, 30, 10, -40 ,
30, 30, 40, 20, 40, 20, 20, -40 ,
-50, 0, 0, -30, -30, -30, -30, -10
},
{//Rooks
0, 0, 50, 50, 50, 80, 0, 0 ,
0, 0, 0, 30, 30, 0, 0, 0 ,
0, 0, 0, 20, 20, 0, 0, 0 ,
0, 0, 0, 20, 20, 0, 0, 0 ,
0, 0, 0, 20, 20, 0, 0, 0 ,
0, 0, 0, 20, 20, 0, 0, 0 ,
50, 50, 50, 50, 50, 50, 50, 50 ,
0, 0, 0, 40, 40, 0, 0, 0
},
{//Queens
0, 0, 0, 0, 0, 0, -10, -20 ,
0, 0, 0, 30, 30, 30, -20, -20 ,
10, 10, 10, 40, 40, 40, 0, -20 ,
20, 40, 50, 60, 60, 40, 0, 0 ,
20, 40, 60, 60, 60, 40, 0, 0 ,
20, 40, 60, 50, 50, 40, 0, 0 ,
20, 40, 40, 50, 50, 0, 0, 0 ,
0, 0, 50, 50, 50, 0, 0, 0
},
{//Kings
/*
10, 50, -50, -50, -50, -50, 40, 0 ,
5, -30, -30, -30, -30, -30, -30, -30 ,
-40, -40, -40, -40, -40, -40, -40, -40 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
*/
10, 50, 50, -50, -50, -50, 50, 0 ,
5, -10, -10, -10, -10, -10, -10, -10 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
10, 10, 10, 10, 10, 10, 10, 10 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
}
};
int pSqTbleEndGame[numOfPieceType][numOfSq] =
{
/*
{ a1, b1, c1, d1, e1, f1, g1, h1 },
{ a2, b2, c2, d2, e2, f2, g2, h2 },
{ a3, b3, c3, d3, e3, f3, g3, h3 },
{ a4, b4, c4, d4, e4, f4, g4, h4 },
{ a5, b5, c5, d5, e5, f5, g5, h5 },
{ a6, b6, c6, d6, e6, f6, g6, h6 },
{ a7, b7, c7, d7, e7, f7, g7, h7 },
{ a8, b8, c8, d8, e8, f8, g8, h8 },
*/
{//Pawns
0, 0, 0, 0, 0, 0, 0, 0 ,
5, 5, 10, -50, -50, -25, 5, 0 ,
5, 0, 0, 35, 45, 25, 0, 3 ,
0, 0, 0, 50, 50, 45, 0, 0 ,
5, 5, 5, 30, 30, 15, 5, 5 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
50, 50, 50, 50, 50, 50, 50, 50 ,
0, 0, 0, 0, 0, 0, 0, 0
},
{//Knights
-50, -40, -30, -30, -30, -30, -40, -50 ,
-40, -30, 0, 5, 5, 0, -40, -40 ,
-40, 10, 35, 30, 30, 35, 10, -40 ,
-10, 10, 30, 50, 50, 30, 0, -40 ,
-10, 25, 30, 50, 50, 30, 0, -40 ,
20, 35, 35, 35, 35, 35, 0, -40 ,
10, 10, 10, 10, 10, 10, -30, -40 ,
-20, -20, -20, -20, -20, -20, -40, -50
},
{//Bishops
-10, -50, -50, -50, -50, -50, -10, 0 ,
-50, 30, 30, 30, 30, 30, 35, 0 ,
-50, 30, 40, 20, 20, 40, 10, -30 ,
-50, 10, 40, 50, 50, 40, 10, -40 ,
-50, 10, 40, 50, 50, 40, 10, -40 ,
-50, 30, 50, 40, 40, 30, 10, -40 ,
30, 30, 40, 20, 40, 20, 20, -40 ,
-50, 0, 0, -30, -30, -30, -30, -10
},
{//Rooks
0, 0, 50, 50, 50, 80, 0, 0 ,
0, 0, 0, 30, 30, 0, 0, 0 ,
0, 0, 0, 20, 20, 0, 0, 0 ,
0, 0, 0, 20, 20, 0, 0, 0 ,
0, 0, 0, 20, 20, 0, 0, 0 ,
0, 0, 0, 20, 20, 0, 0, 0 ,
50, 50, 50, 50, 50, 50, 50, 50 ,
0, 0, 0, 40, 40, 0, 0, 0
},
{//Queens
0, 0, 0, 0, 0, 0, 0, 0 ,
0, 0, 0, 30, 30, 30, -20, -10 ,
10, 10, 10, 40, 40, 40, 20, 10 ,
20, 40, 50, 60, 60, 40, 40, 20 ,
20, 40, 60, 60, 60, 40, 40, 20 ,
20, 40, 60, 50, 50, 40, 40, 20 ,
20, 40, 60, 50, 50, 60, 40, 20 ,
0, 0, 50, 50, 50, 50, 0, 0
},
{//Kings
/*
10, 50, -50, -50, -50, -50, 40, 0 ,
5, -30, -30, -30, -30, -30, -30, -30 ,
-40, -40, -40, -40, -40, -40, -40, -40 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
-50, -50, -50, -50, -50, -50, -50, -50 ,
*/
0, 0, 0, 0, 0, 0, 0, 0 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
5, 5, 5, 5, 5, 5, 5, 5 ,
5, 5, 5, 5, 5, 5, 5, 5 ,
10, 20, 20, 30, 30, 30, 30, 20 ,
30, 40, 40, 50, 50, 40, 40, 40 ,
10, 30, 30, 30, 30, 30, 30, 30 ,
0, 0, 0, 0, 0, 0, 0, 0 ,
}
};
void evaluation::init(){
for(square sq = a1; sq < numOfSq; sq++)
{
oppSq[sq] = square(sq ^ (black * 56));
}
int totalMaterial = (8 * pawnVal) + (2 * knightVal) + (2 * bishopVal) + (2 * rookVal) + queenVal;
gamePhaseCutoff = 2 * totalMaterial - 2 * (rookVal * 2);
}
value evaluate (position& pos)
{
value eval = pos.get_material(white) - pos.get_material(black);
int totalMaterial = pos.get_material(white) + pos.get_material(black);
gamePhase phase = (totalMaterial >= gamePhaseCutoff) ? middleGame : endGame;
//PIECE SQUARE TABLE EVALUATIONS
//////////////////////////////////////////////////////////
bitboard pieces = 0;
square sq = a1;
//white pieces
for(piece pt = w_knight; pt < w_king; pt ++)
{
pieces = pos.get_pieces(pt);
while(pieces)
{
sq = lsb_sq(pieces);
eval =value( eval + pSqTble[pt][sq]);
pop_lsb(&pieces);
};
}
if(phase == middleGame)
{
//w_pawn
pieces = pos.get_pieces(w_pawn);
while(pieces)
{
sq = lsb_sq(pieces);
eval =value( eval + pSqTble[w_pawn][sq]);
pop_lsb(&pieces);
};
//w_king
pieces = pos.get_pieces(w_king);
sq = lsb_sq(pieces);
eval =value( eval + pSqTble[w_king][sq]);
pop_lsb(&pieces);
}
else{
pieces = pos.get_pieces(w_pawn);
//w_pawn middleGame
while(pieces)
{
sq = lsb_sq(pieces);
eval =value( eval + pSqTbleEndGame[w_pawn][sq]);
pop_lsb(&pieces);
};
//w_king Endgame
pieces = pos.get_pieces(w_king);
sq = lsb_sq(pieces);
eval =value( eval + pSqTbleEndGame[w_king][sq]);
pop_lsb(&pieces);
}
//black pieces
//////////////////////////////////////////////////////////
for(piece pt = b_knight; pt < b_king; pt ++)
{
pieces = pos.get_pieces(pt);
while(pieces)
{
sq = lsb_sq(pieces);
eval =value( eval - pSqTble[piece_To_Piecetype(pt)][oppSq[sq]]);
pop_lsb(&pieces);
};
}
if(phase == middleGame)
{
//B_pawn
pieces = pos.get_pieces(b_pawn);
while(pieces)
{
sq = lsb_sq(pieces);
eval =value( eval - pSqTble[piece_To_Piecetype(b_pawn)][oppSq[sq]]);
pop_lsb(&pieces);
};
//B_king
pieces = pos.get_pieces(b_king);
sq = lsb_sq(pieces);
eval =value( eval - pSqTble[piece_To_Piecetype(b_king)][oppSq[sq]]);
pop_lsb(&pieces);
}
else{
pieces = pos.get_pieces(b_pawn);
//w_pawn EndGame
while(pieces)
{
sq = lsb_sq(pieces);
eval =value( eval - pSqTbleEndGame[piece_To_Piecetype(b_pawn)][oppSq[sq]]);
pop_lsb(&pieces);
};
//b_king Endgame
pieces = pos.get_pieces(b_king);
sq = lsb_sq(pieces);
eval =value( eval - pSqTbleEndGame[piece_To_Piecetype(b_king)][oppSq[sq]]);
pop_lsb(&pieces);
}
///////////////////////////////////////////////////////////
if (pos.get_sideToPlay() == white)
return eval;
else return -eval;
}