From fa1c6b452e33b079e7c7f3fe740b668afc82217e Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 21 Jan 2024 10:38:50 -0800 Subject: [PATCH] [board] Remove BoardSide enum; use Castle everywhere Move Castle to a castle module inside the move module. Implement into_index() on Castle to turn it into a usize. --- board/src/lib.rs | 2 +- board/src/move.rs | 87 ++++++++++++++++--- board/src/position/builders/move_builder.rs | 6 +- .../src/position/builders/position_builder.rs | 17 ++-- board/src/position/flags.rs | 6 +- board/src/position/mod.rs | 2 - board/src/position/position.rs | 17 ---- board/src/square.rs | 21 ++--- 8 files changed, 95 insertions(+), 63 deletions(-) diff --git a/board/src/lib.rs b/board/src/lib.rs index d91df46..b267517 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -14,7 +14,7 @@ mod square; pub use piece::{Color, Piece}; pub use position::{Position, PositionBuilder}; -pub use r#move::{MakeMoveError, Move, MoveBuilder}; +pub use r#move::{Castle, MakeMoveError, Move, MoveBuilder}; pub use square::{File, Rank, Square}; pub(crate) use bitboard::BitBoard; diff --git a/board/src/move.rs b/board/src/move.rs index 45c3f7b..3e48c50 100644 --- a/board/src/move.rs +++ b/board/src/move.rs @@ -2,12 +2,12 @@ use crate::{ piece::{Piece, PlacedPiece, Shape}, - position::BoardSide, square::Rank, Square, }; use std::fmt; +pub use castle::Castle; pub(crate) use move_formatter::AlgebraicMoveFormatter; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -18,18 +18,81 @@ pub enum MakeMoveError { IllegalCastle, } -#[repr(u16)] -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub enum Castle { - KingSide = 0b10, - QueenSide = 0b11, -} +mod castle { + use crate::{Color, Square}; -impl From for Castle { - fn from(value: BoardSide) -> Self { - match value { - BoardSide::King => Castle::KingSide, - BoardSide::Queen => Castle::QueenSide, + #[repr(u16)] + #[derive(Copy, Clone, Debug, Eq, PartialEq)] + pub enum Castle { + KingSide = 0b10, + QueenSide = 0b11, + } + + pub(crate) struct Squares { + pub king: Square, + pub rook: Square, + } + + impl Castle { + const STARTING_SQUARES: [[Squares; 2]; 2] = [ + [ + Squares { + king: Square::E1, + rook: Square::H1, + }, + Squares { + king: Square::E1, + rook: Square::A1, + }, + ], + [ + Squares { + king: Square::E8, + rook: Square::H8, + }, + Squares { + king: Square::E8, + rook: Square::A8, + }, + ], + ]; + + const TARGET_SQUARES: [[Squares; 2]; 2] = [ + [ + Squares { + king: Square::G1, + rook: Square::F1, + }, + Squares { + king: Square::C1, + rook: Square::D1, + }, + ], + [ + Squares { + king: Square::G8, + rook: Square::F8, + }, + Squares { + king: Square::C8, + rook: Square::D8, + }, + ], + ]; + + pub(crate) fn starting_squares(&self, color: Color) -> &'static Squares { + &Castle::STARTING_SQUARES[color as usize][self.into_index()] + } + + pub(crate) fn target_squares(&self, color: Color) -> &'static Squares { + &Castle::TARGET_SQUARES[color as usize][self.into_index()] + } + + pub(crate) fn into_index(&self) -> usize { + match self { + Castle::KingSide => 0, + Castle::QueenSide => 1, + } } } } diff --git a/board/src/position/builders/move_builder.rs b/board/src/position/builders/move_builder.rs index 1dac36c..e899793 100644 --- a/board/src/position/builders/move_builder.rs +++ b/board/src/position/builders/move_builder.rs @@ -176,12 +176,14 @@ impl<'p> Builder<'p, ValidatedMove> { } => { let mut pieces = self.position.piece_bitboards().clone(); + let target_squares = castle.target_squares(player); + let king_from: BitBoard = king.square().into(); - let king_to: BitBoard = Square::king_castle_target(player, castle).into(); + let king_to: BitBoard = target_squares.king.into(); *pieces.bitboard_for_piece_mut(king.piece()) ^= king_from | king_to; let rook_from: BitBoard = rook.square().into(); - let rook_to: BitBoard = Square::rook_castle_target(player, castle).into(); + let rook_to: BitBoard = target_squares.rook.into(); *pieces.bitboard_for_piece_mut(rook.piece()) ^= rook_from | rook_to; *pieces.bitboard_for_color_mut(player) &= diff --git a/board/src/position/builders/position_builder.rs b/board/src/position/builders/position_builder.rs index cd895f9..d9c0a18 100644 --- a/board/src/position/builders/position_builder.rs +++ b/board/src/position/builders/position_builder.rs @@ -3,7 +3,7 @@ use crate::{ bitboard::BitBoardBuilder, piece::{PlacedPiece, Shape}, - position::{flags::Flags, piece_sets::PieceBitBoards, BoardSide}, + position::{flags::Flags, piece_sets::PieceBitBoards}, r#move::Castle, square::{Direction, Rank}, BitBoard, Color, MakeMoveError, Move, Piece, Position, Square, @@ -64,22 +64,19 @@ impl Builder { impl Default for Builder { fn default() -> Self { + let white_king_square = Square::king_starting_square(Color::White); + let black_king_square = Square::king_starting_square(Color::Black); + let pieces = BTreeMap::from_iter([ - ( - Square::KING_STARTING_SQUARES[Color::White as usize], - piece!(White King), - ), - ( - Square::KING_STARTING_SQUARES[Color::Black as usize], - piece!(Black King), - ), + (white_king_square, piece!(White King)), + (black_king_square, piece!(Black King)), ]); Self { player_to_move: Color::White, flags: Flags::default(), pieces: pieces, - kings: Square::KING_STARTING_SQUARES, + kings: [white_king_square, black_king_square], } } } diff --git a/board/src/position/flags.rs b/board/src/position/flags.rs index 8bc0550..e92b9f6 100644 --- a/board/src/position/flags.rs +++ b/board/src/position/flags.rs @@ -1,6 +1,5 @@ // Eryn Wells -use super::position::BoardSide; use crate::{r#move::Castle, Color}; use std::fmt; @@ -9,9 +8,8 @@ pub(super) struct Flags(u8); impl Flags { #[inline] - pub(super) fn player_has_right_to_castle_flag_offset(color: Color, castle: Castle) -> u8 { - let board_side: BoardSide = castle.into(); - ((color as u8) << 1) + board_side as u8 + pub(super) fn player_has_right_to_castle_flag_offset(color: Color, castle: Castle) -> usize { + ((color as usize) << 1) + castle.into_index() } pub(super) fn player_has_right_to_castle(&self, color: Color, castle: Castle) -> bool { diff --git a/board/src/position/mod.rs b/board/src/position/mod.rs index b6805e3..a03e392 100644 --- a/board/src/position/mod.rs +++ b/board/src/position/mod.rs @@ -11,5 +11,3 @@ pub use builders::{MoveBuilder, PositionBuilder}; pub use diagram_formatter::DiagramFormatter; pub use pieces::Pieces; pub use position::Position; - -pub(crate) use position::BoardSide; diff --git a/board/src/position/position.rs b/board/src/position/position.rs index b547d09..f0535f7 100644 --- a/board/src/position/position.rs +++ b/board/src/position/position.rs @@ -11,23 +11,6 @@ use crate::{ }; use std::{cell::OnceCell, fmt}; -/// A lateral side of the board relative to the player. Kingside is the side the -/// player's king starts on. Queenside is the side of the board the player's -/// queen starts on. -pub(crate) enum BoardSide { - King = 0, - Queen = 1, -} - -impl From for BoardSide { - fn from(value: Castle) -> Self { - match value { - Castle::KingSide => BoardSide::King, - Castle::QueenSide => BoardSide::Queen, - } - } -} - #[derive(Clone, Debug, Eq, PartialEq)] pub struct Position { color_to_move: Color, diff --git a/board/src/square.rs b/board/src/square.rs index b04aede..4501178 100644 --- a/board/src/square.rs +++ b/board/src/square.rs @@ -1,6 +1,6 @@ // Eryn Wells -use crate::{position::BoardSide, r#move::Castle, Color}; +use crate::{r#move::Castle, Color}; use std::{fmt, str::FromStr}; pub enum Direction { @@ -156,25 +156,16 @@ impl Square { } impl Square { - pub(crate) const KING_STARTING_SQUARES: [Square; 2] = [Square::E1, Square::E8]; - pub(crate) const KING_CASTLE_TARGET_SQUARES: [[Square; 2]; 2] = - [[Square::G1, Square::C1], [Square::G8, Square::C8]]; - pub(crate) const ROOK_CASTLE_TARGET_SQUARES: [[Square; 2]; 2] = - [[Square::F1, Square::D1], [Square::F8, Square::D8]]; + const KING_STARTING_SQUARES: [Square; 2] = [Square::E1, Square::E8]; + + pub fn king_starting_square(color: Color) -> Square { + Square::KING_STARTING_SQUARES[color as usize] + } pub fn from_algebraic_str(s: &str) -> Result { s.parse() } - pub fn king_castle_target(player: Color, castle: Castle) -> Square { - let board_side: BoardSide = castle.into(); - Self::KING_CASTLE_TARGET_SQUARES[player as usize][board_side as usize] - } - - pub fn rook_castle_target(player: Color, castle: Castle) -> Square { - let board_side: BoardSide = castle.into(); - Self::ROOK_CASTLE_TARGET_SQUARES[player as usize][board_side as usize] - } pub fn neighbor(self, direction: Direction) -> Option { match direction { Direction::North => Square::try_index(self as usize + 8),