-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
317 lines (287 loc) · 9.97 KB
/
engine.cpp
File metadata and controls
317 lines (287 loc) · 9.97 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//
// Created by LociStar on 28.09.2020.
//
#include "engine.h"
#include <bitset>
#include "positionTables.h"
template<Color Us>
int Engine::negamax(Position *p, int depth, int alpha, int beta, bool first) {
//int localalpha = alpha;
//tableNode tableNode = transposition.get(key);
counter++;
int move_score = 0;
if (depth == 0) {
//move_score = quiescenceSearch(alpha, beta);
//evaluation_2<Us>();
return evaluation();
}
int best_score = -999999999; // Negative-Infinity
MoveList<Us> moveList(*p);
//sortMoves(moveList, key);2020-09-30 12:07:43,585-->1:position startpos moves b1a3 b8a6 g1f3 g8f6 a1b1 a8b8 h1g1 h8g8
if (isDraw()) {
return 0;
}
if (isCheckMate()) {
return -9000 * depth;
}
for (Move move : moveList) {
p->play<Us>(move);
move_score = -negamax<~Us>(p, depth - 1, -beta, -alpha, false);
p->undo<Us>(move);
if (move_score > best_score) {
best_score = move_score;
if (first) {
bestMove = move;
}
}
alpha = std::max(alpha, best_score);
if (alpha >= beta) {
break;
}
}
//addTransposition(key, depth, alphaOriginal, beta, move_score, best);
return best_score;
}
void evalPawn(Position *position, int *value) {
auto x = static_cast<unsigned long long>(position->bitboard_of(WHITE_PAWN));
//unsigned long long fix for issue pointed out by @Zac Howland in comments
unsigned int count = 0;//number bits
while (x != 0) {
unsigned long long bit = x & 1;
if (bit == 1) {
*value += pawn[63 - count] + 100;
}
x >>= 1;
count++;
}
x = static_cast<unsigned long long>(position->bitboard_of(BLACK_PAWN));
//unsigned long long fix for issue pointed out by @Zac Howland in comments
count = 0;//number of 1 bits
while (x != 0) {
unsigned long long bit = x & 1;
if (bit == 1) {
*value -= pawn[count] + 100;
}
x >>= 1;
count++;
}
}
void evalKnight(Position *position, int *value) {
auto x = static_cast<unsigned long long>(position->bitboard_of(WHITE_KNIGHT));
//unsigned long long fix for issue pointed out by @Zac Howland in comments
unsigned int count = 0;//number of 1 bits
while (x != 0) {
unsigned long long bit = x & 1;
if (bit == 1) {
*value += knight[63 - count] + 300;
}
x >>= 1;
count++;
}
x = static_cast<unsigned long long>(position->bitboard_of(BLACK_KNIGHT));
//unsigned long long fix for issue pointed out by @Zac Howland in comments
count = 0;//number of 1 bits
while (x != 0) {
unsigned long long bit = x & 1;
if (bit == 1) {
*value -= knight[count] + 300;
}
x >>= 1;
count++;
}
}
void evalBishop(Position *position, int *value) {
auto x = static_cast<unsigned long long>(position->bitboard_of(WHITE_BISHOP));
//unsigned long long fix for issue pointed out by @Zac Howland in comments
unsigned int count = 0;//number of 1 bits
while (x != 0) {
unsigned long long bit = x & 1;
if (bit == 1) {
*value += bishop[63 - count] + 300;
}
x >>= 1;
count++;
}
x = static_cast<unsigned long long>(position->bitboard_of(BLACK_BISHOP));
//unsigned long long fix for issue pointed out by @Zac Howland in comments
count = 0;//number of 1 bits
while (x != 0) {
unsigned long long bit = x & 1;
if (bit == 1) {
*value -= bishop[count] + 300;
}
x >>= 1;
count++;
}
}
void evalKing(Position *position, int *value) {
auto x = static_cast<unsigned long long>(position->bitboard_of(WHITE_KING));
//unsigned long long fix for issue pointed out by @Zac Howland in comments
unsigned int count = 0;//number of 1 bits
while (x != 0) {
unsigned long long bit = x & 1;
if (bit == 1) {
*value += king[63 - count];
}
x >>= 1;
count++;
}
x = static_cast<unsigned long long>(position->bitboard_of(BLACK_KING));
//unsigned long long fix for issue pointed out by @Zac Howland in comments
count = 0;//number of 1 bits
while (x != 0) {
unsigned long long bit = x & 1;
if (bit == 1) {
*value -= king[count];
}
x >>= 1;
count++;
}
}
int Engine::evaluation() {
int materialScore = 900 * (std::bitset<64>(position.bitboard_of(WHITE_QUEEN)).count() -
std::bitset<64>(position.bitboard_of(BLACK_QUEEN)).count())
+ 500 * (std::bitset<64>(position.bitboard_of(WHITE_ROOK)).count() -
std::bitset<64>(position.bitboard_of(BLACK_ROOK)).count());
//value from positioning
int value = 0;
evalPawn(&position, &value);
evalBishop(&position, &value);
evalKnight(&position, &value);
evalKing(&position, &value);
if (position.turn() == 0) return materialScore + value;
else return (materialScore + value) * (-1);
}
/*template<Color Us>
int Engine::evaluation_2() {
int materialScore = 900 * (std::bitset<64>(position.bitboard_of(WHITE_QUEEN)).count() -
std::bitset<64>(position.bitboard_of(BLACK_QUEEN)).count())
+ 500 * (std::bitset<64>(position.bitboard_of(WHITE_ROOK)).count() -
std::bitset<64>(position.bitboard_of(BLACK_ROOK)).count())
+ 300 * (std::bitset<64>(position.bitboard_of(WHITE_KNIGHT)).count() -
std::bitset<64>(position.bitboard_of(BLACK_KNIGHT)).count())
+ 300 * (std::bitset<64>(position.bitboard_of(WHITE_BISHOP)).count() -
std::bitset<64>(position.bitboard_of(BLACK_BISHOP)).count())
+ 100 * (std::bitset<64>(position.bitboard_of(WHITE_PAWN)).count() -
std::bitset<64>(position.bitboard_of(BLACK_PAWN)).count());
if (Us == WHITE) return materialScore;
else return materialScore * (-1);
}*/
bool Engine::isDraw() {
if (isRepetition()) {
// std::cout << "rep" << std::endl;
return true;
}
//std::cout << "InM" << std::endl;
if (isInsufficientMaterial()) return true;
//std::cout << "halfMove" << std::endl;
if (position.halfMoveCounter >= 100) return true;
//std::cout << "Stalemate" << std::endl;
return isStaleMate();
}
bool Engine::isRepetition() {
int i = std::min(position.ply() - 1,
position.halfMoveCounter); //TODO: instead of ply use moveCounter which resets every capture
if (position.ply() >= 4) {
int lastKey = position.history[position.ply() - 1].entry;
int rep = 0;
for (int x = 4; x <= i; x += 2) {
int k = position.history[position.ply() - x - 1].entry;
if (k == lastKey && ++rep >= 2) {
return true;
}
}
}
return false;
}
bool Engine::isInsufficientMaterial() {
bool result = false;
unsigned long long pawns = position.bitboard_of(Piece::WHITE_PAWN) |
position.bitboard_of(Piece::BLACK_PAWN);
if (pawns == 0L) {
if ((position.bitboard_of(Piece::WHITE_QUEEN) +
position.bitboard_of(Piece::BLACK_QUEEN) +
position.bitboard_of(Piece::WHITE_ROOK) +
position.bitboard_of(Piece::BLACK_ROOK)) != 0L) {
result = false;
} else {
std::bitset<64> bs(position.all_pieces<Color::WHITE>() | position.all_pieces<Color::BLACK>());
long count = bs.count();
if (count == 4) {
if (std::bitset<64>(position.all_pieces<Color::WHITE>()).count() > 1 &&
std::bitset<64>(position.all_pieces<Color::BLACK>()).count() > 1) {
result = true;
} else if (std::bitset<64>(position.bitboard_of(WHITE_KNIGHT)).count() == 2 ||
std::bitset<64>(position.bitboard_of(BLACK_KNIGHT)).count() == 2) {
result = true;
}
} else if (count < 4) {
return true;
}
}
}
return result;
}
bool Engine::isStaleMate() {
if (position.turn() == 0) {
if (!position.in_check<WHITE>()) {
MoveList<WHITE> moveList(position);
if (moveList.size() == 0) {
std::cout << "Stalemate" << std::endl;
return true;
}
}
return false;
} else {
if (!position.in_check<BLACK>()) {
MoveList<BLACK> moveList(position);
if (moveList.size() == 0) {
return true;
}
}
return false;
}
return false;
}
bool Engine::isCheckMate() { //TODO: make sideToPlay public/ getter?
if (position.turn() == 0) {
if (position.in_check<WHITE>()) {
MoveList<WHITE> moveList(position);
if (moveList.size() == 0) {
return true;
}
}
return false;
} else {
if (position.in_check<BLACK>()) {
MoveList<BLACK> moveList(position);
if (moveList.size() == 0) {
return true;
}
}
return false;
}
}
int Engine::search() {
int x = 0;
int depth = 6;
int ni = -999999999;
int pi = 999999999;
bool f = true;
if (position.turn() == 0) {
x = negamax<WHITE>(&position, depth, ni, pi, f);
MoveList<WHITE> moves(position);
for (Move move : moves) {
std::cout << move << " ";
}
position.play<WHITE>(bestMove);
} else {
x = negamax<BLACK>(&position, depth, ni, pi, f);
MoveList<BLACK> moves(position);
for (Move move : moves) {
std::cout << move << " ";
}
position.play<BLACK>(bestMove);
}
return x;
}