[moves] Clean up implementation of Move and export Kind enum

This commit is contained in:
Eryn Wells 2025-05-19 14:19:05 -07:00
parent 9010f1e9c2
commit 6816e350eb
4 changed files with 16 additions and 16 deletions

View file

@ -9,3 +9,4 @@ edition = "2021"
chessfriend_bitboard = { path = "../bitboard" }
chessfriend_board = { path = "../board" }
chessfriend_core = { path = "../core" }
thiserror = "2"

View file

@ -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 {

View file

@ -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;

View file

@ -1,5 +1,6 @@
// Eryn Wells <eryn@erynwells.me>
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() {