diff options
| author | eug-vs <eugene@eug-vs.xyz> | 2023-02-23 13:21:57 +0300 | 
|---|---|---|
| committer | eug-vs <eugene@eug-vs.xyz> | 2023-02-23 14:01:19 +0300 | 
| commit | 43dc24718c442ef45f6cecf5790df0ab84a72cfc (patch) | |
| tree | 16f446b1d5a3e0fdf638e0bcab16cf34ec338bd8 /src/board | |
| parent | f60c573ba71207c18a28413e3940a4e21b07c73f (diff) | |
| download | chessnost-43dc24718c442ef45f6cecf5790df0ab84a72cfc.tar.gz | |
refactor: apply clippy suggestions
Diffstat (limited to 'src/board')
| -rw-r--r-- | src/board/io.rs | 2 | ||||
| -rw-r--r-- | src/board/mod.rs | 59 | ||||
| -rw-r--r-- | src/board/zobrist.rs | 25 | 
3 files changed, 44 insertions, 42 deletions
| diff --git a/src/board/io.rs b/src/board/io.rs index ca75c8c..67aa096 100644 --- a/src/board/io.rs +++ b/src/board/io.rs @@ -8,7 +8,7 @@ const PIECE_CHARS: [&str; 12] = [  /// Input/Output operations with Board  pub trait IO { -    fn print(&self) -> (); +    fn print(&self);      #[allow(non_snake_case)]      fn from_FEN(fen: String) -> Self; diff --git a/src/board/mod.rs b/src/board/mod.rs index 579488b..aab3136 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -32,6 +32,13 @@ pub struct Board {      attacks: Attacks,  } +impl Default for Board { +    fn default() -> Self { +        Board::new() +    } + +} +  impl Board {      pub fn new() -> Self {          let default_fen = String::from("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); @@ -68,7 +75,7 @@ impl Board {              .iter()              .enumerate()              .find(|(_, bitboard)| *bitboard & square_bb > 0) -            .and_then(|(pt, _)| Some(Piece::from(pt))) +            .map(|(pt, _)| Piece::from(pt))      }      pub fn ep_bitboard(&self) -> Bitboard { @@ -143,7 +150,7 @@ impl Board {                          self.zobrist_toggle_piece(source_piece, mov.target);                      }                  } -                Piece::from(source_piece) +                source_piece              },              None => {                  self.print(); @@ -290,22 +297,19 @@ impl Board {          }          // Return captured piece to target square -        match captured_piece { -            Some(target_piece) => { -                match mov.kind { -                    // Return pawn captured by En Passant pawn if needed -                    MoveKind::EnPassant => { -                        let original_dead_pawn_bb = Square::from_coords(mov.source.rank(), mov.target.file()).to_bitboard(); -                        self.piece_sets[target_piece as usize] |= original_dead_pawn_bb; -                        self.occupancy                     |= original_dead_pawn_bb; -                    }, -                    _ => { -                        self.piece_sets[target_piece as usize] |= move_target_bb; -                        self.occupancy                     |= move_target_bb; -                    }, -                } -            }, -            None => {} +        if let Some(target_piece) = captured_piece { +            match mov.kind { +                // Return pawn captured by En Passant pawn if needed +                MoveKind::EnPassant => { +                    let original_dead_pawn_bb = Square::from_coords(mov.source.rank(), mov.target.file()).to_bitboard(); +                    self.piece_sets[target_piece as usize] |= original_dead_pawn_bb; +                    self.occupancy                     |= original_dead_pawn_bb; +                }, +                _ => { +                    self.piece_sets[target_piece as usize] |= move_target_bb; +                    self.occupancy                     |= move_target_bb; +                }, +            }          } @@ -381,16 +385,15 @@ mod tests {      fn make_move() {          let fen = String::from("q1b2k2/5p1p/4p1pb/pPPp4/3N4/3nPB2/P2QKnR1/1R6 w - - 0 25");          let mut board = Board::from_FEN(fen); -        let initial_board = board.clone(); +        let initial_board = board;          board.print();          let black_move = Move { source: Square::F7, target: Square::F5, kind: MoveKind::Quiet };          println!("\n{:?}", black_move); -        match board.make_move(black_move) { -            Some(..) => panic!("No piece should be captured"), -            None => {}, -        }; +        if let Some(..) = board.make_move(black_move) { +            panic!("No piece should be captured"); +        }          board.print(); @@ -423,7 +426,7 @@ mod tests {      fn unmake_move() {          let fen = String::from("q1b2k2/5p1p/4p1pb/pPPp4/3N4/3nPB2/P2QKnR1/1R6 w - - 0 25");          let mut board = Board::from_FEN(fen); -        let initial_board = board.clone(); +        let initial_board = board;          let mov = Move { source: Square::D2, target: Square::A5, kind: MoveKind::Capture }; @@ -442,9 +445,9 @@ mod tests {      fn is_square_attacked() {          let board = Board::new(); -        assert_eq!(board.is_square_attacked(Square::E2, Color::White), true); -        assert_eq!(board.is_square_attacked(Square::E2, Color::Black), false); -        assert_eq!(board.is_square_attacked(Square::E4, Color::White), false); -        assert_eq!(board.is_square_attacked(Square::B6, Color::Black), true); +        assert!(board.is_square_attacked(Square::E2, Color::White)); +        assert!(!board.is_square_attacked(Square::E2, Color::Black)); +        assert!(!board.is_square_attacked(Square::E4, Color::White)); +        assert!(board.is_square_attacked(Square::B6, Color::Black));      }  } diff --git a/src/board/zobrist.rs b/src/board/zobrist.rs index 5286a73..88a2fb7 100644 --- a/src/board/zobrist.rs +++ b/src/board/zobrist.rs @@ -18,12 +18,12 @@ pub trait Zobrist {      /// Compute store zobrist hash of the current position      /// https://www.chessprogramming.org/Zobrist_Hashing -    fn compute_hash(&mut self) -> (); +    fn compute_hash(&mut self); -    fn zobrist_toggle_piece(&mut self, piece_type: Piece, square: Square) -> (); -    fn zobrist_toggle_castling_right(&mut self, color: Color, side: CastlingSide) -> (); -    fn zobrist_toggle_ep_square(&mut self, ep_square: Square) -> (); -    fn zobrist_toggle_color(&mut self) -> (); +    fn zobrist_toggle_piece(&mut self, piece_type: Piece, square: Square); +    fn zobrist_toggle_castling_right(&mut self, color: Color, side: CastlingSide); +    fn zobrist_toggle_ep_square(&mut self, ep_square: Square); +    fn zobrist_toggle_color(&mut self);  }  impl Zobrist for Board { @@ -32,7 +32,7 @@ impl Zobrist for Board {          [(); ZOBRIST_SEED_SIZE].map(|_| rng.gen())      } -    fn compute_hash(&mut self) -> () { +    fn compute_hash(&mut self) {          self.hash = 0;          for piece_id in 0..self.piece_sets.len() { @@ -48,9 +48,8 @@ impl Zobrist for Board {              }          } -        match self.ep_target { -            Some(square) => self.zobrist_toggle_ep_square(square), -            None => {}, +        if let Some(square) = self.ep_target { +            self.zobrist_toggle_ep_square(square);          }          if self.color() == Color::Black { @@ -58,19 +57,19 @@ impl Zobrist for Board {          }      } -    fn zobrist_toggle_piece(&mut self, piece_type: Piece, square: Square) -> () { +    fn zobrist_toggle_piece(&mut self, piece_type: Piece, square: Square) {          self.hash ^= self.zobrist_seed[piece_type as usize * TOTAL_SQUARES + square as usize];      } -    fn zobrist_toggle_castling_right(&mut self, color: Color, side: CastlingSide) -> () { +    fn zobrist_toggle_castling_right(&mut self, color: Color, side: CastlingSide) {          self.hash ^= self.zobrist_seed[(TOTAL_PIECES * TOTAL_SQUARES) + color as usize * 2 + side as usize];      } -    fn zobrist_toggle_ep_square(&mut self, ep_square: Square) -> () { +    fn zobrist_toggle_ep_square(&mut self, ep_square: Square) {          self.hash ^= self.zobrist_seed[(TOTAL_PIECES * TOTAL_SQUARES + TOTAL_CASTLING_RIGHTS) + ep_square.file() as usize];      } -    fn zobrist_toggle_color(&mut self) -> () { +    fn zobrist_toggle_color(&mut self) {          self.hash ^= self.zobrist_seed[ZOBRIST_SEED_SIZE - 1];      }  } | 
