-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaskablePiece.cpp
More file actions
107 lines (96 loc) · 3.13 KB
/
Copy pathaskablePiece.cpp
File metadata and controls
107 lines (96 loc) · 3.13 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
#include "stockfish_src/uci.h"
#include "askablePiece.h"
AskablePiece::AskablePiece(Piece p, Square s, AskablePosition* pos) {
/*
* Set legalMoves of piece in constructor.
*/
piece = p;
color = color_of(p);
currentSq = s;
currentPos = pos;
if (color == currentPos->side_to_move()) {
for (const auto &o: currentPos->legalMoves) {
if (from_sq(o.sfMove) == currentSq) {
this->legalMoves.push_back(o);
}
}
} else {
for (const auto &m: currentPos->oppLegalMoves) {
if (from_sq(m.sfMove) == currentSq) {
this->legalMoves.push_back(m);
}
}
}
}
string pieceToString(Piece p) {
if (p == 1 or p == 9) {
return string("pawn");
} else if (p == 2 or p == 10) {
return string("knight");
} else if (p == 3 or p == 11) {
return string("bishop");
} else if (p == 4 or p == 12) {
return string("rook");
} else if (p == 5 or p == 13) {
return string("queen");
} else if (p == 6 or p == 14) {
return string("king");
}
return string("none");
}
void AskablePiece::debug() const {
cerr << (color ? "black " : "white ") << pieceToString(piece) << " " << UCI::square(currentSq) << " ";
cerr << "legalMovesCount: " << legalMovesCount() << " ";
cerr << "targetsCount: " << targetsCount() << " ";
cerr << "isDefended: " << isDefended() << " ";
cerr << "isPinned: " << isPinned() << " ";
cerr << "isHanging: " << isHanging() << " ";
cerr << "targetsLegalMovesCount: " << targetsLegalMovesCount() << endl;
}
size_t AskablePiece::legalMovesCount() const {
return legalMoves.size();
}
size_t AskablePiece::targetsCount() const { // how many pieces it can captures
size_t tarCount = 0;
for (const auto& m: legalMoves) {
if (currentPos->capture(m.sfMove)) {
tarCount++;
}
}
return tarCount;
}
bool AskablePiece::isPinned() const {
/*
* Use `Stockfish::Position::blockers_for_king`;
*/
return currentPos->blockers_for_king(color) & currentSq;
}
bool AskablePiece::isDefended() const {
/*
* Intersection of attackers to current Square and our pieces;
*/
return currentPos->attackers_to(currentSq) & currentPos->pieces(color);
}
bool AskablePiece::isHanging() const {
/*
* Is not defended and one or more opponent pieces attacks it.
*/
return !isDefended() && currentPos->attackers_to(currentSq) & currentPos->pieces(~color);
}
size_t AskablePiece::targetsLegalMovesCount() const { // how many legal moves are there for all the pieces it can capture
/*
* Iter on all captures and sum targets legal moves.
*/
vector<AskablePiece> considered = (color == currentPos->side_to_move() ? currentPos->theirPieces : currentPos->ourPieces);
size_t count = 0;
for (const auto& m: legalMoves) {
if (currentPos->capture(m.sfMove)) {
for (const auto& p: considered) {
if (p.currentSq == to_sq(m.sfMove)) {
count += p.legalMovesCount();
}
}
}
}
return count;
}