58 byte NumOnRank[16][8];
59 byte NumOnFyle[16][8];
60 byte NumOnLeftDiag[16][16];
61 byte NumOnRightDiag[16][16];
62 byte NumOnSquareColor[16][2];
64 directionT Pinned[16];
75 squareT castleRookSq_[4];
83 inline void AddHash (pieceT p, squareT sq);
84 inline void UnHash (pieceT p, squareT sq);
86 inline void AddToBoard (pieceT p, squareT sq);
87 inline void RemoveFromBoard (pieceT p, squareT sq);
89 void CalcPinsDir (directionT dir, pieceT attacker);
91 void GenSliderMoves (
MoveList * mlist, colorT c, squareT sq,
94 void GenKnightMoves (
MoveList * mlist, colorT c, squareT sq,
97 void AddLegalMove (
MoveList * mlist, squareT from, squareT to, pieceT promo);
99 void GenKingMoves (
MoveList * mlist, genMovesT genType);
100 void AddPromotions (
MoveList * mlist, squareT from, squareT dest);
101 bool IsValidEnPassant (squareT from, squareT to);
102 void GenPawnMoves (
MoveList * mlist, squareT from, directionT dir,
105 void GenCheckEvasions(
MoveList* mlist, pieceT mask, genMovesT genType,
108 errorT readPieceMoveAction(
MoveAction* sm,
const char* str,
size_t slen,
110 errorT readCastleMoveAction(
MoveAction* sm, std::string_view str)
const;
111 errorT readPawnMoveAction(
MoveAction* sm,
const char* str,
size_t slen,
113 errorT readKingMoveAction(
MoveAction* sm,
const char* str,
115 errorT readCoordinateMoveAction(
MoveAction* m,
const char* s,
116 size_t slen,
bool reverse);
117 errorT parseMoveAction(
MoveAction* sm,
const char* begin,
const char* end);
119 void resolveMove(squareT from, squareT to, pieceT promo,
122 template <
typename TFunc>
123 bool under_attack(squareT target_sq, squareT captured_sq,
124 TFunc not_empty)
const;
125 bool under_attack(squareT target_sq)
const;
127 static constexpr unsigned castlingIdx(colorT color, castleDirT side) {
128 return 2 * color + side;
130 squareT find_castle_rook(colorT col, squareT rsq)
const;
131 squareT castleRookSq(colorT color,
bool king_side)
const {
132 return castleRookSq_[2 * color + (king_side ? 1 : 0)];
139 static const Position& getStdStart();
142 void StdStart() { *
this = getStdStart(); }
143 bool IsStdStart()
const;
144 errorT AddPiece (pieceT p, squareT sq);
146 bool isChess960()
const {
return variant_ == 1; }
149 byte PieceCount (pieceT p) {
return Material[p]; }
150 const byte* GetMaterial()
const {
return Material; }
151 void SetEPTarget (squareT s) { EPTarget = s; }
152 squareT GetEPTarget ()
const {
return EPTarget; }
153 void SetToMove (colorT c) { ToMove = c; }
154 colorT GetToMove ()
const {
return ToMove; }
155 bool WhiteToMove ()
const {
return ToMove == WHITE; }
156 void SetPlyCounter (ushort x) { PlyCounter = x; }
157 ushort GetPlyCounter ()
const {
return PlyCounter; }
158 ushort GetFullMoveCount()
const {
return PlyCounter / 2 + 1; }
162 const squareT* GetList(colorT c)
const {
return List[c]; }
163 uint GetCount (colorT c)
const {
return Count[c]; }
164 uint TotalMaterial ()
const {
return Count[WHITE] + Count[BLACK]; }
165 uint NumNonPawns (colorT c) {
166 return Count[c] - Material[piece_Make(c,PAWN)];
168 bool InPawnEnding () {
169 return (NumNonPawns(WHITE) == 1 && NumNonPawns(BLACK) == 1);
171 uint MaterialValue (colorT c);
172 inline uint FyleCount (pieceT p, fyleT f)
const {
173 return NumOnFyle[p][f];
175 inline uint RankCount (pieceT p, rankT r)
const {
176 return NumOnRank[p][r];
178 inline uint LeftDiagCount (pieceT p, leftDiagT diag)
const {
179 return NumOnLeftDiag[p][diag];
181 inline uint RightDiagCount (pieceT p, rightDiagT diag)
const {
182 return NumOnRightDiag[p][diag];
184 inline uint SquareColorCount (pieceT p, colorT sqColor)
const {
185 return NumOnSquareColor[p][sqColor];
188 const pieceT* GetBoard()
const {
189 const_cast<Position*
>(
this)->Board[COLOR_SQUARE] = COLOR_CHAR[ToMove];
193 pieceT GetPiece(squareT sq)
const {
199 squareT GetKingSquare (colorT c)
const {
return List[c][0]; }
200 squareT GetKingSquare ()
const {
return List[ToMove][0]; }
201 squareT GetEnemyKingSquare ()
const {
return List[1-ToMove][0]; }
204 bool GetCastling(colorT c, castleDirT dir)
const {
205 return Castling & (1u << castlingIdx(c, dir));
207 byte GetCastlingFlags()
const {
return Castling; }
208 bool validCastlingFlag(colorT color,
bool king_side)
const;
211 inline uint HashValue (
void)
const {
return Hash; }
212 inline uint PawnHashValue (
void)
const {
return PawnHash; }
217 void GenPieceMoves (
MoveList * mlist, squareT sq,
221 void GenerateMoves (
MoveList* mlist, pieceT mask, genMovesT genType,
bool maybeInCheck);
222 void GenerateMoves (
MoveList * mlist) { GenerateMoves (mlist, EMPTY, GEN_ALL_MOVES,
true); }
223 void GenerateMoves (
MoveList * mlist, genMovesT genType) { GenerateMoves (mlist, EMPTY, genType,
true); }
224 void GenerateCaptures (
MoveList * mlist) { GenerateMoves (mlist, EMPTY, GEN_CAPTURES,
true); }
225 int IsLegalMove(squareT from, squareT to, pieceT promo)
const;
232 template <
bool check_legal = true>
bool canCastle(
bool king_side)
const;
234 uint CalcAttacks (colorT toMove, squareT kingSq,
SquareList * squares)
const;
235 int TreeCalcAttacks (squareT target);
236 uint CalcNumChecks ()
const {
237 return CalcAttacks (1-ToMove, GetKingSquare(), NULL);
239 uint CalcNumChecks (squareT kingSq)
const {
240 return CalcAttacks (1-ToMove, kingSq, NULL);
242 uint CalcNumChecks (squareT kingSq, SquareList * checkSquares)
const {
243 return CalcAttacks (1-ToMove, kingSq, checkSquares);
246 uint Mobility (pieceT p, colorT color, squareT from);
247 bool IsKingInCheck () {
return (CalcNumChecks() > 0); }
248 bool IsKingInCheck (MoveAction
const& sm);
249 bool IsKingInMate ();
252 bool IsPromoMove (squareT from, squareT to);
254 errorT parseMoveSpec(MoveSpec& spec, std::string_view notation);
255 errorT readCoordinateMoveSpec(MoveSpec& spec,
256 std::string_view notation,
258 std::string makeSan(MoveSpec
const& spec, sanFlagT flag);
259 errorT applyMove(MoveSpec
const& spec);
261 errorT resolveMove(MoveSpec
const& spec, MoveAction& action)
const;
262 void apply(MoveAction
const& action);
263 void undo(MoveAction
const& action);
265 void writeSan(MoveAction
const& action,
char* s, sanFlagT flag);
267 errorT applyCoordinateMoves(
const char* moves,
size_t movesLen,
268 std::string* toSAN =
nullptr);
271 void MakeLongStr (
char* str)
const;
272 errorT ReadFromLongStr (
const char * str);
273 errorT ReadFromFEN (
const char * s);
274 errorT ReadFromFENorUCI (std::string_view str);
275 void PrintCompactStr (
char * cboard)
const;
276 void PrintFEN(
char* str,
size_t len)
const;
277 void DumpLatexBoard (DString * dstr,
bool flip);
278 void DumpLatexBoard (DString * dstr) {
279 DumpLatexBoard (dstr,
false);
281 void DumpHtmlBoard (DString * dstr, uint style,
const char * dir,
283 void DumpHtmlBoard (DString * dstr, uint style,
const char * dir) {
284 DumpHtmlBoard (dstr, style, dir,
false);
288 int Compare (Position * p);
289 void CopyFrom (Position * src) { *
this = *src; }
292 errorT Random (
const char * material);
295 void setCastling(colorT col, squareT rsq);
296 void ClearCastling(colorT col, castleDirT dir) {
297 Castling &= ~(1u << castlingIdx(col, dir));
299 void ClearCastlingFlags(colorT c) {
300 Castling &= (c == WHITE) ? 0b11111100 : 0b11110011;