From 43dc24718c442ef45f6cecf5790df0ab84a72cfc Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 23 Feb 2023 13:21:57 +0300 Subject: refactor: apply clippy suggestions --- src/grossmeister/mod.rs | 2 +- src/grossmeister/move_generation.rs | 23 +++++------- src/grossmeister/search.rs | 73 ++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 57 deletions(-) (limited to 'src/grossmeister') diff --git a/src/grossmeister/mod.rs b/src/grossmeister/mod.rs index b7c134d..6ed5f17 100644 --- a/src/grossmeister/mod.rs +++ b/src/grossmeister/mod.rs @@ -26,7 +26,7 @@ pub struct Grossmeister { impl Grossmeister { pub fn new(board: Board) -> Self { - return Self { + Self { board, attacks: Attacks::new(), transposition_table: vec![None; TTABLE_SIZE as usize], diff --git a/src/grossmeister/move_generation.rs b/src/grossmeister/move_generation.rs index 25d92c7..c3acc91 100644 --- a/src/grossmeister/move_generation.rs +++ b/src/grossmeister/move_generation.rs @@ -153,7 +153,7 @@ impl Grossmeister { } } } - return moves + moves } /// Evaluate move for move ordering, prioritizing efficient captures @@ -195,25 +195,20 @@ impl Grossmeister { let mut ordered_moves: Vec = moves_with_eval.iter().map(|(m, _)| *m).collect(); // Insert killer moves after winning captures - let equal_capture_index = match moves_with_eval.iter().position(|(m, eval)| m.is_tactical() && *eval == 0.0) { - Some(x) => x, - None => 0, - }; + let equal_capture_index = moves_with_eval + .iter() + .position(|(m, eval)| m.is_tactical() && *eval == 0.0) + .unwrap_or(0); for killer in killer_moves { - match ordered_moves.iter().position(|m| *m == killer) { - Some(index) => { + if let Some(index) = ordered_moves.iter().position(|m| *m == killer) { let mov = ordered_moves.remove(index); ordered_moves.insert(equal_capture_index, mov); - } - None => {} - }; - + } } - match self.transposition() { - Some(transposition) => ordered_moves.insert(0, transposition.mov), - None => {}, + if let Some(transposition) = self.transposition() { + ordered_moves.insert(0, transposition.mov); } ordered_moves diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index 57ce672..3efa9f8 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -21,30 +21,27 @@ impl Grossmeister { let mut killer_moves = Vec::new(); let color = self.board.color(); - match self.transposition() { - Some(transposition) => { - if transposition.depth == depth_left { - match transposition.node_type { - NodeType::PV => { // PV-nodes have exact score + if let Some(transposition) = self.transposition() { + if transposition.depth == depth_left { + match transposition.node_type { + NodeType::PV => { // PV-nodes have exact score + principal_variation.push(transposition.mov); + return (transposition.score, principal_variation); + } + NodeType::Cut => { + if transposition.score >= beta { principal_variation.push(transposition.mov); - return (transposition.score, principal_variation); - } - NodeType::Cut => { - if transposition.score >= beta { - principal_variation.push(transposition.mov); - return (beta, principal_variation); - } + return (beta, principal_variation); } - NodeType::All => { - if transposition.score <= alpha { - principal_variation.push(transposition.mov); - return (alpha, principal_variation); - } + } + NodeType::All => { + if transposition.score <= alpha { + principal_variation.push(transposition.mov); + return (alpha, principal_variation); } } } } - None => {}, } if depth_left == 0 { @@ -57,9 +54,9 @@ impl Grossmeister { let mut should_pv_search = true; let mut legal_move_found = false; for mov in moves { - let ep_target_before = self.board.ep_target.clone(); - let castling_rights_before = self.board.castling_rights.clone(); - let hash_before = self.board.hash.clone(); + let ep_target_before = self.board.ep_target; + let castling_rights_before = self.board.castling_rights; + let hash_before = self.board.hash; let captured_piece = self.board.make_move(mov); if !self.board.is_king_in_check(color) { @@ -135,10 +132,8 @@ impl Grossmeister { } } - if !legal_move_found { - if self.board.is_king_in_check(color) { - return (-VALUE_WIN, principal_variation); - } + if !legal_move_found && self.board.is_king_in_check(color) { + return (-VALUE_WIN, principal_variation); } (alpha, principal_variation) @@ -161,18 +156,14 @@ impl Grossmeister { } // If we are not in check, we can only search tactical moves - moves = moves - .iter() - .filter(|m| m.is_tactical()) - .map(|m| *m) - .collect() + moves.retain(|m| m.is_tactical()) } let mut legal_move_found = false; for mov in moves { - let ep_target_before = self.board.ep_target.clone(); - let castling_rights_before = self.board.castling_rights.clone(); - let hash_before = self.board.hash.clone(); + let ep_target_before = self.board.ep_target; + let castling_rights_before = self.board.castling_rights; + let hash_before = self.board.hash; let captured_piece = self.board.make_move(mov); if !self.board.is_king_in_check(color) { @@ -191,10 +182,8 @@ impl Grossmeister { } } - if !legal_move_found { - if self.board.is_king_in_check(color) { - return -VALUE_WIN - } + if !legal_move_found && self.board.is_king_in_check(color) { + return -VALUE_WIN } alpha @@ -252,7 +241,7 @@ impl Grossmeister { continue; } - if search_result.1.len() > 0 { + if !search_result.1.is_empty() { depth += 1; gradual_widening_counter = 0; alpha = search_result.0 - window_size; @@ -265,7 +254,7 @@ impl Grossmeister { } match result { - Some(r) => return r, + Some(r) => r, None => panic!("Could not find a move in time"), } } @@ -287,9 +276,9 @@ impl Grossmeister { } for mov in moves { - let ep_target_before = self.board.ep_target.clone(); - let castling_rights_before = self.board.castling_rights.clone(); - let hash_before = self.board.hash.clone(); + let ep_target_before = self.board.ep_target; + let castling_rights_before = self.board.castling_rights; + let hash_before = self.board.hash; let captured_piece = self.board.make_move(mov); // King can not be in check after our own move if !self.board.is_king_in_check(color) { -- cgit v1.2.3