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
This commit is contained in:
FauziAkram
2026-06-25 13:11:17 +02:00
committed by Joost VandeVondele
parent 3c858c19e6
commit 47575ebd8b
+11 -18
View File
@@ -276,25 +276,18 @@ Position::set(const string& fenStr, bool isChess960, StateInfo* si) {
if (count<KING>(WHITE) != 1 || count<KING>(BLACK) != 1) if (count<KING>(WHITE) != 1 || count<KING>(BLACK) != 1)
return PositionSetError("Unsupported position. Incorrect number of kings."); return PositionSetError("Unsupported position. Incorrect number of kings.");
const int wPawns = count<PAWN>(WHITE); for (Color c : {WHITE, BLACK})
const int bPawns = count<PAWN>(BLACK); {
if (wPawns > 8) if (count<PAWN>(c) > 8)
return PositionSetError("Unsupported position. WHITE has more than 8 pawns."); return PositionSetError(std::string("Unsupported position. ")
if (bPawns > 8) + (c == WHITE ? "WHITE" : "BLACK") + " has more than 8 pawns.");
return PositionSetError("Unsupported position. BLACK has more than 8 pawns.");
const int wAdditionalKnights = std::max((int) count<KNIGHT>(WHITE) - 2, 0); int additional = std::max(count<KNIGHT>(c) - 2, 0) + std::max(count<BISHOP>(c) - 2, 0)
const int bAdditionalKnights = std::max((int) count<KNIGHT>(BLACK) - 2, 0); + std::max(count<ROOK>(c) - 2, 0) + std::max(count<QUEEN>(c) - 1, 0);
const int wAdditionalBishops = std::max((int) count<BISHOP>(WHITE) - 2, 0); if (additional > 8 - count<PAWN>(c))
const int bAdditionalBishops = std::max((int) count<BISHOP>(BLACK) - 2, 0); return PositionSetError(std::string("Unsupported position. Too many pieces for ")
const int wAdditionalRooks = std::max((int) count<ROOK>(WHITE) - 2, 0); + (c == WHITE ? "WHITE." : "BLACK."));
const int bAdditionalRooks = std::max((int) count<ROOK>(BLACK) - 2, 0); }
const int wAdditionalQueens = std::max((int) count<QUEEN>(WHITE) - 1, 0);
const int bAdditionalQueens = std::max((int) count<QUEEN>(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.");
// 2. Active color // 2. Active color
if (!(ss >> token)) if (!(ss >> token))