[moves, position] Improve the error messages when asserting during legal move generation

The move generators should only generate moves that can be made, so calling
make_move() and unmake_move() should never give an error during legal move
generation. Both of these calls assert that the result is not an Err(). Improve
the error messaging so that they log the move, the current board position, and
the error message. Highlight the squares relevant to the move (origin, target,
and capture) when printing the board.
This commit is contained in:
Eryn Wells 2025-06-07 19:35:32 -07:00
parent a9674e3215
commit 8fd7ffa586
2 changed files with 26 additions and 4 deletions

View file

@ -1,6 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
use crate::defs::{Kind, PromotionShape};
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Rank, Shape, Square, Wing};
use std::fmt;
@ -215,6 +216,19 @@ impl Move {
}
}
impl Move {
pub fn relevant_squares(&self) -> BitBoard {
[
Some(self.origin_square()),
Some(self.target_square()),
self.capture_square(),
]
.into_iter()
.flatten()
.collect()
}
}
impl Move {
fn transfer_char(self) -> char {
if self.is_capture() || self.is_en_passant() {

View file

@ -110,13 +110,21 @@ impl Position {
let ply: Move = ply.clone().into();
let record = test_board
.make_move(ply, ValidateMove::No)
.expect("unable to make generated move");
.unwrap_or_else(|err| {
panic!(
"unable to make generated move [{ply}]: {err}\n\n{}",
test_board.display().highlight(ply.relevant_squares())
);
});
let move_is_legal = !test_board.color_is_in_check(Some(active_color_before_move));
test_board
.unmake_move(&record)
.expect("unable to unmake generated move");
test_board.unmake_move(&record).unwrap_or_else(|err| {
panic!(
"unable to unmake generated move [{ply}]: {err}\n\n{}",
test_board.display().highlight(ply.relevant_squares())
);
});
move_is_legal
}))