diff --git a/moves/Cargo.toml b/moves/Cargo.toml index b042344..797f7c8 100644 --- a/moves/Cargo.toml +++ b/moves/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" chessfriend_bitboard = { path = "../bitboard" } chessfriend_board = { path = "../board" } chessfriend_core = { path = "../core" } +thiserror = "2" diff --git a/moves/src/defs.rs b/moves/src/defs.rs index 8254cb9..caea420 100644 --- a/moves/src/defs.rs +++ b/moves/src/defs.rs @@ -2,8 +2,9 @@ use chessfriend_core::Shape; +#[repr(u16)] #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) enum Kind { +pub enum Kind { Quiet = 0b0000, DoublePush = 0b0001, KingSideCastle = 0b0010, @@ -14,12 +15,6 @@ pub(crate) enum Kind { CapturePromotion = 0b1100, } -impl Kind { - fn is_reversible(self) -> bool { - (self as u16) & 0b1100 == 0 - } -} - #[repr(u16)] #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum PromotionShape { diff --git a/moves/src/lib.rs b/moves/src/lib.rs index e43a61d..09ccb0d 100644 --- a/moves/src/lib.rs +++ b/moves/src/lib.rs @@ -7,5 +7,5 @@ mod defs; mod moves; pub use builder::{Builder, Error as BuildMoveError, Result as BuildMoveResult}; -pub use defs::PromotionShape; +pub use defs::{Kind, PromotionShape}; pub use moves::Move; diff --git a/moves/src/moves.rs b/moves/src/moves.rs index b7d2539..bc66d77 100644 --- a/moves/src/moves.rs +++ b/moves/src/moves.rs @@ -1,5 +1,6 @@ // Eryn Wells +use crate::builder::Builder; use crate::defs::Kind; use chessfriend_board::castle::Castle; use chessfriend_core::{Rank, Shape, Square}; @@ -57,7 +58,7 @@ impl Move { #[must_use] pub fn is_castle(&self) -> bool { - self.castle().is_some() + (self.0 & 0b0010) != 0 } #[must_use] @@ -71,17 +72,17 @@ impl Move { #[must_use] pub fn is_capture(&self) -> bool { - (self.0 & 0b0100) != 0 + (self.0 & Kind::Capture as u16) != 0 } #[must_use] pub fn is_en_passant(&self) -> bool { - self.flags() == 0b0101 + self.0 == Kind::EnPassantCapture as u16 } #[must_use] pub fn is_promotion(&self) -> bool { - (self.0 & 0b1000) != 0 + (self.0 & Kind::Promotion as u16) != 0 } #[must_use] @@ -113,7 +114,7 @@ impl Move { } impl Move { - fn _transfer_char(self) -> char { + fn transfer_char(self) -> char { if self.is_capture() || self.is_en_passant() { 'x' } else { @@ -122,18 +123,21 @@ impl Move { } } +const KINGSIDE_CASTLE_STR: &str = "0-0"; +const QUEENSIDE_CASTLE_STR: &str = "0-0-0"; + impl fmt::Display for Move { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { if let Some(castle) = self.castle() { return match castle { - Castle::KingSide => write!(f, "0-0"), - Castle::QueenSide => write!(f, "0-0-0"), + Castle::KingSide => write!(f, "{KINGSIDE_CASTLE_STR}"), + Castle::QueenSide => write!(f, "{QUEENSIDE_CASTLE_STR}"), }; } let origin = self.origin_square(); let target = self.target_square(); - let transfer_char = self._transfer_char(); + let transfer_char = self.transfer_char(); write!(f, "{origin}{transfer_char}{target}")?; if let Some(promotion) = self.promotion() {