From 47575ebd8bc092b091e496ea776dc56d22972487 Mon Sep 17 00:00:00 2001 From: FauziAkram Date: Thu, 25 Jun 2026 13:11:17 +0200 Subject: [PATCH] Deduplicating Color-Specific Piece Validation Deduplicating Color-Specific Piece Validation. The validation checks for the number of pawns and additional promoted pieces are duplicated for WHITE and BLACK. We can combine this logic into a single range-based for loop over both colors. closes https://github.com/official-stockfish/Stockfish/pull/6922 No functional change --- src/position.cpp | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index d29a23767..a70acd6c1 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -276,25 +276,18 @@ Position::set(const string& fenStr, bool isChess960, StateInfo* si) { if (count(WHITE) != 1 || count(BLACK) != 1) return PositionSetError("Unsupported position. Incorrect number of kings."); - const int wPawns = count(WHITE); - const int bPawns = count(BLACK); - if (wPawns > 8) - return PositionSetError("Unsupported position. WHITE has more than 8 pawns."); - if (bPawns > 8) - return PositionSetError("Unsupported position. BLACK has more than 8 pawns."); + for (Color c : {WHITE, BLACK}) + { + if (count(c) > 8) + return PositionSetError(std::string("Unsupported position. ") + + (c == WHITE ? "WHITE" : "BLACK") + " has more than 8 pawns."); - const int wAdditionalKnights = std::max((int) count(WHITE) - 2, 0); - const int bAdditionalKnights = std::max((int) count(BLACK) - 2, 0); - const int wAdditionalBishops = std::max((int) count(WHITE) - 2, 0); - const int bAdditionalBishops = std::max((int) count(BLACK) - 2, 0); - const int wAdditionalRooks = std::max((int) count(WHITE) - 2, 0); - const int bAdditionalRooks = std::max((int) count(BLACK) - 2, 0); - const int wAdditionalQueens = std::max((int) count(WHITE) - 1, 0); - const int bAdditionalQueens = std::max((int) count(BLACK) - 1, 0); - if (wAdditionalKnights + wAdditionalBishops + wAdditionalRooks + wAdditionalQueens > 8 - wPawns) - return PositionSetError("Unsupported position. Too many pieces for WHITE."); - if (bAdditionalKnights + bAdditionalBishops + bAdditionalRooks + bAdditionalQueens > 8 - bPawns) - return PositionSetError("Unsupported position. Too many pieces for BLACK."); + int additional = std::max(count(c) - 2, 0) + std::max(count(c) - 2, 0) + + std::max(count(c) - 2, 0) + std::max(count(c) - 1, 0); + if (additional > 8 - count(c)) + return PositionSetError(std::string("Unsupported position. Too many pieces for ") + + (c == WHITE ? "WHITE." : "BLACK.")); + } // 2. Active color if (!(ss >> token))