The issue likely arises because your cheat_move_pruning heuristic bypasses the normal checks and balances for verifying the legality of moves. Here’s an analysis of potential causes and where to investigate:
1. Move Legality Checks are Bypassed
Stockfish incorporates strict legality checks to ensure moves are valid in the current position. These include:
- Pseudo-legal move generation: Moves that are valid based on piece movement rules.
- Legality enforcement: Ensuring the move doesn’t leave the king in check.
If your cheat_move_pruning skips or improperly handles these checks, illegal moves may slip through.
Relevant Code
-
Position::legal(Move m) in position.cpp:
bool Position::legal(Move m) const {
...
if (m.type_of() == EN_PASSANT) {
...
// Ensure the king is not left in check after an en passant capture
}
...
return true;
}
If cheat_move_pruning bypasses Position::legal, illegal moves (e.g., moves leaving the king in check) might be generated.
-
generate<LEGAL> in movegen.cpp:
template<>
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
...
moveList = pos.checkers() ? generate<EVASIONS>(pos, moveList)
: generate<NON_EVASIONS>(pos, moveList);
...
// Remove any moves that are not strictly legal
while (cur != moveList) {
if (!pos.legal(*cur))
*cur = *(--moveList);
else
++cur;
}
return moveList;
}
If your heuristic modifies move generation without ensuring legality, illegal moves may appear in the search tree.
2. do_move or undo_move State Corruption
Stockfish relies on do_move and undo_move to incrementally update the position. If your cheat_move_pruning alters the state improperly, it could lead to:
- Incorrect board state.
- Inconsistent Zobrist hash keys.
- Illegal moves being considered valid.
Relevant Code
-
Position::do_move(Move m, StateInfo& newSt) in position.cpp:
void Position::do_move(Move m, StateInfo& newSt) {
...
// Update the position state incrementally
move_piece(from, to);
...
}
If cheat_move_pruning modifies the board state (e.g., moves a piece to an illegal square) without proper reversal in undo_move, the search may propagate an invalid position.
-
Position::undo_move(Move m) in position.cpp:
void Position::undo_move(Move m) {
...
// Restore the position state
move_piece(to, from);
...
}
Ensure the cheat logic correctly restores the position.
3. Inconsistent Evaluation Shortcut
Your heuristic seems to involve evaluating a position after applying a cheat move. If the evaluation or legality check is skipped for speed, the engine may misinterpret the position as valid.
Relevant Code
Search::Worker::search in search.cpp:
template<NodeType nodeType>
Value Search::Worker::search(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) {
...
// Generate moves
ExtMove* endMoves = generate<LEGAL>(pos, moveList);
...
for (ExtMove* move = moveList; move != endMoves; ++move) {
...
// Do the move
pos.do_move(m, newSt);
...
// Undo the move
pos.undo_move(m);
}
...
}
If your heuristic modifies generate<LEGAL> or skips legality checks in the search, illegal moves may propagate.
4. Cheating Logic May Skip King Safety
A common cause of illegal moves in chess engines is failing to ensure the king's safety (e.g., a move leaving the king in check). Ensure cheat_move_pruning respects king safety.
Relevant Code
5. Potential Bugs in cheat_move_pruning
If cheat_move_pruning modifies the move list or position state:
- Moves generated must still pass legality checks.
- The position must be restored properly after a cheat move is undone.
Debugging Steps
-
Add Assertions in do_move and undo_move:
Verify the position state after every move.
assert(pos.pos_is_ok());
-
Trace Move Generation and Legality:
Log moves generated by cheat_move_pruning and ensure they pass Position::legal.
-
Verify Evaluation Consistency:
Ensure the evaluation logic doesn’t short-circuit legality checks for cheat moves.
-
Reproduce the Bug:
Use a minimized test case to trigger the issue and identify the exact sequence of events.
Likely Cause
The most probable cause is that cheat_move_pruning skips Position::legal or improperly modifies the position state, leading to illegal moves being considered valid. Specifically:
- A cheat move may leave the king in check or violate other legality rules.
- The position state may become inconsistent after applying or undoing a cheat move.
Fix Suggestions
-
Ensure Legality Checks for Cheat Moves:
Explicitly verify cheat moves with Position::legal.
-
Restore State Correctly:
Verify do_move and undo_move handle cheat moves properly.
-
Integrate with Existing Move Generation:
Modify generate<LEGAL> to include cheat moves while maintaining legality checks.
-
Add Debugging Assertions:
Use assert(pos.pos_is_ok()) to detect state corruption early.
By addressing these points, you should be able to resolve the issue of illegal moves in your cheat_move_pruning heuristic.