mirror of
https://github.com/sockspls/badfish
synced 2025-07-11 19:49:14 +00:00
Rewrite pos_is_ok()
No functional change.
This commit is contained in:
parent
47a0768102
commit
152a4dc5cd
2 changed files with 64 additions and 79 deletions
141
src/position.cpp
141
src/position.cpp
|
@ -1172,96 +1172,81 @@ void Position::flip() {
|
||||||
/// Position::pos_is_ok() performs some consistency checks for the position object.
|
/// Position::pos_is_ok() performs some consistency checks for the position object.
|
||||||
/// This is meant to be helpful when debugging.
|
/// This is meant to be helpful when debugging.
|
||||||
|
|
||||||
bool Position::pos_is_ok(int* step) const {
|
bool Position::pos_is_ok(bool fast, int* failedStep) const {
|
||||||
|
|
||||||
// Which parts of the position should be verified?
|
enum { Default, King, Bitboards, State, Lists, Castling };
|
||||||
const bool all = false;
|
|
||||||
|
|
||||||
const bool testBitboards = all || false;
|
for (int step = Default; step <= (fast ? Default : Castling); step++)
|
||||||
const bool testState = all || false;
|
|
||||||
const bool testKingCount = all || false;
|
|
||||||
const bool testKingCapture = all || false;
|
|
||||||
const bool testPieceCounts = all || false;
|
|
||||||
const bool testPieceList = all || false;
|
|
||||||
const bool testCastlingSquares = all || false;
|
|
||||||
|
|
||||||
if (step)
|
|
||||||
*step = 1;
|
|
||||||
|
|
||||||
if ( (sideToMove != WHITE && sideToMove != BLACK)
|
|
||||||
|| piece_on(king_square(WHITE)) != W_KING
|
|
||||||
|| piece_on(king_square(BLACK)) != B_KING
|
|
||||||
|| ( ep_square() != SQ_NONE
|
|
||||||
&& relative_rank(sideToMove, ep_square()) != RANK_6))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (step && ++*step, testBitboards)
|
|
||||||
{
|
{
|
||||||
// The intersection of the white and black pieces must be empty
|
if (failedStep)
|
||||||
if (pieces(WHITE) & pieces(BLACK))
|
*failedStep = step;
|
||||||
return false;
|
|
||||||
|
|
||||||
// The union of the white and black pieces must be equal to all
|
if (step == Default)
|
||||||
// occupied squares
|
if ( (sideToMove != WHITE && sideToMove != BLACK)
|
||||||
if ((pieces(WHITE) | pieces(BLACK)) != pieces())
|
|| piece_on(king_square(WHITE)) != W_KING
|
||||||
return false;
|
|| piece_on(king_square(BLACK)) != B_KING
|
||||||
|
|| ( ep_square() != SQ_NONE
|
||||||
|
&& relative_rank(sideToMove, ep_square()) != RANK_6))
|
||||||
|
return false;
|
||||||
|
|
||||||
// Separate piece type bitboards must have empty intersections
|
if (step == King)
|
||||||
for (PieceType p1 = PAWN; p1 <= KING; ++p1)
|
if ( std::count(board, board + SQUARE_NB, W_KING) != 1
|
||||||
for (PieceType p2 = PAWN; p2 <= KING; ++p2)
|
|| std::count(board, board + SQUARE_NB, B_KING) != 1
|
||||||
if (p1 != p2 && (pieces(p1) & pieces(p2)))
|
|| attackers_to(king_square(~sideToMove)) & pieces(sideToMove))
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
if (step && ++*step, testState)
|
if (step == Bitboards)
|
||||||
{
|
{
|
||||||
StateInfo si;
|
if ( (pieces(WHITE) & pieces(BLACK))
|
||||||
set_state(&si);
|
||(pieces(WHITE) | pieces(BLACK)) != pieces())
|
||||||
if ( st->key != si.key
|
return false;
|
||||||
|| st->pawnKey != si.pawnKey
|
|
||||||
|| st->materialKey != si.materialKey
|
|
||||||
|| st->nonPawnMaterial[WHITE] != si.nonPawnMaterial[WHITE]
|
|
||||||
|| st->nonPawnMaterial[BLACK] != si.nonPawnMaterial[BLACK]
|
|
||||||
|| st->psq != si.psq
|
|
||||||
|| st->checkersBB != si.checkersBB)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (step && ++*step, testKingCount)
|
for (PieceType p1 = PAWN; p1 <= KING; ++p1)
|
||||||
if ( std::count(board, board + SQUARE_NB, W_KING) != 1
|
for (PieceType p2 = PAWN; p2 <= KING; ++p2)
|
||||||
|| std::count(board, board + SQUARE_NB, B_KING) != 1)
|
if (p1 != p2 && (pieces(p1) & pieces(p2)))
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (step && ++*step, testKingCapture)
|
if (step == State)
|
||||||
if (attackers_to(king_square(~sideToMove)) & pieces(sideToMove))
|
{
|
||||||
return false;
|
StateInfo si;
|
||||||
|
set_state(&si);
|
||||||
|
if ( st->key != si.key
|
||||||
|
|| st->pawnKey != si.pawnKey
|
||||||
|
|| st->materialKey != si.materialKey
|
||||||
|
|| st->nonPawnMaterial[WHITE] != si.nonPawnMaterial[WHITE]
|
||||||
|
|| st->nonPawnMaterial[BLACK] != si.nonPawnMaterial[BLACK]
|
||||||
|
|| st->psq != si.psq
|
||||||
|
|| st->checkersBB != si.checkersBB)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (step && ++*step, testPieceCounts)
|
if (step == Lists)
|
||||||
for (Color c = WHITE; c <= BLACK; ++c)
|
for (Color c = WHITE; c <= BLACK; ++c)
|
||||||
for (PieceType pt = PAWN; pt <= KING; ++pt)
|
for (PieceType pt = PAWN; pt <= KING; ++pt)
|
||||||
if (pieceCount[c][pt] != popcount<Full>(pieces(c, pt)))
|
{
|
||||||
return false;
|
if (pieceCount[c][pt] != popcount<Full>(pieces(c, pt)))
|
||||||
|
|
||||||
if (step && ++*step, testPieceList)
|
|
||||||
for (Color c = WHITE; c <= BLACK; ++c)
|
|
||||||
for (PieceType pt = PAWN; pt <= KING; ++pt)
|
|
||||||
for (int i = 0; i < pieceCount[c][pt]; ++i)
|
|
||||||
if ( board[pieceList[c][pt][i]] != make_piece(c, pt)
|
|
||||||
|| index[pieceList[c][pt][i]] != i)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (step && ++*step, testCastlingSquares)
|
for (int i = 0; i < pieceCount[c][pt]; ++i)
|
||||||
for (Color c = WHITE; c <= BLACK; ++c)
|
if ( board[pieceList[c][pt][i]] != make_piece(c, pt)
|
||||||
for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1))
|
|| index[pieceList[c][pt][i]] != i)
|
||||||
{
|
return false;
|
||||||
if (!can_castle(c | s))
|
}
|
||||||
continue;
|
|
||||||
|
|
||||||
if ( (castlingRightsMask[king_square(c)] & (c | s)) != (c | s)
|
if (step == Castling)
|
||||||
|| piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK)
|
for (Color c = WHITE; c <= BLACK; ++c)
|
||||||
|| castlingRightsMask[castlingRookSquare[c | s]] != (c | s))
|
for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1))
|
||||||
return false;
|
{
|
||||||
}
|
if (!can_castle(c | s))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if ( piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK)
|
||||||
|
|| castlingRightsMask[castlingRookSquare[c | s]] != (c | s)
|
||||||
|
||(castlingRightsMask[king_square(c)] & (c | s)) != (c | s))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,7 +174,7 @@ public:
|
||||||
Value non_pawn_material(Color c) const;
|
Value non_pawn_material(Color c) const;
|
||||||
|
|
||||||
// Position consistency check, for debugging
|
// Position consistency check, for debugging
|
||||||
bool pos_is_ok(int* step = nullptr) const;
|
bool pos_is_ok(bool fast = true, int* failedStep = nullptr) const;
|
||||||
void flip();
|
void flip();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Add table
Reference in a new issue