engine: enhance debug output

This commit is contained in:
dece 2020-06-11 19:33:18 +02:00
parent b334d55abf
commit 7f1890608d
4 changed files with 50 additions and 1 deletions

View file

@ -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.

View file

@ -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.

View file

@ -75,6 +75,10 @@ pub fn parse_fen_fields(fields: &[&str]) -> Option<Fen> {
})
}
pub fn en_passant_to_string(ep: Option<board::Pos>) -> String {
ep.and_then(|p| Some(board::pos_string(&p))).unwrap_or("-".to_string())
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -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;