From 1770a4906a9b4cb5f16dc0abafe10330f6342776 Mon Sep 17 00:00:00 2001 From: Saphereye Date: Sun, 6 Oct 2024 16:38:04 +0530 Subject: [PATCH] refactor: clippy --- src/evaluate.rs | 14 ++++++-------- src/main.rs | 31 +++++++++---------------------- src/uci.rs | 9 ++++----- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/evaluate.rs b/src/evaluate.rs index 1f80ab3..8615b57 100644 --- a/src/evaluate.rs +++ b/src/evaluate.rs @@ -163,7 +163,7 @@ pub fn evaluate_board(board: &Board) -> i64 { } // Piece mobility (count legal moves) - let mobility = MoveGen::new_legal(&board) + let mobility = MoveGen::new_legal(board) .filter(|m| m.get_source() == sq) .count() as i32; if board.color_on(sq) == Some(Color::White) { @@ -182,13 +182,11 @@ pub fn evaluate_board(board: &Board) -> i64 { } // Rook on open files - if piece == Piece::Rook { - if rook_on_open_file(sq, board) { - if board.color_on(sq) == Some(Color::White) { - white_rook_bonus += 50; - } else { - black_rook_bonus += 50; - } + if piece == Piece::Rook && rook_on_open_file(sq, board) { + if board.color_on(sq) == Some(Color::White) { + white_rook_bonus += 50; + } else { + black_rook_bonus += 50; } } } diff --git a/src/main.rs b/src/main.rs index c2f9812..a8d20de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use evaluate::evaluate_board; mod uci; use uci::*; -#[derive(Clone)] +#[derive(Clone, Default)] struct Node { board: Board, total_score: i64, @@ -22,22 +22,9 @@ struct Node { move_to_reach: Option, } -impl Default for Node { - fn default() -> Self { - Self { - board: Board::default(), - total_score: 0, - number_of_visits: 0, - first_child: None, - next_sibling: None, - move_to_reach: None, - } - } -} - impl Debug for Node { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - writeln!(f, "")?; + writeln!(f)?; self.fmt_with_indent(f, 0) } } @@ -201,9 +188,9 @@ fn find_best_move(root: &Node) -> ChessMove { best_child.and_then(|child| child.move_to_reach).unwrap() } -const VERSION: &'static str = env!("CARGO_PKG_VERSION"); -const AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS"); -const NAME: &'static str = env!("CARGO_PKG_NAME"); +const VERSION: &str = env!("CARGO_PKG_VERSION"); +const AUTHORS: &str = env!("CARGO_PKG_AUTHORS"); +const NAME: &str = env!("CARGO_PKG_NAME"); fn uci_to_move(uci: &str) -> Result { let from = Square::from_str(&uci[0..2]).map_err(|_| "Invalid from square")?; @@ -229,15 +216,15 @@ fn main() { let mut current_root: *mut Node = &mut root; loop { - unsafe { - dbg!(&*current_root); - } + // unsafe { + // dbg!(&*current_root); + // } let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); let input = input.trim(); match parse(input) { - Ok(("", UCICommand::UCI)) => { + Ok(("", UCICommand::Uci)) => { println!("id name {NAME} {VERSION}"); println!("id author {AUTHORS}"); println!("uciok"); diff --git a/src/uci.rs b/src/uci.rs index da0516c..a46297f 100644 --- a/src/uci.rs +++ b/src/uci.rs @@ -11,8 +11,7 @@ use nom::{ #[derive(Debug, PartialEq)] pub enum UCICommand { - UCI, - Debug(bool), + Uci, IsReady, Position { fen: Option, @@ -89,7 +88,7 @@ pub fn parse_quit(input: &str) -> IResult<&str, UCICommand> { pub fn parse_uci(input: &str) -> IResult<&str, UCICommand> { let (input, _) = tag("uci")(input)?; - Ok((input, UCICommand::UCI)) + Ok((input, UCICommand::Uci)) } pub fn parse_isready(input: &str) -> IResult<&str, UCICommand> { @@ -99,7 +98,7 @@ pub fn parse_isready(input: &str) -> IResult<&str, UCICommand> { fn parse_fen(input: &str) -> IResult<&str, String> { let (input, fen) = - take_until(" moves")(input).or_else(|_: nom::Err>| Ok((input, input)))?; + take_until::<&str, &str, NomError<&str>>(" moves")(input).or(Ok((input, input)))?; Ok((input, fen.trim().to_string())) } @@ -149,7 +148,7 @@ mod tests { #[test] fn test_parse_uci() { let input = "uci"; - let expected = UCICommand::UCI; + let expected = UCICommand::Uci; let result = parse(input); assert_eq!(result, Ok(("", expected))); }