Okay, I will analyze the code to identify potential causes for the illegal move generation when the cheat_move_pruning heuristic is enabled. Since the issue is rare, it likely involves a subtle interaction between the new heuristic and existing move generation or legality checking logic. Here's a breakdown of areas to investigate, along with code snippets from the provided files that might be relevant:
1. Move Generation (movegen.cpp):
-
Problem: The cheat_move_pruning might be interfering with or bypassing the standard move generation process, leading to the creation of moves that are not actually legal in the given position. It's important to check how the heuristic modifies move generation or introduces new moves directly.
-
How to investigate: Review the code where the cheat_move_pruning is implemented. Is it adding moves to the move list without proper validation? Does it accidentally skip the generation of certain legal moves? Is it correctly handling special move types like castling or en passant?
Check that the en passant capture generation within generate_pawn_moves function is not bypassed.
template<Color Us, GenType Type>
ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
...
if (pos.ep_square() != SQ_NONE)
{
assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
// An en passant capture cannot resolve a discovered check
if (Type == EVASIONS && (target & (pos.ep_square() + Up)))
return moveList;
b1 = pawnsNotOn7 & pawn_attacks_bb(Them, pos.ep_square());
assert(b1);
while (b1)
*moveList++ = Move::make<EN_PASSANT>(pop_lsb(b1), pos.ep_square());
}
...
}
2. Legality Checking (position.cpp):
-
Problem: The legal() function in position.cpp is responsible for verifying that a generated move is indeed legal. The cheat_move_pruning might be bypassing or incorrectly modifying the conditions used in this function, causing it to accept illegal moves.
-
How to investigate: Carefully examine how legal() is called in conjunction with the new heuristic. Are there cases where it's skipped or bypassed? Does the heuristic modify the position in a way that confuses the legal() function? Pay close attention to how checks, pins, and other constraints are handled.
Check that the discovered checks are properly handled when determining legality of moves.
bool Position::legal(Move m) const {
...
// En passant captures are a tricky special case. Because they are rather
// uncommon, we do it simply by testing whether the king is attacked after
// the move is made.
if (m.type_of() == EN_PASSANT)
{
Square ksq = square<KING>(us);
Square capsq = to - pawn_push(us);
Bitboard occupied = (pieces() ^ from ^ capsq) | to;
assert(to == ep_square);
return !(attackers_to(ksq, occupied) & pieces(~us));
}
// If the moving piece is a king, then test that the destination square
// is not attacked by the opponent. Castling moves are checked for legality
// when they are generated.
if (type_of(moved_piece(m)) == KING)
return !(st->checkSquares[KING] & to);
...
}
3. Interaction between cheat_move_pruning and Check Evasion (position.cpp, movegen.cpp):
-
Problem: If the cheat_move_pruning is applied during check evasion move generation (generate<EVASIONS>), it could lead to illegal moves being played that do not actually get the king out of check.
-
How to investigate: Make sure that the cheat_move_pruning doesn't interfere with the logic that determines the target squares for check evasion (using between_bb and checkers() in movegen.cpp). Ensure that the generated moves block the check or move the king to a safe square. Also, double-check that the heuristic doesn't prevent the king from moving out of check when it's the only legal option.
Check that the King moves are properly generated and validated when the side to move is in check.
template<Color Us, GenType Type>
ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
...
Bitboard b = attacks_bb<KING>(ksq) & (Type == EVASIONS ? ~pos.pieces(Us) : target);
while (b)
*moveList++ = Move(ksq, pop_lsb(b));
...
}
4. Castling Rights and Legality (position.cpp):
-
Problem: The cheat_move_pruning might corrupt the castlingRights or castlingRightsMask, leading to illegal castling moves.
-
How to investigate: Verify that the heuristic doesn't inadvertently clear or modify castling rights when it shouldn't. Ensure that the castling_impeded() function is still correctly identifying squares that are attacked or occupied, preventing illegal castling.
Ensure that the castling moves are only generated when the condition are met.
template<Color Us, GenType Type>
ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
...
if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING))
for (CastlingRights cr : {Us & KING_SIDE, Us & QUEEN_SIDE})
if (!pos.castling_impeded(cr) && pos.can_castle(cr))
*moveList++ = Move::make<CASTLING>(ksq, pos.castling_rook_square(cr));
return moveList;
}
5. State Information Corruption (position.cpp):
-
Problem: The cheat_move_pruning might be corrupting the StateInfo object, particularly the checkersBB or blockersForKing bitboards. This could lead to incorrect assessments of check status and, consequently, illegal move generation.
-
How to investigate: Check how the heuristic modifies or interacts with the StateInfo object during its operation. Use assertions to ensure that these bitboards remain consistent and accurate. Also, check the set_check_info function.
Verify the integrity of st->checkSquares after the cheat_move_pruning is applied.
void Position::set_check_info() const {
update_slider_blockers(WHITE);
update_slider_blockers(BLACK);
Square ksq = square<KING>(~sideToMove);
st->checkSquares[PAWN] = pawn_attacks_bb(~sideToMove, ksq);
st->checkSquares[KNIGHT] = attacks_bb<KNIGHT>(ksq);
st->checkSquares[BISHOP] = attacks_bb<BISHOP>(ksq, pieces());
st->checkSquares[ROOK] = attacks_bb<ROOK>(ksq, pieces());
st->checkSquares[QUEEN] = st->checkSquares[BISHOP] | st->checkSquares[ROOK];
st->checkSquares[KING] = 0;
}
Debugging Strategies:
- Assertions: Add assertions throughout the
cheat_move_pruning code and the legal() function to verify that key conditions are met (e.g., the king is not left in check, castling rights are valid, etc.).
- Logging: Introduce logging statements to track the moves being generated, the results of the
legal() check, and the values of relevant variables in StateInfo.
- Minimal Reproducible Example: Try to create a simplified test case (a specific FEN string) that consistently triggers the illegal move generation. This will make debugging much easier.
- Step-by-Step Execution: Use a debugger to step through the code when the illegal move is generated, paying close attention to the values of variables and the flow of execution.
- Compare with Standard Search: Temporarily disable the
cheat_move_pruning and compare the generated moves with the standard search. This can help pinpoint where the divergence occurs.
By systematically investigating these areas and using the debugging techniques, you should be able to identify the root cause of the illegal move generation and fix the issue. Remember to focus on the interactions between the new heuristic and the existing move generation and legality checking mechanisms.