- if (failedStep) *failedStep = 1;
-
- // Side to move OK?
- if (sideToMove != WHITE && sideToMove != BLACK)
- return false;
-
- // Are the king squares in the position correct?
- if (failedStep) (*failedStep)++;
- if (piece_on(king_square(WHITE)) != W_KING)
- return false;
-
- if (failedStep) (*failedStep)++;
- if (piece_on(king_square(BLACK)) != B_KING)
- return false;
-
- // Do both sides have exactly one king?
- if (failedStep) (*failedStep)++;
- if (debugKingCount)
- {
- int kingCount[2] = {0, 0};
- for (Square s = SQ_A1; s <= SQ_H8; s++)
- if (type_of(piece_on(s)) == KING)
- kingCount[color_of(piece_on(s))]++;
-
- if (kingCount[0] != 1 || kingCount[1] != 1)
- return false;
- }
-
- // Can the side to move capture the opponent's king?
- if (failedStep) (*failedStep)++;
- if (debugKingCapture)
- {
- Color us = sideToMove;
- Color them = ~us;
- Square ksq = king_square(them);
- if (attackers_to(ksq) & pieces(us))
- return false;
- }
-
- // Is there more than 2 checkers?
- if (failedStep) (*failedStep)++;
- if (debugCheckerCount && popcount<Full>(st->checkersBB) > 2)
- return false;
-
- // Bitboards OK?
- if (failedStep) (*failedStep)++;
- if (debugBitboards)
- {
- // The intersection of the white and black pieces must be empty
- if (pieces(WHITE) & pieces(BLACK))
- return false;
-
- // The union of the white and black pieces must be equal to all
- // occupied squares
- if ((pieces(WHITE) | pieces(BLACK)) != pieces())
- return false;
-
- // Separate piece type bitboards must have empty intersections
- for (PieceType p1 = PAWN; p1 <= KING; p1++)
- for (PieceType p2 = PAWN; p2 <= KING; p2++)
- if (p1 != p2 && (pieces(p1) & pieces(p2)))
- return false;
- }
-
- // En passant square OK?
- if (failedStep) (*failedStep)++;
- if (ep_square() != SQ_NONE)
- {
- // The en passant square must be on rank 6, from the point of view of the
- // side to move.
- if (relative_rank(sideToMove, ep_square()) != RANK_6)
- return false;
- }
-
- // Hash key OK?
- if (failedStep) (*failedStep)++;
- if (debugKey && st->key != compute_key())
- return false;
-
- // Pawn hash key OK?
- if (failedStep) (*failedStep)++;
- if (debugPawnKey && st->pawnKey != compute_pawn_key())
- return false;
-
- // Material hash key OK?
- if (failedStep) (*failedStep)++;
- if (debugMaterialKey && st->materialKey != compute_material_key())
- return false;
-
- // Incremental eval OK?
- if (failedStep) (*failedStep)++;
- if (debugIncrementalEval && st->psqScore != compute_psq_score())
- return false;