-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.cpp
204 lines (172 loc) · 5.66 KB
/
evaluation.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
#include "evaluation.h"
inline void evalPieces(const Board &cboard, int &wscore, int &bscore)
{
// white score
for (int i=0; i<6; ++i) {
ull aux = cboard.bb[i];
for (int pp = LSBi(aux); pp != 64; pp = LSBi(aux))
wscore += PieceScore[i];
}
// black score
for (int i=0; i<6; ++i) {
ull aux = cboard.bb[i+7];
for (int pp = LSBi(aux); pp != 64; pp = LSBi(aux))
bscore += PieceScore[i];
}
}
inline void evalPawn(const Board &cboard, const int gameStage, int &wscore, int &bscore)
{
float pawnPieceScale = 0, pawnPosScale = 1;
if (gameStage == 1) pawnPieceScale = 0.25, pawnPosScale = 1.3;
if (gameStage == 2) pawnPieceScale = 0.40, pawnPosScale = 1.7;
if (gameStage == 3) pawnPieceScale = 0.70, pawnPosScale = 2.0;
// white
ull aux = cboard.bb[0];
for (int pp = LSBi(aux); pp != 64; pp = LSBi(aux)) {
wscore += (int) (PieceScore[0] * pawnPieceScale);
wscore += (int) (PiecePositionScore[0][63-pp] * pawnPosScale);
// check if it's on an empty file or a half-empty file
int col = pp % 8;
if (MSB(cbb[col] & (cboard.bb[6] | cboard.bb[13])) == pp) {
// the pawn has no pieces in front of it, add half it's value
wscore += (int) (PieceScore[0] * (pawnPieceScale)/2);
}
else if (MSB(cbb[col] & cboard.bb[0]) != pp) {
// there is another pawn on the same file
wscore -= (int) (12 * pawnPosScale);
}
// isolated pawns get a penalty
if ((col==0 || (col>0 && LSB(cbb[col-1] & cboard.bb[0]) == 64)) &&
(col==7 || (col<7 && LSB(cbb[col+1] & cboard.bb[0]) == 64))) {
wscore -= IsolatedPawnPenalty[col];
}
}
// black
aux = cboard.bb[0+7];
for (int pp = LSBi(aux); pp != 64; pp = LSBi(aux)) {
bscore += (int) (PieceScore[0] * pawnPieceScale);
bscore += (int) (PiecePositionScore[0][63 - (pp%8 + (7-(pp/8))*8)] * pawnPosScale);
// check if it's on an empty file or a half-empty file
int col = pp % 8;
if (LSB(cbb[col] & (cboard.bb[6] | cboard.bb[13])) == pp) {
// the pawn has no pieces in front of it, add half it's value
bscore += (int) (PieceScore[0] * (pawnPieceScale)/2);
}
else if (LSB(cbb[col] & cboard.bb[7]) != pp) {
// there is another pawn on the same file
bscore -= (int) (12 * pawnPosScale);
}
// isolated pawns get a penalty
if ((col==0 || (col>0 && LSB(cbb[col-1] & cboard.bb[7]) == 64)) &&
(col==7 || (col<7 && LSB(cbb[col+1] & cboard.bb[7]) == 64))) {
bscore -= IsolatedPawnPenalty[col];
}
}
}
inline void evalKing(const Board &cboard, const int gameStage, int &wscore, int &bscore)
{
// In late game, use a different piece position scoring table at KING_W+1
const int piece_position_table = gameStage > 1 ? KING_W+1 : KING_W;
const int wpos = LSB(cboard.bb[KING_W]);
wscore += PiecePositionScore[piece_position_table][63-wpos];
const int bpos = LSB(cboard.bb[KING_B]);
bscore += PiecePositionScore[piece_position_table][63 - (bpos%8 + (7-(bpos/8))*8)];
}
int CalculateScore(const Board &cboard)
{
int wscore=0, bscore=0;
int gameStage=0;
evalPieces(cboard, wscore, bscore);
if (wscore < KING_SCORE+2000 || bscore < KING_SCORE+2000) gameStage = 1;
if (wscore < KING_SCORE+1500 || bscore < KING_SCORE+1500) gameStage = 2;
if (wscore < KING_SCORE+1000 || bscore < KING_SCORE+1000) gameStage = 3;
evalPawn(cboard, gameStage, wscore, bscore);
evalKing(cboard, gameStage, wscore, bscore);
//////
// Position score
ull attack = 0;
ull occ = cboard.GetOccupancy();
// white
for (int i=PAWN_W; i<=KING_W; ++i) {
ull aux = cboard.bb[i];
for (int pos = LSBi(aux); pos != 64; pos = LSBi(aux)) {
// attack squares
if (i == PAWN_W) {
if (pos+7 < 64) SET_BIT(attack, pos+7);
if (pos+9 < 64) SET_BIT(attack, pos+9);
continue;
}
else if (i == KNIGHT_W) {
attack |= Nmagic(pos);
}
else if (i == BISHOP_W) {
attack |= Bmagic(pos, occ);
}
else if (i == ROOK_W) {
attack |= Rmagic(pos, occ);
}
else if (i == QUEEN_W) {
attack |= Qmagic(pos, occ);
}
else if (i == KING_W) {
attack |= Kmagic(pos);
continue;
}
// piece position
wscore += PiecePositionScore[i][63-pos];
}
}
for (int i=LSBi(attack); i != 64; i = LSBi(attack)) {
wscore += AttackScore[i];
}
// black
attack = 0;
for (int i=PAWN_B; i<=KING_B; ++i) {
ull aux = cboard.bb[i];
for (int pos = LSBi(aux); pos != 64; pos = LSBi(aux)) {
// attack squares
if (i == PAWN_B) {
if (pos-7 >= 0) SET_BIT(attack, pos-7);
if (pos-9 >= 0) SET_BIT(attack, pos-9);
continue;
}
else if (i == KNIGHT_B) {
attack |= Nmagic(pos);
}
else if (i == BISHOP_B) {
attack |= Bmagic(pos, occ);
}
else if (i == ROOK_B) {
attack |= Rmagic(pos, occ);
}
else if (i == QUEEN_B) {
attack |= Qmagic(pos, occ);
}
else if (i == KING_B) {
attack |= Kmagic(pos);
continue;
}
// piece position
bscore += PiecePositionScore[i-7][63 - (pos%8 + (7-(pos/8))*8)];
}
}
for (int i=LSBi(attack); i != 64; i = LSBi(attack)) {
bscore += AttackScore[i];
}
#ifndef NORAND
// a little bit of random if we are winning
if ((wscore - bscore) > 1200 || (wscore - bscore) < -1200)
return wscore - bscore + (rand()%201 - 100);
if ((wscore - bscore) > 600 || (wscore - bscore) < -600)
return wscore - bscore + (rand()%101 - 50);
if ((wscore - bscore) > 400 || (wscore - bscore) < -400)
return wscore - bscore + (rand()%51 - 25);
#endif
return wscore - bscore;
}
int SCalculateScore(const Board &cboard)
{
return (cboard.player==0) ? CalculateScore(cboard) : -CalculateScore(cboard);
}
//
///////////////////////////////////////////////////////////////