-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cpp
More file actions
60 lines (43 loc) · 1.39 KB
/
Engine.cpp
File metadata and controls
60 lines (43 loc) · 1.39 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
#include <stdexcept>
#include <vector>
#include "Engine.h"
Engine::Engine()
: rng(std::random_device{}()){
}
Move Engine::chooseRandomMove(const Board& board) {
std::vector<Move> legalMoves = board.generateLegalMoves();
if (legalMoves.empty())
throw std::runtime_error("Cannot choose a move: no legal moves available.");
std::uniform_int_distribution<std::size_t> distribution(
0,
legalMoves.size() - 1
);
return legalMoves[distribution(rng)];
}
int Engine::evaluateMaterial(const Board& board) const {
static_assert(
static_cast<int>(Piece::PIECE_COUNT) == 12,
"Expected 12 piece bitboards"
);
const auto& pieces = board.getPieces();
constexpr int FIRST_BLACK_PIECE = static_cast<int>(Piece::BP);
int score = 0;
for (int piece = 0; piece < static_cast<int>(Piece::PIECE_COUNT); ++piece) {
int count = __builtin_popcountll(pieces[piece]);
int material = PIECE_VALUES[piece] * count;
score += piece < FIRST_BLACK_PIECE ? material : -material;
}
return score;
}
int Engine::evaluate(const Board& board, int plyFromRoot) const {
if (board.isCheckmate()) {
if (board.isWhiteToMove()) {
return -MATE_SCORE + plyFromRoot;
}
return MATE_SCORE - plyFromRoot;
}
if (board.isStalemate()) {
return 0;
}
return evaluateMaterial(board);
}