From 7f1890608dc00f809c7988d683592f1fab5b465e Mon Sep 17 00:00:00 2001 From: dece Date: Thu, 11 Jun 2020 19:33:18 +0200 Subject: [PATCH] engine: enhance debug output --- src/board.rs | 15 ++++++++++++++- src/engine.rs | 16 ++++++++++++++++ src/notation.rs | 4 ++++ src/rules.rs | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/board.rs b/src/board.rs index 5955689..afb73d1 100644 --- a/src/board.rs +++ b/src/board.rs @@ -233,7 +233,7 @@ pub fn draw(board: &Board, f: &mut dyn std::io::Write) { } writeln!(f, "{} {}", r + 1, rank).unwrap(); } - writeln!(f, " abcdefgh").unwrap(); + write!(f, " abcdefgh").unwrap(); } /// Storage for precomputed board pieces stats. @@ -274,6 +274,19 @@ impl BoardStats { } } +impl std::fmt::Display for BoardStats { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!( + f, + "{}P {}B {}N {}R {}Q {}K {}dp {}bp {}ip {}m", + self.num_pawns, self.num_bishops, self.num_knights, self.num_rooks, + self.num_queens, self.num_kings, + self.num_doubled_pawns, self.num_backward_pawns, self.num_isolated_pawns, + self.mobility + ) + } +} + /// Create two new BoardStats objects from the board, for white and black. /// /// See `compute_stats_into` for details. diff --git a/src/engine.rs b/src/engine.rs index 05703f0..d4052dc 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -54,6 +54,22 @@ impl std::fmt::Debug for Node { } } +impl std::fmt::Display for Node { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let mut s = vec!(); + board::draw(&self.board, &mut s); + let board_drawing = String::from_utf8_lossy(&s).to_string(); + write!( + f, + "* Board:\n{}\n\ + * Game state:\n{}\n\ + * White statistics:\n{}\n\ + * Black statistics:\n{}", + board_drawing, self.game_state, self.stats.0, self.stats.1 + ) + } +} + /// Engine communication mode. enum Mode { /// No mode, sit here and do nothing. diff --git a/src/notation.rs b/src/notation.rs index e5aca4b..350afb3 100644 --- a/src/notation.rs +++ b/src/notation.rs @@ -75,6 +75,10 @@ pub fn parse_fen_fields(fields: &[&str]) -> Option { }) } +pub fn en_passant_to_string(ep: Option) -> String { + ep.and_then(|p| Some(board::pos_string(&p))).unwrap_or("-".to_string()) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/rules.rs b/src/rules.rs index 1e9a9e5..a4591e0 100644 --- a/src/rules.rs +++ b/src/rules.rs @@ -1,6 +1,7 @@ //! Functions to determine legal moves. use crate::board::*; +use crate::notation; /// Characteristics of the state of a game. /// @@ -30,6 +31,21 @@ impl GameState { } } +impl std::fmt::Display for GameState { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!( + f, + "- color: {}\n\ + - castling: {:04b}\n\ + - en_passant: {}\n\ + - halfmove: {}\n\ + - fullmove: {}", + self.color, self.castling, notation::en_passant_to_string(self.en_passant), + self.halfmove, self.fullmove + ) + } +} + pub const CASTLING_WH_K: u8 = 0b00000001; pub const CASTLING_WH_Q: u8 = 0b00000010; pub const CASTLING_WH_MASK: u8 = 0b00000011;