Skip to content

Commit

Permalink
Terminate on positions with piece configurations impossible in chess …
Browse files Browse the repository at this point in the history
…that could result in too many generated moves. (for example 3 rooks and 9 queens)
  • Loading branch information
Sopel97 committed May 11, 2023
1 parent c606f7d commit a44292c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,26 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
UCI::critical_error(std::string("Invalid FEN. Invalid piece: ") + std::string(1, token));
}

int pawns_w = count<PAWN>(WHITE);
int pawns_b = count<PAWN>(BLACK);
if (pawns_w > 8)
UCI::critical_error("Invalid FEN. WHITE has more than 8 pawns.");
if (pawns_b > 8)
UCI::critical_error("Invalid FEN. BLACK has more than 8 pawns.");

int additional_knights_w = std::max((int)count<KNIGHT>(WHITE) - 2, 0);
int additional_knights_b = std::max((int)count<KNIGHT>(BLACK) - 2, 0);
int additional_bishops_w = std::max((int)count<BISHOP>(WHITE) - 2, 0);
int additional_bishops_b = std::max((int)count<BISHOP>(BLACK) - 2, 0);
int additional_rooks_w = std::max((int)count<ROOK>(WHITE) - 2, 0);
int additional_rooks_b = std::max((int)count<ROOK>(BLACK) - 2, 0);
int additional_queens_w = std::max((int)count<QUEEN>(WHITE) - 1, 0);
int additional_queens_b = std::max((int)count<QUEEN>(BLACK) - 1, 0);
if (additional_knights_w + additional_bishops_w + additional_rooks_w + additional_queens_w > 8 - pawns_w)
UCI::critical_error("Invalid FEN. Invalid piece configuration for WHITE.");
if (additional_knights_b + additional_bishops_b + additional_rooks_b + additional_queens_b > 8 - pawns_b)
UCI::critical_error("Invalid FEN. Invalid piece configuration for BLACK.");

// 2. Active color
ss >> token;
if (token != 'w' && token != 'b')
Expand Down

0 comments on commit a44292c

Please sign in to comment.