From 8fd7ffa58639a9180eb1ff3b34fff401cdc2d069 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 7 Jun 2025 19:35:32 -0700 Subject: [PATCH] [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. --- moves/src/moves.rs | 14 ++++++++++++++ position/src/position.rs | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/moves/src/moves.rs b/moves/src/moves.rs index 94ef0ad..c1fda0c 100644 --- a/moves/src/moves.rs +++ b/moves/src/moves.rs @@ -1,6 +1,7 @@ // Eryn Wells 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() { diff --git a/position/src/position.rs b/position/src/position.rs index 17f36eb..332bf65 100644 --- a/position/src/position.rs +++ b/position/src/position.rs @@ -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 }))