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
|
#include "evaluate.hpp"
#include "board/board.hpp"
#include "moves.hpp"
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <iostream>
constexpr int PAWN_VALUE = 100;
constexpr int KNIGHT_VALUE = 320;
constexpr int BISHOP_VALUE = 400;
constexpr int ROOK_VALUE = 500;
constexpr int QUEEN_VALUE = 900;
constexpr int MATE = 10000;
static std::array<int, 64> PAWN_TABLE = {
0, 0, 0, 0, 0, 0, 0, 0, // last rank promotes to a quuen
50, 50, 50, 50, 50, 50, 50, 50, // comments to stop formating to ruin it
10, 10, 20, 35, 35, 20, 10, 10, //
5, 5, 10, 30, 30, 10, 5, 5, //
0, 0, 0, 25, 25, 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, //
};
static std::array<int, 64> 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, //
};
static std::array<int, 64> BISHOP_TABLE = {
-20, -10, -10, -10, -10, -10, -10, -20, //
-10, 5, 0, 0, 0, 0, 5, -10, //
-10, 10, 10, 10, 10, 10, 10, -10, //
-10, 0, 10, 15, 15, 10, 0, -10, //
-10, 5, 5, 10, 10, 5, 5, -10, //
-10, 0, 5, 10, 10, 5, 0, -10, //
-10, 0, 0, 0, 0, 0, 0, -10, //
-20, -10, -10, -10, -10, -10, -10, -20, //
};
static std::array<int, 64> ROOK_TABLE = {
0, 0, 5, 10, 10, 5, 0, 0, //
5, 10, 10, 10, 10, 10, 10, 5, //
-5, 0, 0, 0, 0, 0, 0, -5, //
-5, 0, 0, 5, 5, 0, 0, -5, //
-5, 0, 0, 5, 5, 0, 0, -5, //
-5, 0, 0, 0, 0, 0, 0, -5, //
5, 10, 10, 10, 10, 10, 10, 5, //
0, 0, 5, 10, 10, 5, 0, 0, //
};
static std::array<int, 64> 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, //
};
static std::array<int, 64> KING_TABLE_EARLY = {
-30, -40, -40, -50, -50, -40, -40, -30, //
-30, -40, -40, -50, -50, -40, -40, -30, //
-30, -40, -40, -50, -50, -40, -40, -30, //
-30, -40, -40, -50, -50, -40, -40, -30, //
-20, -30, -30, -40, -40, -30, -30, -20, //
-10, -20, -20, -20, -20, -20, -20, -10, //
20, 20, 0, 0, 0, 0, 20, 20, //
20, 30, 10, 0, 0, 10, 30, 20, //
};
static std::array<int, 64> KING_TABLE_LATE = {
-50, -40, -30, -20, -20, -30, -40, -50, //
-30, -20, -10, 0, 0, -10, -20, -30, //
-30, -10, 20, 30, 30, 20, -10, -30, //
-30, -10, 30, 40, 40, 30, -10, -30, //
-30, -10, 30, 40, 40, 30, -10, -30, //
-30, -10, 20, 30, 30, 20, -10, -30, //
-30, -30, 0, 0, 0, 0, -30, -30, //
-50, -30, -30, -30, -30, -30, -30, -50, //
};
int EvaluateBoard(Game *g) {
int score = EvaluateBoardForWhite(g);
return g->turn ? score : score * -1;
};
/*
* i cound have come up with better name.
* this function just takes square and if color is black rotate it
*/
static size_t RotateBoardForBlack(const size_t square, const bool color) {
return !color ? square : (56 ^ square);
}
static int ForceKingToEdgeBonus(const Position enemyKing,
const Position myKing) {
int bonus = 0;
// Push enemy king toward edge
const int distToCenter =
std::abs(enemyKing.file - 3) + std::abs(enemyKing.rank - 3);
bonus += distToCenter * 10;
// Bring own king closer
const int kingDistance = std::abs(myKing.file - enemyKing.file) +
std::abs(myKing.rank - enemyKing.rank);
bonus += (14 - kingDistance) * 5;
return bonus;
}
int CountBoardMaterial(Game *g, bool isEndgame) {
int score = 0;
uint64_t piece_bitboard = g->PieceBitboard;
while (piece_bitboard != 0) {
auto i = static_cast<size_t>(__builtin_ctzll(piece_bitboard));
piece_bitboard &= piece_bitboard - 1;
const Piece piece = g->pieces[i];
if (piece.type == NONEPIECE) {
assert(false && "in piece bitboard found none piece");
std::cout << "Internal error\n";
exit(1);
}
int value = 0;
switch (piece.type) {
case PAWN:
value = PAWN_VALUE;
value += PAWN_TABLE[RotateBoardForBlack(i, piece.color)];
break;
case KNIGHT:
value = KNIGHT_VALUE;
value += KNIGHT_TABLE[RotateBoardForBlack(i, piece.color)];
break;
case BISHOP:
value = BISHOP_VALUE;
value += BISHOP_TABLE[RotateBoardForBlack(i, piece.color)];
break;
case ROOK:
value = ROOK_VALUE;
value += ROOK_TABLE[RotateBoardForBlack(i, piece.color)];
break;
case QUEEN:
value = QUEEN_VALUE;
value += QUEEN_TABLE[RotateBoardForBlack(i, piece.color)];
break;
case KING:
if (isEndgame) {
value = KING_TABLE_LATE[RotateBoardForBlack(i, piece.color)];
} else {
value = KING_TABLE_EARLY[RotateBoardForBlack(i, piece.color)];
}
break;
default:
break;
}
if (piece.color) {
score += value;
} else {
score -= value;
}
}
return score;
};
bool IsEndgame(const Game *g) {
int queens = 0;
int minor = 0;
for (const Piece &piece : g->pieces) {
switch (piece.type) {
case QUEEN:
queens++;
break;
case BISHOP:
case KNIGHT:
case ROOK:
minor++;
break;
default:
break;
}
}
return (queens == 0 && minor <= 2) || (queens == 2 && minor <= 1);
}
int EvaluateBoardForWhite(Game *g) {
int score = 0;
bool isEndGame = IsEndgame(g);
score += CountBoardMaterial(g, isEndGame);
if (isEndGame) {
Position whiteKing = FindKing(*g, true);
Position blackKing = FindKing(*g, false);
score += ForceKingToEdgeBonus(blackKing, whiteKing);
score -= ForceKingToEdgeBonus(whiteKing, blackKing);
}
return score;
}
|