From 99dd2d1be26ce02ac43e11a9e60f0036f118becd Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 3 May 2025 15:47:56 -0700 Subject: [PATCH] [board] Remove the Builder --- board/src/builder.rs | 166 ------------------------------------------- board/src/lib.rs | 2 - 2 files changed, 168 deletions(-) delete mode 100644 board/src/builder.rs diff --git a/board/src/builder.rs b/board/src/builder.rs deleted file mode 100644 index 434e517..0000000 --- a/board/src/builder.rs +++ /dev/null @@ -1,166 +0,0 @@ -// Eryn Wells - -use crate::{castle, piece_sets::Mailbox, Board, Castle, EnPassant, MoveCounter, PieceSet}; -use chessfriend_core::{piece, Color, PlacedPiece, Rank, Shape, Square}; - -#[derive(Clone)] -pub struct Builder { - castling_rights: castle::Rights, - pieces: Mailbox, - kings: [Option; Color::NUM], - en_passant: Option, - move_counter: MoveCounter, -} - -impl Builder { - #[must_use] - pub fn new() -> Self { - Self::default() - } - - #[must_use] - pub fn from_board(board: &Board) -> Self { - let pieces = board.iter_all_pieces().collect::(); - - let white_king = board.king_square(Color::White); - let black_king = board.king_square(Color::Black); - - Self { - pieces, - castling_rights: board.castling_rights, - kings: [Some(white_king), Some(black_king)], - en_passant: board.en_passant(), - move_counter: board.move_counter, - } - } - - pub fn to_move(&mut self, player: Color) -> &mut Self { - self.move_counter.active_color = player; - self - } - - pub fn fullmove_number(&mut self, num: u16) -> &mut Self { - self.move_counter.fullmove_number = num; - self - } - - pub fn halfmove_number(&mut self, num: u16) -> &mut Self { - self.move_counter.halfmove_number = num; - self - } - - pub fn en_passant(&mut self, en_passant: Option) -> &mut Self { - self.en_passant = en_passant; - self - } - - pub fn place_piece(&mut self, piece: PlacedPiece) -> &mut Self { - let square = piece.square(); - let shape = piece.shape(); - - if shape == Shape::King { - let color = piece.color(); - let color_index: usize = color as usize; - - if let Some(king_square) = self.kings[color_index] { - self.pieces.remove(king_square); - } - self.kings[color_index] = Some(square); - } - - self.pieces.set(piece.piece(), square); - - self - } - - pub fn player_can_castle(&mut self, color: Color, castle: Castle) -> &mut Self { - self.castling_rights - .set_player_has_right_to_castle_flag(color, castle); - self - } - - pub fn no_castling_rights(&mut self) -> &mut Self { - self.castling_rights.clear_all(); - self - } - - pub fn build(&self) -> Board { - let pieces: PieceSet = self - .pieces - .iter() - .filter(Self::is_piece_placement_valid) - .collect(); - - let mut castling_rights = self.castling_rights; - - for color in Color::ALL { - for castle in Castle::ALL { - let parameters = castle.parameters(color); - - let has_rook_on_starting_square = self - .pieces - .get(parameters.rook_origin_square()) - .is_some_and(|piece| piece.shape() == Shape::Rook); - let king_is_on_starting_square = - self.kings[color as usize] == Some(parameters.king_origin_square()); - - if !king_is_on_starting_square || !has_rook_on_starting_square { - castling_rights.clear_player_has_right_to_castle_flag(color, castle); - } - } - } - - Board { - pieces, - self.en_passant, - move_counter: self.move_counter, - castling_rights, - } - } -} - -impl Builder { - fn is_piece_placement_valid(piece: &PlacedPiece) -> bool { - if piece.shape() == Shape::Pawn { - // Pawns cannot be placed on the first (back) rank of their side, - // and cannot be placed on the final rank without a promotion. - let rank = piece.square().rank(); - return rank != Rank::ONE && rank != Rank::EIGHT; - } - - true - } -} - -impl Default for Builder { - fn default() -> Self { - let white_king_square = Square::E1; - let black_king_square = Square::E8; - - let pieces = Mailbox::from_iter([ - (white_king_square, piece!(White King)), - (black_king_square, piece!(Black King)), - ]); - - Self { - castling_rights: castle::Rights::default(), - pieces, - kings: [Some(white_king_square), Some(black_king_square)], - en_passant: None, - move_counter: MoveCounter::default(), - } - } -} - -#[cfg(test)] -mod tests { - use crate::Builder; - use chessfriend_core::piece; - - #[test] - fn place_piece() { - let piece = piece!(White Queen on E4); - let builder = Builder::new().place_piece(piece).build(); - assert_eq!(builder.piece_on_square(piece.square()), Some(piece)); - } -} diff --git a/board/src/lib.rs b/board/src/lib.rs index 35c7803..10916c6 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -8,11 +8,9 @@ pub mod macros; pub mod move_counter; mod board; -mod builder; mod piece_sets; pub use board::Board; -pub use builder::Builder; use castle::Castle; use en_passant::EnPassant;