Fix NNUE compatibility check to validate king count (#932)
authorFabian Fichter <ianfab@users.noreply.github.com>
Mon, 10 Nov 2025 16:34:09 +0000 (17:34 +0100)
committerGitHub <noreply@github.com>
Mon, 10 Nov 2025 16:34:09 +0000 (17:34 +0100)
Add safeguard in nnue_applicable() to ensure that when nnueKing is set,
both WHITE and BLACK have exactly one king of that piece type. This prevents
NNUE from being used in positions where the king count doesn't match the
expected configuration, which could lead to incorrect evaluations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <noreply@anthropic.com>

src/position.h

index 45517d2..1693407 100644 (file)
@@ -594,7 +594,9 @@ inline bool Position::nnue_use_pockets() const {
 
 inline bool Position::nnue_applicable() const {
   // Do not use NNUE during setup phases (placement, sittuyin)
-  return (!count_in_hand(ALL_PIECES) || nnue_use_pockets() || !must_drop()) && !virtualPieces;
+  return (!count_in_hand(ALL_PIECES) || nnue_use_pockets() || !must_drop())
+         && !virtualPieces
+         && (!nnue_king() || (count(WHITE, nnue_king()) == 1 && count(BLACK, nnue_king()) == 1));
 }
 
 inline int Position::nnue_piece_square_index(Color perspective, Piece pc) const {