[moves] Clean up implementation of Move and export Kind enum
This commit is contained in:
parent
9010f1e9c2
commit
6816e350eb
4 changed files with 16 additions and 16 deletions
|
@ -9,3 +9,4 @@ edition = "2021"
|
||||||
chessfriend_bitboard = { path = "../bitboard" }
|
chessfriend_bitboard = { path = "../bitboard" }
|
||||||
chessfriend_board = { path = "../board" }
|
chessfriend_board = { path = "../board" }
|
||||||
chessfriend_core = { path = "../core" }
|
chessfriend_core = { path = "../core" }
|
||||||
|
thiserror = "2"
|
||||||
|
|
|
@ -2,8 +2,9 @@
|
||||||
|
|
||||||
use chessfriend_core::Shape;
|
use chessfriend_core::Shape;
|
||||||
|
|
||||||
|
#[repr(u16)]
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub(crate) enum Kind {
|
pub enum Kind {
|
||||||
Quiet = 0b0000,
|
Quiet = 0b0000,
|
||||||
DoublePush = 0b0001,
|
DoublePush = 0b0001,
|
||||||
KingSideCastle = 0b0010,
|
KingSideCastle = 0b0010,
|
||||||
|
@ -14,12 +15,6 @@ pub(crate) enum Kind {
|
||||||
CapturePromotion = 0b1100,
|
CapturePromotion = 0b1100,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Kind {
|
|
||||||
fn is_reversible(self) -> bool {
|
|
||||||
(self as u16) & 0b1100 == 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[repr(u16)]
|
#[repr(u16)]
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||||
pub enum PromotionShape {
|
pub enum PromotionShape {
|
||||||
|
|
|
@ -7,5 +7,5 @@ mod defs;
|
||||||
mod moves;
|
mod moves;
|
||||||
|
|
||||||
pub use builder::{Builder, Error as BuildMoveError, Result as BuildMoveResult};
|
pub use builder::{Builder, Error as BuildMoveError, Result as BuildMoveResult};
|
||||||
pub use defs::PromotionShape;
|
pub use defs::{Kind, PromotionShape};
|
||||||
pub use moves::Move;
|
pub use moves::Move;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
// Eryn Wells <eryn@erynwells.me>
|
// Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
|
use crate::builder::Builder;
|
||||||
use crate::defs::Kind;
|
use crate::defs::Kind;
|
||||||
use chessfriend_board::castle::Castle;
|
use chessfriend_board::castle::Castle;
|
||||||
use chessfriend_core::{Rank, Shape, Square};
|
use chessfriend_core::{Rank, Shape, Square};
|
||||||
|
@ -57,7 +58,7 @@ impl Move {
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_castle(&self) -> bool {
|
pub fn is_castle(&self) -> bool {
|
||||||
self.castle().is_some()
|
(self.0 & 0b0010) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
@ -71,17 +72,17 @@ impl Move {
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_capture(&self) -> bool {
|
pub fn is_capture(&self) -> bool {
|
||||||
(self.0 & 0b0100) != 0
|
(self.0 & Kind::Capture as u16) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_en_passant(&self) -> bool {
|
pub fn is_en_passant(&self) -> bool {
|
||||||
self.flags() == 0b0101
|
self.0 == Kind::EnPassantCapture as u16
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_promotion(&self) -> bool {
|
pub fn is_promotion(&self) -> bool {
|
||||||
(self.0 & 0b1000) != 0
|
(self.0 & Kind::Promotion as u16) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
@ -113,7 +114,7 @@ impl Move {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Move {
|
impl Move {
|
||||||
fn _transfer_char(self) -> char {
|
fn transfer_char(self) -> char {
|
||||||
if self.is_capture() || self.is_en_passant() {
|
if self.is_capture() || self.is_en_passant() {
|
||||||
'x'
|
'x'
|
||||||
} else {
|
} 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 {
|
impl fmt::Display for Move {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
if let Some(castle) = self.castle() {
|
if let Some(castle) = self.castle() {
|
||||||
return match castle {
|
return match castle {
|
||||||
Castle::KingSide => write!(f, "0-0"),
|
Castle::KingSide => write!(f, "{KINGSIDE_CASTLE_STR}"),
|
||||||
Castle::QueenSide => write!(f, "0-0-0"),
|
Castle::QueenSide => write!(f, "{QUEENSIDE_CASTLE_STR}"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let origin = self.origin_square();
|
let origin = self.origin_square();
|
||||||
let target = self.target_square();
|
let target = self.target_square();
|
||||||
let transfer_char = self._transfer_char();
|
let transfer_char = self.transfer_char();
|
||||||
write!(f, "{origin}{transfer_char}{target}")?;
|
write!(f, "{origin}{transfer_char}{target}")?;
|
||||||
|
|
||||||
if let Some(promotion) = self.promotion() {
|
if let Some(promotion) = self.promotion() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue