From d2cd0f407371c26f8974b1664ff73b9c7be2bfcd Mon Sep 17 00:00:00 2001 From: Clemens Horn Date: Thu, 15 Feb 2024 09:55:27 +0100 Subject: [PATCH 1/6] refactor(board): generic encode trait implementation --- libs/board/src/moves/irreversible/capture/mod.rs | 3 +-- libs/board/src/moves/irreversible/castle/king.rs | 3 +-- libs/board/src/moves/irreversible/castle/queen.rs | 3 +-- libs/board/src/moves/irreversible/pawn/enpassant/mod.rs | 3 +-- libs/board/src/moves/irreversible/pawn/promotion/bishop.rs | 3 +-- .../src/moves/irreversible/pawn/promotion/capture/bishop.rs | 3 +-- .../src/moves/irreversible/pawn/promotion/capture/knight.rs | 3 +-- .../src/moves/irreversible/pawn/promotion/capture/queen.rs | 3 +-- .../src/moves/irreversible/pawn/promotion/capture/rook.rs | 3 +-- libs/board/src/moves/irreversible/pawn/promotion/knight.rs | 3 +-- libs/board/src/moves/irreversible/pawn/promotion/queen.rs | 3 +-- libs/board/src/moves/irreversible/pawn/promotion/rook.rs | 3 +-- libs/board/src/moves/irreversible/pawn/push/mod.rs | 3 +-- libs/board/src/moves/mod.rs | 2 ++ libs/board/src/moves/reversible/quiet.rs | 3 +-- 15 files changed, 16 insertions(+), 28 deletions(-) diff --git a/libs/board/src/moves/irreversible/capture/mod.rs b/libs/board/src/moves/irreversible/capture/mod.rs index 6e1a96b..46779b8 100644 --- a/libs/board/src/moves/irreversible/capture/mod.rs +++ b/libs/board/src/moves/irreversible/capture/mod.rs @@ -1,6 +1,6 @@ use super::IrreversibleMoveExt; use crate::{ - moves::{coordinates::Coordinates, Encode, MoveExt}, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -60,7 +60,6 @@ impl Debug for CaptureMove { impl CaptureMoveExt for CaptureMove {} impl IrreversibleMoveExt for CaptureMove {} -impl Encode for CaptureMove {} #[cfg(test)] mod tests { diff --git a/libs/board/src/moves/irreversible/castle/king.rs b/libs/board/src/moves/irreversible/castle/king.rs index 398d1e7..0d96c2b 100644 --- a/libs/board/src/moves/irreversible/castle/king.rs +++ b/libs/board/src/moves/irreversible/castle/king.rs @@ -1,5 +1,5 @@ use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, Encode, MoveExt}, + moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, Board, }; @@ -56,4 +56,3 @@ impl Debug for KingCastleMove { impl KingCastleMoveExt for KingCastleMove {} impl CastleMoveExt for KingCastleMove {} impl IrreversibleMoveExt for KingCastleMove {} -impl Encode for KingCastleMove {} diff --git a/libs/board/src/moves/irreversible/castle/queen.rs b/libs/board/src/moves/irreversible/castle/queen.rs index e2ee7f4..d84b8c4 100644 --- a/libs/board/src/moves/irreversible/castle/queen.rs +++ b/libs/board/src/moves/irreversible/castle/queen.rs @@ -1,6 +1,6 @@ use super::CastleMoveExt; use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, Encode, MoveExt}, + moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, Board, }; use api::Square; @@ -55,4 +55,3 @@ impl Debug for QueenCastleMove { impl QueenCastleMoveExt for QueenCastleMove {} impl CastleMoveExt for QueenCastleMove {} impl IrreversibleMoveExt for QueenCastleMove {} -impl Encode for QueenCastleMove {} diff --git a/libs/board/src/moves/irreversible/pawn/enpassant/mod.rs b/libs/board/src/moves/irreversible/pawn/enpassant/mod.rs index a52dc87..59b1da3 100644 --- a/libs/board/src/moves/irreversible/pawn/enpassant/mod.rs +++ b/libs/board/src/moves/irreversible/pawn/enpassant/mod.rs @@ -1,6 +1,6 @@ use super::PawnMoveExt; use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, Encode, MoveExt}, + moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, Board, }; use api::Square; @@ -56,7 +56,6 @@ impl Debug for EnPassantMove { } } -impl Encode for EnPassantMove {} impl EnPassantMoveExt for EnPassantMove {} impl PawnMoveExt for EnPassantMove {} impl IrreversibleMoveExt for EnPassantMove {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/bishop.rs b/libs/board/src/moves/irreversible/pawn/promotion/bishop.rs index bb7dc05..b02f9d2 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/bishop.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/bishop.rs @@ -3,7 +3,7 @@ use crate::{ moves::{ coordinates::Coordinates, irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - Encode, MoveExt, + MoveExt, }, Board, }; @@ -56,7 +56,6 @@ impl Debug for BishopPromotionMove { } } -impl Encode for BishopPromotionMove {} impl BishopPromotionMoveExt for BishopPromotionMove {} impl PromotionMoveExt for BishopPromotionMove {} impl PawnMoveExt for BishopPromotionMove {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/bishop.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/bishop.rs index 20d2062..f74461d 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/bishop.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/bishop.rs @@ -6,7 +6,7 @@ use crate::{ pawn::{promotion::PromotionMoveExt, PawnMoveExt}, IrreversibleMoveExt, }, - Encode, MoveExt, + MoveExt, }, Board, }; @@ -59,7 +59,6 @@ impl Debug for BishopPromotionCaptureMove { } } -impl Encode for BishopPromotionCaptureMove {} impl BishopPromotionCaptureMoveExt for BishopPromotionCaptureMove {} impl PromotionCaptureMoveExt for BishopPromotionCaptureMove {} impl PromotionMoveExt for BishopPromotionCaptureMove {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/knight.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/knight.rs index 7aad514..16705e8 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/knight.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/knight.rs @@ -6,7 +6,7 @@ use crate::{ pawn::{promotion::PromotionMoveExt, PawnMoveExt}, IrreversibleMoveExt, }, - Encode, MoveExt, + MoveExt, }, Board, }; @@ -59,7 +59,6 @@ impl Debug for KnightPromotionCaptureMove { } } -impl Encode for KnightPromotionCaptureMove {} impl KnightPromotionCaptureMoveExt for KnightPromotionCaptureMove {} impl PromotionCaptureMoveExt for KnightPromotionCaptureMove {} impl PromotionMoveExt for KnightPromotionCaptureMove {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/queen.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/queen.rs index 509f7f5..9da9f01 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/queen.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/queen.rs @@ -6,7 +6,7 @@ use crate::{ pawn::{promotion::PromotionMoveExt, PawnMoveExt}, IrreversibleMoveExt, }, - Encode, MoveExt, + MoveExt, }, Board, }; @@ -59,7 +59,6 @@ impl Debug for QueenPromotionCaptureMove { } } -impl Encode for QueenPromotionCaptureMove {} impl QueenPromotionCaptureMoveExt for QueenPromotionCaptureMove {} impl PromotionCaptureMoveExt for QueenPromotionCaptureMove {} impl PromotionMoveExt for QueenPromotionCaptureMove {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/rook.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/rook.rs index 71f5f88..4a4a2e8 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/rook.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/rook.rs @@ -6,7 +6,7 @@ use crate::{ pawn::{promotion::PromotionMoveExt, PawnMoveExt}, IrreversibleMoveExt, }, - Encode, MoveExt, + MoveExt, }, Board, }; @@ -59,7 +59,6 @@ impl Debug for RookPromotionCaptureMove { } } -impl Encode for RookPromotionCaptureMove {} impl RookPromotionCaptureMoveExt for RookPromotionCaptureMove {} impl PromotionCaptureMoveExt for RookPromotionCaptureMove {} impl PromotionMoveExt for RookPromotionCaptureMove {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/knight.rs b/libs/board/src/moves/irreversible/pawn/promotion/knight.rs index 21141aa..5e8c487 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/knight.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/knight.rs @@ -3,7 +3,7 @@ use crate::{ moves::{ coordinates::Coordinates, irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - Encode, MoveExt, + MoveExt, }, Board, }; @@ -56,7 +56,6 @@ impl Debug for KnightPromotionMove { } } -impl Encode for KnightPromotionMove {} impl KnightPromotionMoveExt for KnightPromotionMove {} impl PromotionMoveExt for KnightPromotionMove {} impl PawnMoveExt for KnightPromotionMove {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/queen.rs b/libs/board/src/moves/irreversible/pawn/promotion/queen.rs index df9e84b..303d6d8 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/queen.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/queen.rs @@ -3,7 +3,7 @@ use crate::{ moves::{ coordinates::Coordinates, irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - Encode, MoveExt, + MoveExt, }, Board, }; @@ -56,7 +56,6 @@ impl Debug for QueenPromotionMove { } } -impl Encode for QueenPromotionMove {} impl QueenPromotionMoveExt for QueenPromotionMove {} impl PromotionMoveExt for QueenPromotionMove {} impl PawnMoveExt for QueenPromotionMove {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/rook.rs b/libs/board/src/moves/irreversible/pawn/promotion/rook.rs index e043afb..68fd76c 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/rook.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/rook.rs @@ -3,7 +3,7 @@ use crate::{ moves::{ coordinates::Coordinates, irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - Encode, MoveExt, + MoveExt, }, Board, }; @@ -56,7 +56,6 @@ impl Debug for RookPromotionMove { } } -impl Encode for RookPromotionMove {} impl RookPromotionMoveExt for RookPromotionMove {} impl PromotionMoveExt for RookPromotionMove {} impl PawnMoveExt for RookPromotionMove {} diff --git a/libs/board/src/moves/irreversible/pawn/push/mod.rs b/libs/board/src/moves/irreversible/pawn/push/mod.rs index 2987747..0ea38e0 100644 --- a/libs/board/src/moves/irreversible/pawn/push/mod.rs +++ b/libs/board/src/moves/irreversible/pawn/push/mod.rs @@ -1,6 +1,6 @@ use super::PawnMoveExt; use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, Encode, MoveExt}, + moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, Board, }; use api::Square; @@ -52,7 +52,6 @@ impl Debug for DoublePushMove { } } -impl Encode for DoublePushMove {} impl DoublePushMoveExt for DoublePushMove {} impl PawnMoveExt for DoublePushMove {} impl IrreversibleMoveExt for DoublePushMove {} diff --git a/libs/board/src/moves/mod.rs b/libs/board/src/moves/mod.rs index c8e6d75..1d70cd0 100644 --- a/libs/board/src/moves/mod.rs +++ b/libs/board/src/moves/mod.rs @@ -105,6 +105,8 @@ pub trait Encode: MoveExt { } } +impl Encode for T {} + impl MoveExt for Move { fn coordinates(&self) -> Coordinates { match *self { diff --git a/libs/board/src/moves/reversible/quiet.rs b/libs/board/src/moves/reversible/quiet.rs index 9b9a8de..6f331b5 100644 --- a/libs/board/src/moves/reversible/quiet.rs +++ b/libs/board/src/moves/reversible/quiet.rs @@ -1,6 +1,6 @@ use super::ReversibleMoveExt; use crate::{ - moves::{coordinates::Coordinates, Encode, MoveExt}, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -64,7 +64,6 @@ impl Debug for QuietMove { impl QuietMoveExt for QuietMove {} impl ReversibleMoveExt for QuietMove {} -impl Encode for QuietMove {} #[cfg(test)] mod tests { From 30bd39e3f5866d94bf2edf63224fecd04365ebff Mon Sep 17 00:00:00 2001 From: Clemens Horn Date: Thu, 15 Feb 2024 10:18:34 +0100 Subject: [PATCH 2/6] refactor(board): automatic trait implementations for moves --- .../src/moves/irreversible/capture/mod.rs | 1 - .../src/moves/irreversible/castle/king.rs | 3 +-- .../src/moves/irreversible/castle/mod.rs | 2 -- .../src/moves/irreversible/castle/queen.rs | 3 +-- libs/board/src/moves/irreversible/mod.rs | 2 +- .../moves/irreversible/pawn/enpassant/mod.rs | 6 ++--- libs/board/src/moves/irreversible/pawn/mod.rs | 11 +++----- .../irreversible/pawn/promotion/bishop.rs | 11 ++------ .../pawn/promotion/capture/bishop.rs | 19 +++---------- .../pawn/promotion/capture/knight.rs | 19 +++---------- .../pawn/promotion/capture/mod.rs | 11 ++------ .../pawn/promotion/capture/queen.rs | 19 +++---------- .../pawn/promotion/capture/rook.rs | 19 +++---------- .../irreversible/pawn/promotion/knight.rs | 15 +++-------- .../moves/irreversible/pawn/promotion/mod.rs | 6 ++--- .../irreversible/pawn/promotion/queen.rs | 15 +++-------- .../moves/irreversible/pawn/promotion/rook.rs | 15 +++-------- .../src/moves/irreversible/pawn/push/mod.rs | 10 +++---- libs/board/src/moves/mod.rs | 27 +++++++++---------- libs/board/src/moves/reversible/mod.rs | 4 +-- libs/board/src/moves/reversible/quiet.rs | 3 +-- 21 files changed, 63 insertions(+), 158 deletions(-) diff --git a/libs/board/src/moves/irreversible/capture/mod.rs b/libs/board/src/moves/irreversible/capture/mod.rs index 46779b8..9e15644 100644 --- a/libs/board/src/moves/irreversible/capture/mod.rs +++ b/libs/board/src/moves/irreversible/capture/mod.rs @@ -59,7 +59,6 @@ impl Debug for CaptureMove { } impl CaptureMoveExt for CaptureMove {} -impl IrreversibleMoveExt for CaptureMove {} #[cfg(test)] mod tests { diff --git a/libs/board/src/moves/irreversible/castle/king.rs b/libs/board/src/moves/irreversible/castle/king.rs index 0d96c2b..3a714cc 100644 --- a/libs/board/src/moves/irreversible/castle/king.rs +++ b/libs/board/src/moves/irreversible/castle/king.rs @@ -1,5 +1,5 @@ use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, + moves::{coordinates::Coordinates, MoveExt}, Board, }; @@ -55,4 +55,3 @@ impl Debug for KingCastleMove { impl KingCastleMoveExt for KingCastleMove {} impl CastleMoveExt for KingCastleMove {} -impl IrreversibleMoveExt for KingCastleMove {} diff --git a/libs/board/src/moves/irreversible/castle/mod.rs b/libs/board/src/moves/irreversible/castle/mod.rs index 92e6fc9..538467c 100644 --- a/libs/board/src/moves/irreversible/castle/mod.rs +++ b/libs/board/src/moves/irreversible/castle/mod.rs @@ -40,5 +40,3 @@ impl MoveExt for CastleMove { } impl CastleMoveExt for CastleMove {} - -impl IrreversibleMoveExt for CastleMove {} diff --git a/libs/board/src/moves/irreversible/castle/queen.rs b/libs/board/src/moves/irreversible/castle/queen.rs index d84b8c4..0434040 100644 --- a/libs/board/src/moves/irreversible/castle/queen.rs +++ b/libs/board/src/moves/irreversible/castle/queen.rs @@ -1,6 +1,6 @@ use super::CastleMoveExt; use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -54,4 +54,3 @@ impl Debug for QueenCastleMove { impl QueenCastleMoveExt for QueenCastleMove {} impl CastleMoveExt for QueenCastleMove {} -impl IrreversibleMoveExt for QueenCastleMove {} diff --git a/libs/board/src/moves/irreversible/mod.rs b/libs/board/src/moves/irreversible/mod.rs index e5fdc6a..fbc59f1 100644 --- a/libs/board/src/moves/irreversible/mod.rs +++ b/libs/board/src/moves/irreversible/mod.rs @@ -46,4 +46,4 @@ impl MoveExt for IrreversibleMove { } } -impl IrreversibleMoveExt for IrreversibleMove {} +impl IrreversibleMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/enpassant/mod.rs b/libs/board/src/moves/irreversible/pawn/enpassant/mod.rs index 59b1da3..08ba377 100644 --- a/libs/board/src/moves/irreversible/pawn/enpassant/mod.rs +++ b/libs/board/src/moves/irreversible/pawn/enpassant/mod.rs @@ -1,6 +1,6 @@ use super::PawnMoveExt; use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -56,6 +56,4 @@ impl Debug for EnPassantMove { } } -impl EnPassantMoveExt for EnPassantMove {} -impl PawnMoveExt for EnPassantMove {} -impl IrreversibleMoveExt for EnPassantMove {} +impl EnPassantMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/mod.rs b/libs/board/src/moves/irreversible/pawn/mod.rs index 591ebd3..d8cf56d 100644 --- a/libs/board/src/moves/irreversible/pawn/mod.rs +++ b/libs/board/src/moves/irreversible/pawn/mod.rs @@ -2,19 +2,17 @@ pub mod enpassant; pub mod promotion; pub mod push; -use std::error::Error; - +use super::IrreversibleMoveExt; use crate::{ moves::{coordinates::Coordinates, MoveExt}, Board, }; - -use super::IrreversibleMoveExt; use api::Square; use bitboard::BitboardExt; use enpassant::EnPassantMove; use promotion::PromotionMove; use push::DoublePushMove; +use std::error::Error; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum PawnMove { @@ -32,9 +30,6 @@ pub trait PawnMoveExt: IrreversibleMoveExt { } } -impl PawnMoveExt for PawnMove {} -impl IrreversibleMoveExt for PawnMove {} - impl MoveExt for PawnMove { fn coordinates(&self) -> Coordinates { match *self { @@ -52,3 +47,5 @@ impl MoveExt for PawnMove { } } } + +impl PawnMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/bishop.rs b/libs/board/src/moves/irreversible/pawn/promotion/bishop.rs index b02f9d2..d44a8f0 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/bishop.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/bishop.rs @@ -1,10 +1,6 @@ use super::PromotionMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -56,7 +52,4 @@ impl Debug for BishopPromotionMove { } } -impl BishopPromotionMoveExt for BishopPromotionMove {} -impl PromotionMoveExt for BishopPromotionMove {} -impl PawnMoveExt for BishopPromotionMove {} -impl IrreversibleMoveExt for BishopPromotionMove {} +impl BishopPromotionMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/bishop.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/bishop.rs index f74461d..5ec7d53 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/bishop.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/bishop.rs @@ -1,13 +1,6 @@ use super::PromotionCaptureMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{ - pawn::{promotion::PromotionMoveExt, PawnMoveExt}, - IrreversibleMoveExt, - }, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -21,6 +14,8 @@ pub struct BishopPromotionCaptureMove { coordinates: Coordinates, } +pub trait BishopPromotionCaptureMoveExt: PromotionCaptureMoveExt {} + impl BishopPromotionCaptureMove { pub fn new(source: Square, destination: Square) -> Self { Self { @@ -33,8 +28,6 @@ impl BishopPromotionCaptureMove { } } -pub trait BishopPromotionCaptureMoveExt: PromotionCaptureMoveExt {} - impl MoveExt for BishopPromotionCaptureMove { fn coordinates(&self) -> Coordinates { *self.coordinates() @@ -59,8 +52,4 @@ impl Debug for BishopPromotionCaptureMove { } } -impl BishopPromotionCaptureMoveExt for BishopPromotionCaptureMove {} -impl PromotionCaptureMoveExt for BishopPromotionCaptureMove {} -impl PromotionMoveExt for BishopPromotionCaptureMove {} -impl PawnMoveExt for BishopPromotionCaptureMove {} -impl IrreversibleMoveExt for BishopPromotionCaptureMove {} +impl BishopPromotionCaptureMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/knight.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/knight.rs index 16705e8..07387bf 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/knight.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/knight.rs @@ -1,13 +1,6 @@ use super::PromotionCaptureMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{ - pawn::{promotion::PromotionMoveExt, PawnMoveExt}, - IrreversibleMoveExt, - }, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -21,6 +14,8 @@ pub struct KnightPromotionCaptureMove { coordinates: Coordinates, } +pub trait KnightPromotionCaptureMoveExt: PromotionCaptureMoveExt {} + impl KnightPromotionCaptureMove { pub fn new(source: Square, destination: Square) -> Self { Self { @@ -33,8 +28,6 @@ impl KnightPromotionCaptureMove { } } -pub trait KnightPromotionCaptureMoveExt: PromotionCaptureMoveExt {} - impl MoveExt for KnightPromotionCaptureMove { fn coordinates(&self) -> Coordinates { *self.coordinates() @@ -59,8 +52,4 @@ impl Debug for KnightPromotionCaptureMove { } } -impl KnightPromotionCaptureMoveExt for KnightPromotionCaptureMove {} -impl PromotionCaptureMoveExt for KnightPromotionCaptureMove {} -impl PromotionMoveExt for KnightPromotionCaptureMove {} -impl PawnMoveExt for KnightPromotionCaptureMove {} -impl IrreversibleMoveExt for KnightPromotionCaptureMove {} +impl KnightPromotionCaptureMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/mod.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/mod.rs index d8fa60d..8e99c64 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/mod.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/mod.rs @@ -5,11 +5,7 @@ pub mod rook; use super::PromotionMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use bishop::BishopPromotionCaptureMove; @@ -52,7 +48,4 @@ impl MoveExt for PromotionCaptureMove { } } -impl PromotionCaptureMoveExt for PromotionCaptureMove {} -impl PromotionMoveExt for PromotionCaptureMove {} -impl PawnMoveExt for PromotionCaptureMove {} -impl IrreversibleMoveExt for PromotionCaptureMove {} +impl PromotionCaptureMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/queen.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/queen.rs index 9da9f01..a58fb49 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/queen.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/queen.rs @@ -1,13 +1,6 @@ use super::PromotionCaptureMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{ - pawn::{promotion::PromotionMoveExt, PawnMoveExt}, - IrreversibleMoveExt, - }, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -21,6 +14,8 @@ pub struct QueenPromotionCaptureMove { coordinates: Coordinates, } +pub trait QueenPromotionCaptureMoveExt: PromotionCaptureMoveExt {} + impl QueenPromotionCaptureMove { pub fn new(source: Square, destination: Square) -> Self { Self { @@ -33,8 +28,6 @@ impl QueenPromotionCaptureMove { } } -pub trait QueenPromotionCaptureMoveExt: PromotionCaptureMoveExt {} - impl MoveExt for QueenPromotionCaptureMove { fn coordinates(&self) -> Coordinates { *self.coordinates() @@ -59,8 +52,4 @@ impl Debug for QueenPromotionCaptureMove { } } -impl QueenPromotionCaptureMoveExt for QueenPromotionCaptureMove {} -impl PromotionCaptureMoveExt for QueenPromotionCaptureMove {} -impl PromotionMoveExt for QueenPromotionCaptureMove {} -impl PawnMoveExt for QueenPromotionCaptureMove {} -impl IrreversibleMoveExt for QueenPromotionCaptureMove {} +impl QueenPromotionCaptureMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/capture/rook.rs b/libs/board/src/moves/irreversible/pawn/promotion/capture/rook.rs index 4a4a2e8..ba4847b 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/capture/rook.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/capture/rook.rs @@ -1,13 +1,6 @@ use super::PromotionCaptureMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{ - pawn::{promotion::PromotionMoveExt, PawnMoveExt}, - IrreversibleMoveExt, - }, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -21,6 +14,8 @@ pub struct RookPromotionCaptureMove { coordinates: Coordinates, } +pub trait RookPromotionCaptureMoveExt: PromotionCaptureMoveExt {} + impl RookPromotionCaptureMove { pub fn new(source: Square, destination: Square) -> Self { Self { @@ -33,8 +28,6 @@ impl RookPromotionCaptureMove { } } -pub trait RookPromotionCaptureMoveExt: PromotionCaptureMoveExt {} - impl MoveExt for RookPromotionCaptureMove { fn coordinates(&self) -> Coordinates { *self.coordinates() @@ -59,8 +52,4 @@ impl Debug for RookPromotionCaptureMove { } } -impl RookPromotionCaptureMoveExt for RookPromotionCaptureMove {} -impl PromotionCaptureMoveExt for RookPromotionCaptureMove {} -impl PromotionMoveExt for RookPromotionCaptureMove {} -impl PawnMoveExt for RookPromotionCaptureMove {} -impl IrreversibleMoveExt for RookPromotionCaptureMove {} +impl RookPromotionCaptureMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/knight.rs b/libs/board/src/moves/irreversible/pawn/promotion/knight.rs index 5e8c487..d982522 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/knight.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/knight.rs @@ -1,10 +1,6 @@ use super::PromotionMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -18,6 +14,8 @@ pub struct KnightPromotionMove { coordinates: Coordinates, } +pub trait KnightPromotionMoveExt: PromotionMoveExt {} + impl KnightPromotionMove { pub fn new(source: Square, destination: Square) -> Self { Self { @@ -30,8 +28,6 @@ impl KnightPromotionMove { } } -pub trait KnightPromotionMoveExt: PromotionMoveExt {} - impl MoveExt for KnightPromotionMove { fn coordinates(&self) -> Coordinates { *self.coordinates() @@ -56,7 +52,4 @@ impl Debug for KnightPromotionMove { } } -impl KnightPromotionMoveExt for KnightPromotionMove {} -impl PromotionMoveExt for KnightPromotionMove {} -impl PawnMoveExt for KnightPromotionMove {} -impl IrreversibleMoveExt for KnightPromotionMove {} +impl KnightPromotionMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/mod.rs b/libs/board/src/moves/irreversible/pawn/promotion/mod.rs index d4c051a..d78406b 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/mod.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/mod.rs @@ -6,7 +6,7 @@ pub mod rook; use super::PawnMoveExt; use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use bishop::BishopPromotionMove; @@ -53,6 +53,4 @@ impl MoveExt for PromotionMove { } } -impl PromotionMoveExt for PromotionMove {} -impl PawnMoveExt for PromotionMove {} -impl IrreversibleMoveExt for PromotionMove {} +impl PromotionMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/queen.rs b/libs/board/src/moves/irreversible/pawn/promotion/queen.rs index 303d6d8..830a15e 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/queen.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/queen.rs @@ -1,10 +1,6 @@ use super::PromotionMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -18,6 +14,8 @@ pub struct QueenPromotionMove { coordinates: Coordinates, } +pub trait QueenPromotionMoveExt: PromotionMoveExt {} + impl QueenPromotionMove { pub fn new(source: Square, destination: Square) -> Self { Self { @@ -30,8 +28,6 @@ impl QueenPromotionMove { } } -pub trait QueenPromotionMoveExt: PromotionMoveExt {} - impl MoveExt for QueenPromotionMove { fn coordinates(&self) -> Coordinates { *self.coordinates() @@ -56,7 +52,4 @@ impl Debug for QueenPromotionMove { } } -impl QueenPromotionMoveExt for QueenPromotionMove {} -impl PromotionMoveExt for QueenPromotionMove {} -impl PawnMoveExt for QueenPromotionMove {} -impl IrreversibleMoveExt for QueenPromotionMove {} +impl QueenPromotionMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/promotion/rook.rs b/libs/board/src/moves/irreversible/pawn/promotion/rook.rs index 68fd76c..b51b15d 100644 --- a/libs/board/src/moves/irreversible/pawn/promotion/rook.rs +++ b/libs/board/src/moves/irreversible/pawn/promotion/rook.rs @@ -1,10 +1,6 @@ use super::PromotionMoveExt; use crate::{ - moves::{ - coordinates::Coordinates, - irreversible::{pawn::PawnMoveExt, IrreversibleMoveExt}, - MoveExt, - }, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -18,6 +14,8 @@ pub struct RookPromotionMove { coordinates: Coordinates, } +pub trait RookPromotionMoveExt: PromotionMoveExt {} + impl RookPromotionMove { pub fn new(source: Square, destination: Square) -> Self { Self { @@ -30,8 +28,6 @@ impl RookPromotionMove { } } -pub trait RookPromotionMoveExt: PromotionMoveExt {} - impl MoveExt for RookPromotionMove { fn coordinates(&self) -> Coordinates { *self.coordinates() @@ -56,7 +52,4 @@ impl Debug for RookPromotionMove { } } -impl RookPromotionMoveExt for RookPromotionMove {} -impl PromotionMoveExt for RookPromotionMove {} -impl PawnMoveExt for RookPromotionMove {} -impl IrreversibleMoveExt for RookPromotionMove {} +impl RookPromotionMoveExt for T {} diff --git a/libs/board/src/moves/irreversible/pawn/push/mod.rs b/libs/board/src/moves/irreversible/pawn/push/mod.rs index 0ea38e0..2283da1 100644 --- a/libs/board/src/moves/irreversible/pawn/push/mod.rs +++ b/libs/board/src/moves/irreversible/pawn/push/mod.rs @@ -1,6 +1,6 @@ use super::PawnMoveExt; use crate::{ - moves::{coordinates::Coordinates, irreversible::IrreversibleMoveExt, MoveExt}, + moves::{coordinates::Coordinates, MoveExt}, Board, }; use api::Square; @@ -14,6 +14,8 @@ pub struct DoublePushMove { coordinates: Coordinates, } +pub trait DoublePushMoveExt: PawnMoveExt {} + impl DoublePushMove { pub fn new(source: Square, destination: Square) -> Self { Self { @@ -26,8 +28,6 @@ impl DoublePushMove { } } -pub trait DoublePushMoveExt: PawnMoveExt {} - impl MoveExt for DoublePushMove { fn coordinates(&self) -> Coordinates { *self.coordinates() @@ -52,6 +52,4 @@ impl Debug for DoublePushMove { } } -impl DoublePushMoveExt for DoublePushMove {} -impl PawnMoveExt for DoublePushMove {} -impl IrreversibleMoveExt for DoublePushMove {} +impl DoublePushMoveExt for T {} diff --git a/libs/board/src/moves/mod.rs b/libs/board/src/moves/mod.rs index 1d70cd0..489ad6f 100644 --- a/libs/board/src/moves/mod.rs +++ b/libs/board/src/moves/mod.rs @@ -4,8 +4,6 @@ pub mod irreversible; pub mod list; pub mod reversible; -use std::{error::Error, fmt::Display}; - use crate::{ fen::active_color::ActiveColorExt, pieces::{piece::Piece, Pawn, PawnExt}, @@ -16,6 +14,7 @@ use bitboard::{Bitboard, BitboardExt}; use coordinates::Coordinates; use irreversible::IrreversibleMove; use reversible::ReversibleMove; +use std::{error::Error, fmt::Display}; pub const QUIET_MOVE: u16 = 0b0000; pub const DOUBLE_PAWN_PUSH: u16 = 0b0001; @@ -46,16 +45,6 @@ pub enum MoveError { OpponentsPiece, } -impl Display for MoveError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - MoveError::OpponentsPiece => write!(f, "Can not move opponents piece"), - } - } -} - -impl Error for MoveError {} - pub trait MoveExt { fn is_capture(destination: Square, board: Board) -> bool { let pieces: Bitboard = if board.fen().is_white() { @@ -105,8 +94,6 @@ pub trait Encode: MoveExt { } } -impl Encode for T {} - impl MoveExt for Move { fn coordinates(&self) -> Coordinates { match *self { @@ -122,3 +109,15 @@ impl MoveExt for Move { } } } + +impl Display for MoveError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + MoveError::OpponentsPiece => write!(f, "Can not move opponents piece"), + } + } +} + +impl Error for MoveError {} + +impl Encode for T {} diff --git a/libs/board/src/moves/reversible/mod.rs b/libs/board/src/moves/reversible/mod.rs index 2830068..e057a75 100644 --- a/libs/board/src/moves/reversible/mod.rs +++ b/libs/board/src/moves/reversible/mod.rs @@ -19,8 +19,6 @@ pub trait ReversibleMoveExt: MoveExt { } } -impl ReversibleMoveExt for ReversibleMove {} - impl MoveExt for ReversibleMove { fn coordinates(&self) -> Coordinates { match *self { @@ -34,3 +32,5 @@ impl MoveExt for ReversibleMove { } } } + +impl ReversibleMoveExt for T {} diff --git a/libs/board/src/moves/reversible/quiet.rs b/libs/board/src/moves/reversible/quiet.rs index 6f331b5..5aee120 100644 --- a/libs/board/src/moves/reversible/quiet.rs +++ b/libs/board/src/moves/reversible/quiet.rs @@ -62,8 +62,7 @@ impl Debug for QuietMove { } } -impl QuietMoveExt for QuietMove {} -impl ReversibleMoveExt for QuietMove {} +impl QuietMoveExt for T {} #[cfg(test)] mod tests { From 5305f0831341c139071f69481044dcd5d86803dd Mon Sep 17 00:00:00 2001 From: Clemens Horn Date: Thu, 15 Feb 2024 10:39:23 +0100 Subject: [PATCH 3/6] refactor(board): piece symbols --- libs/board/src/pieces/bishop/black.rs | 13 ++--- libs/board/src/pieces/bishop/white.rs | 13 ++--- libs/board/src/pieces/king/black.rs | 13 ++--- libs/board/src/pieces/king/mod.rs | 3 +- libs/board/src/pieces/king/white.rs | 13 ++--- libs/board/src/pieces/knight/black.rs | 13 ++--- libs/board/src/pieces/knight/white.rs | 14 ++---- libs/board/src/pieces/mod.rs | 72 +++++++++++++-------------- libs/board/src/pieces/pawn/black.rs | 13 ++--- libs/board/src/pieces/pawn/white.rs | 13 ++--- libs/board/src/pieces/queen/black.rs | 13 ++--- libs/board/src/pieces/queen/white.rs | 13 ++--- libs/board/src/pieces/rook/black.rs | 14 ++---- libs/board/src/pieces/rook/white.rs | 13 ++--- 14 files changed, 87 insertions(+), 146 deletions(-) diff --git a/libs/board/src/pieces/bishop/black.rs b/libs/board/src/pieces/bishop/black.rs index f33aa12..eadadc5 100644 --- a/libs/board/src/pieces/bishop/black.rs +++ b/libs/board/src/pieces/bishop/black.rs @@ -13,6 +13,9 @@ pub struct BlackBishop { } impl BlackBishop { + pub const SYMBOL: char = 'b'; + pub const UTF_SYMBOL: char = '♝'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl BlackBishop { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'b' - } - - pub const fn utf_symbol() -> char { - '♝' - } } impl From for BlackBishop { @@ -42,7 +37,7 @@ impl From for BlackBishop { impl Display for BlackBishop { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", BlackBishop::symbol()) + write!(f, "{}", BlackBishop::SYMBOL) } } diff --git a/libs/board/src/pieces/bishop/white.rs b/libs/board/src/pieces/bishop/white.rs index 1e0efd2..faf803a 100644 --- a/libs/board/src/pieces/bishop/white.rs +++ b/libs/board/src/pieces/bishop/white.rs @@ -13,6 +13,9 @@ pub struct WhiteBishop { } impl WhiteBishop { + pub const SYMBOL: char = 'B'; + pub const UTF_SYMBOL: char = '♗'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl WhiteBishop { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'B' - } - - pub const fn utf_symbol() -> char { - '♗' - } } impl From for WhiteBishop { @@ -42,7 +37,7 @@ impl From for WhiteBishop { impl Display for WhiteBishop { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", WhiteBishop::symbol()) + write!(f, "{}", WhiteBishop::SYMBOL) } } diff --git a/libs/board/src/pieces/king/black.rs b/libs/board/src/pieces/king/black.rs index 6df4c3c..d46dcbf 100644 --- a/libs/board/src/pieces/king/black.rs +++ b/libs/board/src/pieces/king/black.rs @@ -13,6 +13,9 @@ pub struct BlackKing { } impl BlackKing { + pub const SYMBOL: char = 'k'; + pub const UTF_SYMBOL: char = '♚'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl BlackKing { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'k' - } - - pub const fn utf_symbol() -> char { - '♚' - } } impl From for BlackKing { @@ -42,7 +37,7 @@ impl From for BlackKing { impl Display for BlackKing { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", BlackKing::symbol()) + write!(f, "{}", BlackKing::SYMBOL) } } diff --git a/libs/board/src/pieces/king/mod.rs b/libs/board/src/pieces/king/mod.rs index 1a9f76e..777bc78 100644 --- a/libs/board/src/pieces/king/mod.rs +++ b/libs/board/src/pieces/king/mod.rs @@ -1,13 +1,12 @@ pub mod black; pub mod white; -use std::error::Error; - use super::PieceExt; use crate::{Board, Verify}; use api::Square; use bitboard::Bitboard; use black::BlackKing; +use std::error::Error; use white::WhiteKing; #[derive(Debug)] diff --git a/libs/board/src/pieces/king/white.rs b/libs/board/src/pieces/king/white.rs index 2ef403d..f0ee8fa 100644 --- a/libs/board/src/pieces/king/white.rs +++ b/libs/board/src/pieces/king/white.rs @@ -13,6 +13,9 @@ pub struct WhiteKing { } impl WhiteKing { + pub const SYMBOL: char = 'K'; + pub const UTF_SYMBOL: char = '♔'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl WhiteKing { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'K' - } - - pub const fn utf_symbol() -> char { - '♔' - } } impl From for WhiteKing { @@ -42,7 +37,7 @@ impl From for WhiteKing { impl Display for WhiteKing { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", WhiteKing::symbol()) + write!(f, "{}", WhiteKing::SYMBOL) } } diff --git a/libs/board/src/pieces/knight/black.rs b/libs/board/src/pieces/knight/black.rs index 21596e0..811a766 100644 --- a/libs/board/src/pieces/knight/black.rs +++ b/libs/board/src/pieces/knight/black.rs @@ -13,6 +13,9 @@ pub struct BlackKnight { } impl BlackKnight { + pub const SYMBOL: char = 'n'; + pub const UTF_SYMBOL: char = '♞'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl BlackKnight { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'n' - } - - pub const fn utf_symbol() -> char { - '♞' - } } impl From for BlackKnight { @@ -42,7 +37,7 @@ impl From for BlackKnight { impl Display for BlackKnight { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", BlackKnight::symbol()) + write!(f, "{}", BlackKnight::SYMBOL) } } diff --git a/libs/board/src/pieces/knight/white.rs b/libs/board/src/pieces/knight/white.rs index 7a272df..fc824b9 100644 --- a/libs/board/src/pieces/knight/white.rs +++ b/libs/board/src/pieces/knight/white.rs @@ -13,6 +13,9 @@ pub struct WhiteKnight { } impl WhiteKnight { + pub const SYMBOL: char = 'N'; + pub const UTF_SYMBOL: char = '♘'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl WhiteKnight { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'N' - } - - pub const fn utf_symbol() -> char { - '♘' - } } impl From for WhiteKnight { @@ -39,9 +34,10 @@ impl From for WhiteKnight { Self::new(value) } } + impl Display for WhiteKnight { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", WhiteKnight::symbol()) + write!(f, "{}", WhiteKnight::SYMBOL) } } diff --git a/libs/board/src/pieces/mod.rs b/libs/board/src/pieces/mod.rs index d68ed66..247ee56 100644 --- a/libs/board/src/pieces/mod.rs +++ b/libs/board/src/pieces/mod.rs @@ -25,48 +25,48 @@ pub const NUM_PIECES: usize = NUM_COLORS * NUM_COLOR_PIECES; pub const EMPTY_SYMBOL: char = ' '; pub const PIECE_SYMBOLS: [char; NUM_PIECES] = [ - BlackRook::symbol(), - BlackKnight::symbol(), - BlackBishop::symbol(), - BlackQueen::symbol(), - BlackKing::symbol(), - BlackPawn::symbol(), - WhiteRook::symbol(), - WhiteKnight::symbol(), - WhiteBishop::symbol(), - WhiteQueen::symbol(), - WhiteKing::symbol(), - WhitePawn::symbol(), + BlackRook::SYMBOL, + BlackKnight::SYMBOL, + BlackBishop::SYMBOL, + BlackQueen::SYMBOL, + BlackKing::SYMBOL, + BlackPawn::SYMBOL, + WhiteRook::SYMBOL, + WhiteKnight::SYMBOL, + WhiteBishop::SYMBOL, + WhiteQueen::SYMBOL, + WhiteKing::SYMBOL, + WhitePawn::SYMBOL, ]; pub const UTF_SYMBOLS: [char; NUM_PIECES] = [ - BlackRook::utf_symbol(), - BlackKnight::utf_symbol(), - BlackBishop::utf_symbol(), - BlackQueen::utf_symbol(), - BlackKing::utf_symbol(), - BlackPawn::utf_symbol(), - WhiteRook::utf_symbol(), - WhiteKnight::utf_symbol(), - WhiteBishop::utf_symbol(), - WhiteQueen::utf_symbol(), - WhiteKing::utf_symbol(), - WhitePawn::utf_symbol(), + BlackRook::UTF_SYMBOL, + BlackKnight::UTF_SYMBOL, + BlackBishop::UTF_SYMBOL, + BlackQueen::UTF_SYMBOL, + BlackKing::UTF_SYMBOL, + BlackPawn::UTF_SYMBOL, + WhiteRook::UTF_SYMBOL, + WhiteKnight::UTF_SYMBOL, + WhiteBishop::UTF_SYMBOL, + WhiteQueen::UTF_SYMBOL, + WhiteKing::UTF_SYMBOL, + WhitePawn::UTF_SYMBOL, ]; pub const PIECE_BYTES: [u8; NUM_PIECES] = [ - BlackRook::symbol() as u8, - BlackKnight::symbol() as u8, - BlackBishop::symbol() as u8, - BlackQueen::symbol() as u8, - BlackKing::symbol() as u8, - BlackPawn::symbol() as u8, - WhiteRook::symbol() as u8, - WhiteKnight::symbol() as u8, - WhiteBishop::symbol() as u8, - WhiteQueen::symbol() as u8, - WhiteKing::symbol() as u8, - WhitePawn::symbol() as u8, + BlackRook::SYMBOL as u8, + BlackKnight::SYMBOL as u8, + BlackBishop::SYMBOL as u8, + BlackQueen::SYMBOL as u8, + BlackKing::SYMBOL as u8, + BlackPawn::SYMBOL as u8, + WhiteRook::SYMBOL as u8, + WhiteKnight::SYMBOL as u8, + WhiteBishop::SYMBOL as u8, + WhiteQueen::SYMBOL as u8, + WhiteKing::SYMBOL as u8, + WhitePawn::SYMBOL as u8, ]; pub static PIECE_INDEX_LOOKUP_MAP: LazyLock> = LazyLock::new(|| { diff --git a/libs/board/src/pieces/pawn/black.rs b/libs/board/src/pieces/pawn/black.rs index aacef59..b11506c 100644 --- a/libs/board/src/pieces/pawn/black.rs +++ b/libs/board/src/pieces/pawn/black.rs @@ -16,6 +16,9 @@ pub struct BlackPawn { } impl BlackPawn { + pub const SYMBOL: char = 'p'; + pub const UTF_SYMBOL: char = '♟'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -27,14 +30,6 @@ impl BlackPawn { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'p' - } - - pub const fn utf_symbol() -> char { - '♟' - } } impl From for BlackPawn { @@ -45,7 +40,7 @@ impl From for BlackPawn { impl Display for BlackPawn { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", BlackPawn::symbol()) + write!(f, "{}", BlackPawn::SYMBOL) } } diff --git a/libs/board/src/pieces/pawn/white.rs b/libs/board/src/pieces/pawn/white.rs index 7009b0c..9993c69 100644 --- a/libs/board/src/pieces/pawn/white.rs +++ b/libs/board/src/pieces/pawn/white.rs @@ -16,6 +16,9 @@ pub struct WhitePawn { } impl WhitePawn { + pub const SYMBOL: char = 'P'; + pub const UTF_SYMBOL: char = '♙'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -27,14 +30,6 @@ impl WhitePawn { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'P' - } - - pub const fn utf_symbol() -> char { - '♙' - } } impl From for WhitePawn { @@ -45,7 +40,7 @@ impl From for WhitePawn { impl Display for WhitePawn { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", WhitePawn::symbol()) + write!(f, "{}", WhitePawn::SYMBOL) } } diff --git a/libs/board/src/pieces/queen/black.rs b/libs/board/src/pieces/queen/black.rs index 774a876..d04aa59 100644 --- a/libs/board/src/pieces/queen/black.rs +++ b/libs/board/src/pieces/queen/black.rs @@ -13,6 +13,9 @@ pub struct BlackQueen { } impl BlackQueen { + pub const SYMBOL: char = 'q'; + pub const UTF_SYMBOL: char = '♛'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl BlackQueen { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'q' - } - - pub const fn utf_symbol() -> char { - '♛' - } } impl From for BlackQueen { @@ -42,7 +37,7 @@ impl From for BlackQueen { impl Display for BlackQueen { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", BlackQueen::symbol()) + write!(f, "{}", BlackQueen::SYMBOL) } } diff --git a/libs/board/src/pieces/queen/white.rs b/libs/board/src/pieces/queen/white.rs index 9a7046e..87da0dd 100644 --- a/libs/board/src/pieces/queen/white.rs +++ b/libs/board/src/pieces/queen/white.rs @@ -13,6 +13,9 @@ pub struct WhiteQueen { } impl WhiteQueen { + pub const SYMBOL: char = 'Q'; + pub const UTF_SYMBOL: char = '♕'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl WhiteQueen { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'Q' - } - - pub const fn utf_symbol() -> char { - '♕' - } } impl From for WhiteQueen { @@ -42,7 +37,7 @@ impl From for WhiteQueen { impl Display for WhiteQueen { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", WhiteQueen::symbol()) + write!(f, "{}", WhiteQueen::SYMBOL) } } diff --git a/libs/board/src/pieces/rook/black.rs b/libs/board/src/pieces/rook/black.rs index f20de74..01358ba 100644 --- a/libs/board/src/pieces/rook/black.rs +++ b/libs/board/src/pieces/rook/black.rs @@ -13,6 +13,9 @@ pub struct BlackRook { } impl BlackRook { + pub const SYMBOL: char = 'r'; + pub const UTF_SYMBOL: char = '♜'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl BlackRook { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'r' - } - - pub const fn utf_symbol() -> char { - '♜' - } } impl From for BlackRook { @@ -39,9 +34,10 @@ impl From for BlackRook { Self::new(value) } } + impl Display for BlackRook { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", BlackRook::symbol()) + write!(f, "{}", BlackRook::SYMBOL) } } diff --git a/libs/board/src/pieces/rook/white.rs b/libs/board/src/pieces/rook/white.rs index 3c18181..90009ce 100644 --- a/libs/board/src/pieces/rook/white.rs +++ b/libs/board/src/pieces/rook/white.rs @@ -13,6 +13,9 @@ pub struct WhiteRook { } impl WhiteRook { + pub const SYMBOL: char = 'R'; + pub const UTF_SYMBOL: char = '♖'; + pub fn new(bitboard: Bitboard) -> Self { Self { bitboard } } @@ -24,14 +27,6 @@ impl WhiteRook { pub fn bitboard_mut(&mut self) -> &mut Bitboard { &mut self.bitboard } - - pub const fn symbol() -> char { - 'R' - } - - pub const fn utf_symbol() -> char { - '♖' - } } impl From for WhiteRook { @@ -42,7 +37,7 @@ impl From for WhiteRook { impl Display for WhiteRook { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", WhiteRook::symbol()) + write!(f, "{}", WhiteRook::SYMBOL) } } From 72b2db48692121fe8d4c972e2c49fb025d60e87e Mon Sep 17 00:00:00 2001 From: Clemens Horn Date: Thu, 15 Feb 2024 11:16:09 +0100 Subject: [PATCH 4/6] refactor(board): fen constants associated with impl --- libs/api/src/color.rs | 23 ++++++++++++ libs/api/src/lib.rs | 2 + libs/board/src/fen/active_color.rs | 27 +++++++++++--- libs/board/src/fen/castling.rs | 54 ++++++++++++++------------- libs/board/src/fen/enpassant.rs | 22 ++++++----- libs/board/src/fen/full_move_clock.rs | 10 ++--- libs/board/src/fen/half_move_clock.rs | 20 +++++----- libs/board/src/fen/mod.rs | 6 +-- libs/board/src/fen/placements.rs | 2 +- libs/board/src/pieces/mod.rs | 13 ++++++- 10 files changed, 115 insertions(+), 64 deletions(-) create mode 100644 libs/api/src/color.rs diff --git a/libs/api/src/color.rs b/libs/api/src/color.rs new file mode 100644 index 0000000..0a06358 --- /dev/null +++ b/libs/api/src/color.rs @@ -0,0 +1,23 @@ +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum Color { + White = 0, + Black = 1, +} + +impl Color { + /// Returns `true` if the color is [`White`]. + /// + /// [`White`]: Color::White + #[must_use] + pub fn is_white(&self) -> bool { + matches!(self, Self::White) + } + + /// Returns `true` if the color is [`Black`]. + /// + /// [`Black`]: Color::Black + #[must_use] + pub fn is_black(&self) -> bool { + matches!(self, Self::Black) + } +} diff --git a/libs/api/src/lib.rs b/libs/api/src/lib.rs index dc44f2b..df34f0d 100644 --- a/libs/api/src/lib.rs +++ b/libs/api/src/lib.rs @@ -1,4 +1,5 @@ #![feature(variant_count)] +mod color; mod controller; mod fen; mod game; @@ -8,6 +9,7 @@ mod square; mod state; mod view; +pub use color::Color; pub use controller::Controller; pub use fen::ForsythEdwardsNotationExt; pub use game::GameExt; diff --git a/libs/board/src/fen/active_color.rs b/libs/board/src/fen/active_color.rs index 0ccc6ee..12fc97d 100644 --- a/libs/board/src/fen/active_color.rs +++ b/libs/board/src/fen/active_color.rs @@ -1,8 +1,6 @@ +use api::Color; use std::fmt::{Debug, Display}; -pub const NUM_COLORS: usize = 2; -pub const COLORS: [u8; NUM_COLORS] = [b'w', b'b']; - #[derive(PartialEq, Eq, Clone, Copy)] pub struct ActiveColor { color: u8, @@ -14,6 +12,9 @@ pub enum ActiveColorError { } impl ActiveColor { + pub const COLORS: [u8; Self::NUM_COLORS] = [b'w', b'b']; + pub const NUM_COLORS: usize = 2; + pub fn new() -> Self { Self::default() } @@ -25,7 +26,7 @@ impl ActiveColor { impl Default for ActiveColor { fn default() -> Self { - Self { color: COLORS[0] } + Self { color: Self::COLORS[0] } } } @@ -58,11 +59,15 @@ pub trait ActiveColorExt { impl ActiveColorExt for ActiveColor { fn switch(&mut self) { - self.color = if self.color == COLORS[0] { COLORS[1] } else { COLORS[0] } + self.color = if self.color == Self::COLORS[0] { + Self::COLORS[1] + } else { + Self::COLORS[0] + } } fn is_white(&self) -> bool { - self.color == COLORS[0] + self.color == Self::COLORS[0] } } @@ -78,6 +83,16 @@ impl Debug for ActiveColor { } } +impl From for ActiveColor { + fn from(value: Color) -> Self { + if value.is_white() { + Self { color: b'w' } + } else { + Self { color: b'b' } + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/libs/board/src/fen/castling.rs b/libs/board/src/fen/castling.rs index 2bf50e6..57cb324 100644 --- a/libs/board/src/fen/castling.rs +++ b/libs/board/src/fen/castling.rs @@ -1,10 +1,5 @@ use std::fmt::{Debug, Display}; -const WHITE_KING_CASTLE: u8 = 0b1000; -const WHITE_QUEEN_CASTLE: u8 = 0b0100; -const BLACK_KING_CASTLE: u8 = 0b0010; -const BLACK_QUEEN_CASTLE: u8 = 0b0001; - #[derive(PartialEq, Eq, Clone, Copy)] pub struct Castling { rights: u8, @@ -17,11 +12,16 @@ pub enum CastlingError { impl Default for Castling { fn default() -> Self { - Self::new(WHITE_KING_CASTLE | WHITE_QUEEN_CASTLE | BLACK_KING_CASTLE | BLACK_QUEEN_CASTLE) + Self::new(Self::WHITE_KING_CASTLE | Self::WHITE_QUEEN_CASTLE | Self::BLACK_KING_CASTLE | Self::BLACK_QUEEN_CASTLE) } } impl Castling { + pub const BLACK_KING_CASTLE: u8 = 0b0010; + pub const BLACK_QUEEN_CASTLE: u8 = 0b0001; + pub const WHITE_KING_CASTLE: u8 = 0b1000; + pub const WHITE_QUEEN_CASTLE: u8 = 0b0100; + pub fn new(rights: u8) -> Self { Self { rights } } @@ -31,19 +31,19 @@ impl Castling { } pub fn has_white_king_castle(&self) -> bool { - self.rights & WHITE_KING_CASTLE != 0 + self.rights & Self::WHITE_KING_CASTLE != 0 } pub fn has_white_queen_castle(&self) -> bool { - self.rights & WHITE_QUEEN_CASTLE != 0 + self.rights & Self::WHITE_QUEEN_CASTLE != 0 } pub fn has_black_king_castle(&self) -> bool { - self.rights & BLACK_KING_CASTLE != 0 + self.rights & Self::BLACK_KING_CASTLE != 0 } pub fn has_black_queen_castle(&self) -> bool { - self.rights & BLACK_QUEEN_CASTLE != 0 + self.rights & Self::BLACK_QUEEN_CASTLE != 0 } pub fn has_no_castle_rights(&self) -> bool { @@ -61,10 +61,10 @@ impl TryFrom<&str> for Castling { } for char in value.chars() { match char { - 'K' => rights |= WHITE_KING_CASTLE, - 'Q' => rights |= WHITE_QUEEN_CASTLE, - 'k' => rights |= BLACK_KING_CASTLE, - 'q' => rights |= BLACK_QUEEN_CASTLE, + 'K' => rights |= Self::WHITE_KING_CASTLE, + 'Q' => rights |= Self::WHITE_QUEEN_CASTLE, + 'k' => rights |= Self::BLACK_KING_CASTLE, + 'q' => rights |= Self::BLACK_QUEEN_CASTLE, '-' => return Ok(Castling::default()), _ => return Err(Self::Error::Invalid), } @@ -117,13 +117,13 @@ mod tests { let castling = Castling::default(); assert_eq!( castling.rights(), - WHITE_KING_CASTLE | WHITE_QUEEN_CASTLE | BLACK_KING_CASTLE | BLACK_QUEEN_CASTLE + Castling::WHITE_KING_CASTLE | Castling::WHITE_QUEEN_CASTLE | Castling::BLACK_KING_CASTLE | Castling::BLACK_QUEEN_CASTLE ); } #[test] fn test_custom_castling_rights() { - let custom_rights = WHITE_KING_CASTLE | BLACK_QUEEN_CASTLE; + let custom_rights = Castling::WHITE_KING_CASTLE | Castling::BLACK_QUEEN_CASTLE; let castling = Castling::new(custom_rights); assert_eq!(castling.rights(), custom_rights); } @@ -132,7 +132,8 @@ mod tests { fn test_has_white_king_castle() { let castling = Castling::default(); assert!(castling.has_white_king_castle()); - let castling_without_white_king = Castling::new(WHITE_QUEEN_CASTLE | BLACK_KING_CASTLE | BLACK_QUEEN_CASTLE); + let castling_without_white_king = + Castling::new(Castling::WHITE_QUEEN_CASTLE | Castling::BLACK_KING_CASTLE | Castling::BLACK_QUEEN_CASTLE); assert!(!castling_without_white_king.has_white_king_castle()); } @@ -140,7 +141,8 @@ mod tests { fn test_has_white_queen_castle() { let castling = Castling::default(); assert!(castling.has_white_queen_castle()); - let castling_without_white_queen = Castling::new(WHITE_KING_CASTLE | BLACK_KING_CASTLE | BLACK_QUEEN_CASTLE); + let castling_without_white_queen = + Castling::new(Castling::WHITE_KING_CASTLE | Castling::BLACK_KING_CASTLE | Castling::BLACK_QUEEN_CASTLE); assert!(!castling_without_white_queen.has_white_queen_castle()); } @@ -148,7 +150,8 @@ mod tests { fn test_has_black_king_castle() { let castling = Castling::default(); assert!(castling.has_black_king_castle()); - let castling_without_black_king = Castling::new(WHITE_KING_CASTLE | WHITE_QUEEN_CASTLE | BLACK_QUEEN_CASTLE); + let castling_without_black_king = + Castling::new(Castling::WHITE_KING_CASTLE | Castling::WHITE_QUEEN_CASTLE | Castling::BLACK_QUEEN_CASTLE); assert!(!castling_without_black_king.has_black_king_castle()); } @@ -156,13 +159,14 @@ mod tests { fn test_has_black_queen_castle() { let castling = Castling::default(); assert!(castling.has_black_queen_castle()); - let castling_without_black_queen = Castling::new(WHITE_KING_CASTLE | WHITE_QUEEN_CASTLE | BLACK_KING_CASTLE); + let castling_without_black_queen = + Castling::new(Castling::WHITE_KING_CASTLE | Castling::WHITE_QUEEN_CASTLE | Castling::BLACK_KING_CASTLE); assert!(!castling_without_black_queen.has_black_queen_castle()); } #[test] fn test_display_with_castling_rights() { - let castling = Castling::new(WHITE_KING_CASTLE | BLACK_QUEEN_CASTLE); + let castling = Castling::new(Castling::WHITE_KING_CASTLE | Castling::BLACK_QUEEN_CASTLE); assert_eq!(format!("{castling}"), "Kq"); } @@ -174,13 +178,13 @@ mod tests { #[test] fn test_individual_castling_rights() { - let castling = Castling::new(BLACK_KING_CASTLE); + let castling = Castling::new(Castling::BLACK_KING_CASTLE); assert_eq!(format!("{castling}"), "k"); } #[test] fn test_combination_of_castling_rights() { - let castling = Castling::new(WHITE_KING_CASTLE | BLACK_KING_CASTLE | BLACK_QUEEN_CASTLE); + let castling = Castling::new(Castling::WHITE_KING_CASTLE | Castling::BLACK_KING_CASTLE | Castling::BLACK_QUEEN_CASTLE); assert_eq!(format!("{castling}"), "Kkq"); } @@ -202,7 +206,7 @@ mod tests { assert_eq!( result, Ok(Castling::new( - WHITE_KING_CASTLE | WHITE_QUEEN_CASTLE | BLACK_KING_CASTLE | BLACK_QUEEN_CASTLE + Castling::WHITE_KING_CASTLE | Castling::WHITE_QUEEN_CASTLE | Castling::BLACK_KING_CASTLE | Castling::BLACK_QUEEN_CASTLE )) ); } @@ -225,7 +229,7 @@ mod tests { assert_eq!( result, Ok(Castling::new( - WHITE_KING_CASTLE | WHITE_QUEEN_CASTLE | BLACK_KING_CASTLE | BLACK_QUEEN_CASTLE + Castling::WHITE_KING_CASTLE | Castling::WHITE_QUEEN_CASTLE | Castling::BLACK_KING_CASTLE | Castling::BLACK_QUEEN_CASTLE )) ); } diff --git a/libs/board/src/fen/enpassant.rs b/libs/board/src/fen/enpassant.rs index ffbeb30..ddf056a 100644 --- a/libs/board/src/fen/enpassant.rs +++ b/libs/board/src/fen/enpassant.rs @@ -1,9 +1,7 @@ +use api::{Square, Square::*}; +use bitboard::{Bitboard, BitboardExt}; use std::fmt::{Debug, Display}; -use api::Square; -use api::Square::{A3, A6, H3, H6}; -use bitboard::Bitboard; - #[derive(PartialEq, Eq, Clone, Copy, Default)] pub struct EnPassant { square: Option, @@ -14,6 +12,11 @@ pub enum EnPassantError { Invalid, } +pub trait EnPassantExt { + fn set_square(&mut self, square: Option); + fn unset(&mut self); +} + impl EnPassant { pub fn new(square: Option) -> Self { if Self::is_valid_square(square) { @@ -36,7 +39,11 @@ impl EnPassant { } pub fn mask(&self) -> Bitboard { - Bitboard::default() + if let Some(square) = self.square() { + Bitboard::get_single_bit(square.into()) + } else { + Bitboard::default() + } } } @@ -62,11 +69,6 @@ impl TryFrom for EnPassant { } } -pub(crate) trait EnPassantExt { - fn set_square(&mut self, square: Option); - fn unset(&mut self); -} - impl EnPassantExt for EnPassant { fn set_square(&mut self, square: Option) { if Self::is_valid_square(square) { diff --git a/libs/board/src/fen/full_move_clock.rs b/libs/board/src/fen/full_move_clock.rs index 1bcf21d..bbf81ad 100644 --- a/libs/board/src/fen/full_move_clock.rs +++ b/libs/board/src/fen/full_move_clock.rs @@ -1,7 +1,5 @@ use std::fmt::{Debug, Display}; -pub const MIN_FULL_MOVE_CLOCK: u16 = 1; - #[derive(PartialEq, Eq, Clone, Copy)] pub struct FullMoveClock { clock: u16, @@ -14,6 +12,8 @@ pub enum FullMoveClockError { } impl FullMoveClock { + pub const MIN: u16 = 1; + pub fn new(clock: u16) -> Self { Self { clock } } @@ -25,9 +25,7 @@ impl FullMoveClock { impl Default for FullMoveClock { fn default() -> Self { - Self { - clock: MIN_FULL_MOVE_CLOCK, - } + Self { clock: Self::MIN } } } @@ -36,7 +34,7 @@ impl TryFrom<&str> for FullMoveClock { fn try_from(value: &str) -> Result { if let Ok(parsed_value) = value.parse::() { - if parsed_value < MIN_FULL_MOVE_CLOCK { + if parsed_value < Self::MIN { Err(Self::Error::TooLow) } else { Ok(Self::new(parsed_value)) diff --git a/libs/board/src/fen/half_move_clock.rs b/libs/board/src/fen/half_move_clock.rs index c95cb80..c0c6357 100644 --- a/libs/board/src/fen/half_move_clock.rs +++ b/libs/board/src/fen/half_move_clock.rs @@ -1,8 +1,5 @@ use std::fmt::{Debug, Display}; -pub const MAX_HALF_MOVE_CLOCK: u8 = 150; -pub const MIN_HALF_MOVE_CLOCK: u8 = 0; - #[derive(PartialEq, Eq, Clone, Copy)] pub struct HalfMoveClock { clock: u8, @@ -15,6 +12,9 @@ pub enum HalfMoveClockError { } impl HalfMoveClock { + pub const MAX: u8 = 150; + pub const MIN: u8 = 0; + pub fn new(clock: u8) -> Self { Self { clock } } @@ -26,9 +26,7 @@ impl HalfMoveClock { impl Default for HalfMoveClock { fn default() -> Self { - Self { - clock: MIN_HALF_MOVE_CLOCK, - } + Self { clock: Self::MIN } } } @@ -37,7 +35,7 @@ impl TryFrom<&str> for HalfMoveClock { fn try_from(value: &str) -> Result { if let Ok(parsed_value) = value.parse::() { - if parsed_value > MAX_HALF_MOVE_CLOCK { + if parsed_value > Self::MAX { Err(Self::Error::TooHigh) } else { Ok(Self::new(parsed_value)) @@ -63,13 +61,13 @@ pub(crate) trait HalfMoveClockExt { impl HalfMoveClockExt for HalfMoveClock { fn increment(&mut self) { - if self.clock < MAX_HALF_MOVE_CLOCK { + if self.clock < Self::MAX { self.clock += 1; } } fn reset(&mut self) { - self.clock = MIN_HALF_MOVE_CLOCK; + self.clock = Self::MIN; } } @@ -135,9 +133,9 @@ mod tests { #[test] fn test_increment_at_max() { - let mut clock = HalfMoveClock::new(MAX_HALF_MOVE_CLOCK); + let mut clock = HalfMoveClock::new(HalfMoveClock::MAX); clock.increment(); - assert_eq!(clock.clock(), MAX_HALF_MOVE_CLOCK); + assert_eq!(clock.clock(), HalfMoveClock::MAX); } #[test] diff --git a/libs/board/src/fen/mod.rs b/libs/board/src/fen/mod.rs index 807dfde..eed99a2 100644 --- a/libs/board/src/fen/mod.rs +++ b/libs/board/src/fen/mod.rs @@ -13,8 +13,6 @@ use std::{ fmt::{Debug, Display}, }; -pub const FEN_PARTS: usize = 6; - #[derive(Default, Clone, Copy, PartialEq, Eq)] pub struct ForsythEdwardsNotation { placements: Placements, @@ -26,6 +24,8 @@ pub struct ForsythEdwardsNotation { } impl ForsythEdwardsNotation { + pub const PARTS: usize = 6; + pub fn new( placements: Placements, active_color: ActiveColor, @@ -165,7 +165,7 @@ impl TryFrom<&str> for ForsythEdwardsNotation { fn try_from(value: &str) -> Result { let parts: Vec<&str> = value.split(' ').collect(); - if parts.len() != FEN_PARTS { + if parts.len() != Self::PARTS { return Err(Self::Error::Invalid); } let placements = Placements::try_from(parts[0])?; diff --git a/libs/board/src/fen/placements.rs b/libs/board/src/fen/placements.rs index e7623be..3487c30 100644 --- a/libs/board/src/fen/placements.rs +++ b/libs/board/src/fen/placements.rs @@ -127,7 +127,7 @@ impl TryFrom for Placements { impl Display for Placements { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { for (rank, pieces) in self.position.iter().enumerate() { - let mut empty_files = 0; + let mut empty_files: u8 = 0; for &piece in pieces { if piece == 0 { empty_files += 1; diff --git a/libs/board/src/pieces/mod.rs b/libs/board/src/pieces/mod.rs index 247ee56..16f5e6a 100644 --- a/libs/board/src/pieces/mod.rs +++ b/libs/board/src/pieces/mod.rs @@ -9,6 +9,7 @@ mod rook; mod white_pieces; use self::{piece::*, white_pieces::WhitePieces}; +use api::Color; pub use bishop::{black::*, white::*, Bishop}; pub use bitboard::{Bitboard, BitboardExt}; use black_pieces::BlackPieces; @@ -88,6 +89,14 @@ pub struct Pieces { pub trait PiecesExt {} impl Pieces { + pub fn get_pieces(&self, color: Color) -> Bitboard { + if color.is_white() { + self.white_pieces().into() + } else { + self.black_pieces().into() + } + } + pub fn white_pieces(&self) -> WhitePieces { self.white_pieces } @@ -239,8 +248,8 @@ impl From<[[u8; 8]; 8]> for Pieces { } impl From for Bitboard { - fn from(val: Pieces) -> Self { - Bitboard::merge_many(vec![val.white_pieces().into(), val.black_pieces().into()]) + fn from(value: Pieces) -> Self { + Bitboard::merge_many(vec![value.white_pieces().into(), value.black_pieces().into()]) } } From 01a09f9205fc0e244488543c6876c12ed34f0349 Mon Sep 17 00:00:00 2001 From: Clemens Horn Date: Fri, 16 Feb 2024 09:00:35 +0100 Subject: [PATCH 5/6] refactor(board): pieces macro, remove is_illegal_move trait method --- libs/board/src/lib.rs | 1 + libs/board/src/moves/encoded_move.rs | 59 ++++++--------------- libs/board/src/pieces/bishop/black.rs | 4 -- libs/board/src/pieces/bishop/mod.rs | 4 -- libs/board/src/pieces/bishop/white.rs | 4 -- libs/board/src/pieces/black_pieces.rs | 75 --------------------------- libs/board/src/pieces/king/black.rs | 4 -- libs/board/src/pieces/king/mod.rs | 4 -- libs/board/src/pieces/king/white.rs | 4 -- libs/board/src/pieces/knight/black.rs | 4 -- libs/board/src/pieces/knight/mod.rs | 4 -- libs/board/src/pieces/knight/white.rs | 4 -- libs/board/src/pieces/mod.rs | 73 ++++++++++++++++++++------ libs/board/src/pieces/pawn/black.rs | 4 -- libs/board/src/pieces/pawn/mod.rs | 14 +---- libs/board/src/pieces/pawn/white.rs | 4 -- libs/board/src/pieces/piece.rs | 12 ----- libs/board/src/pieces/queen/black.rs | 4 -- libs/board/src/pieces/queen/mod.rs | 4 -- libs/board/src/pieces/queen/white.rs | 4 -- libs/board/src/pieces/rook/black.rs | 4 -- libs/board/src/pieces/rook/mod.rs | 4 -- libs/board/src/pieces/rook/white.rs | 4 -- libs/board/src/pieces/white_pieces.rs | 75 --------------------------- 24 files changed, 75 insertions(+), 302 deletions(-) delete mode 100644 libs/board/src/pieces/black_pieces.rs delete mode 100644 libs/board/src/pieces/white_pieces.rs diff --git a/libs/board/src/lib.rs b/libs/board/src/lib.rs index d5b2f18..32d0d93 100644 --- a/libs/board/src/lib.rs +++ b/libs/board/src/lib.rs @@ -1,5 +1,6 @@ #![feature(variant_count)] #![feature(lazy_cell)] +#![feature(concat_idents)] pub mod fen; pub mod moves; pub mod pieces; diff --git a/libs/board/src/moves/encoded_move.rs b/libs/board/src/moves/encoded_move.rs index 3353ab2..60f900e 100644 --- a/libs/board/src/moves/encoded_move.rs +++ b/libs/board/src/moves/encoded_move.rs @@ -31,7 +31,6 @@ const KIND_MASK: u16 = 0b0000000000001111; pub struct EncodedMove { data: u16, } - pub trait EncodedMoveExt { fn source(&self) -> Square; fn destination(&self) -> Square; @@ -64,119 +63,91 @@ impl EncodedMove { impl Default for EncodedMove { fn default() -> Self { - Self { data: u16::MIN } + Self::new(u16::MIN) } } impl From for EncodedMove { fn from(value: QuietMove) -> Self { - Self { - data: value.encode(QUIET_MOVE), - } + Self::new(value.encode(QUIET_MOVE)) } } impl From for EncodedMove { fn from(value: CaptureMove) -> Self { - Self { - data: value.encode(CAPTURE), - } + Self::new(value.encode(CAPTURE)) } } impl From for EncodedMove { fn from(value: DoublePushMove) -> Self { - Self { - data: value.encode(DOUBLE_PAWN_PUSH), - } + Self::new(value.encode(DOUBLE_PAWN_PUSH)) } } impl From for EncodedMove { fn from(value: KingCastleMove) -> Self { - Self { - data: value.encode(KING_CASTLE), - } + Self::new(value.encode(KING_CASTLE)) } } impl From for EncodedMove { fn from(value: QueenCastleMove) -> Self { - Self { - data: value.encode(QUEEN_CASTLE), - } + Self::new(value.encode(QUEEN_CASTLE)) } } impl From for EncodedMove { fn from(value: EnPassantMove) -> Self { - Self { - data: value.encode(ENPASSANT), - } + Self::new(value.encode(ENPASSANT)) } } impl From for EncodedMove { fn from(value: KnightPromotionMove) -> Self { - Self { - data: value.encode(KNIGHT_PROMOTION), - } + Self::new(value.encode(KNIGHT_PROMOTION)) } } impl From for EncodedMove { fn from(value: BishopPromotionMove) -> Self { - Self { - data: value.encode(BISHOP_PROMOTION), - } + Self::new(value.encode(BISHOP_PROMOTION)) } } impl From for EncodedMove { fn from(value: RookPromotionMove) -> Self { - Self { - data: value.encode(ROOK_PROMOTION), - } + Self::new(value.encode(ROOK_PROMOTION)) } } impl From for EncodedMove { fn from(value: QueenPromotionMove) -> Self { - Self { - data: value.encode(QUEEN_PROMOTION), - } + Self::new(value.encode(QUEEN_PROMOTION)) } } impl From for EncodedMove { fn from(value: KnightPromotionCaptureMove) -> Self { - Self { - data: value.encode(KNIGHT_PROMOTION_CAPTURE), - } + Self::new(value.encode(KNIGHT_PROMOTION_CAPTURE)) } } impl From for EncodedMove { fn from(value: BishopPromotionCaptureMove) -> Self { - Self { - data: value.encode(BISHOP_PROMOTION_CAPTURE), - } + Self::new(value.encode(BISHOP_PROMOTION_CAPTURE)) } } impl From for EncodedMove { fn from(value: RookPromotionCaptureMove) -> Self { - Self { - data: value.encode(ROOK_PROMOTION_CAPTURE), - } + Self::new(value.encode(ROOK_PROMOTION_CAPTURE)) } } impl From for EncodedMove { fn from(value: QueenPromotionCaptureMove) -> Self { - Self { - data: value.encode(QUEEN_PROMOTION_CAPTURE), - } + Self::new(value.encode(QUEEN_PROMOTION_CAPTURE)) } } diff --git a/libs/board/src/pieces/bishop/black.rs b/libs/board/src/pieces/bishop/black.rs index eadadc5..765ead5 100644 --- a/libs/board/src/pieces/bishop/black.rs +++ b/libs/board/src/pieces/bishop/black.rs @@ -48,10 +48,6 @@ impl Debug for BlackBishop { } impl PieceExt for BlackBishop { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - Bishop::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Bishop::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/bishop/mod.rs b/libs/board/src/pieces/bishop/mod.rs index d32c70f..d932376 100644 --- a/libs/board/src/pieces/bishop/mod.rs +++ b/libs/board/src/pieces/bishop/mod.rs @@ -31,10 +31,6 @@ impl From for Bishop { } impl PieceExt for Bishop { - fn is_illegal_move(&self, _source: Square, _destination: Square, _board: Board) -> bool { - todo!() - } - fn get_attacks(&self, _piece: Bitboard, _board: Board) -> bitboard::Bitboard { todo!() } diff --git a/libs/board/src/pieces/bishop/white.rs b/libs/board/src/pieces/bishop/white.rs index faf803a..05752dc 100644 --- a/libs/board/src/pieces/bishop/white.rs +++ b/libs/board/src/pieces/bishop/white.rs @@ -48,10 +48,6 @@ impl Debug for WhiteBishop { } impl PieceExt for WhiteBishop { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - Bishop::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Bishop::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/black_pieces.rs b/libs/board/src/pieces/black_pieces.rs deleted file mode 100644 index e6b53ea..0000000 --- a/libs/board/src/pieces/black_pieces.rs +++ /dev/null @@ -1,75 +0,0 @@ -use super::{BlackBishop, BlackKing, BlackKnight, BlackPawn, BlackQueen, BlackRook}; -use bitboard::{Bitboard, BitboardExt}; - -#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] -pub struct BlackPieces { - rook: BlackRook, - knight: BlackKnight, - bishop: BlackBishop, - queen: BlackQueen, - king: BlackKing, - pawn: BlackPawn, -} - -impl BlackPieces { - pub fn rook(&self) -> BlackRook { - self.rook - } - - pub fn rook_mut(&mut self) -> &mut BlackRook { - &mut self.rook - } - - pub fn knight(&self) -> BlackKnight { - self.knight - } - - pub fn knight_mut(&mut self) -> &mut BlackKnight { - &mut self.knight - } - - pub fn bishop(&self) -> BlackBishop { - self.bishop - } - - pub fn bishop_mut(&mut self) -> &mut BlackBishop { - &mut self.bishop - } - - pub fn queen(&self) -> BlackQueen { - self.queen - } - - pub fn queen_mut(&mut self) -> &mut BlackQueen { - &mut self.queen - } - - pub fn king(&self) -> BlackKing { - self.king - } - - pub fn king_mut(&mut self) -> &mut BlackKing { - &mut self.king - } - - pub fn pawn(&self) -> BlackPawn { - self.pawn - } - - pub fn pawn_mut(&mut self) -> &mut BlackPawn { - &mut self.pawn - } -} - -impl From for Bitboard { - fn from(val: BlackPieces) -> Self { - Bitboard::merge_many(vec![ - val.rook.bitboard(), - val.knight.bitboard(), - val.bishop.bitboard(), - val.queen.bitboard(), - val.king.bitboard(), - val.pawn.bitboard(), - ]) - } -} diff --git a/libs/board/src/pieces/king/black.rs b/libs/board/src/pieces/king/black.rs index d46dcbf..c4003c9 100644 --- a/libs/board/src/pieces/king/black.rs +++ b/libs/board/src/pieces/king/black.rs @@ -48,10 +48,6 @@ impl Debug for BlackKing { } impl PieceExt for BlackKing { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - King::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { King::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/king/mod.rs b/libs/board/src/pieces/king/mod.rs index 777bc78..3f4f396 100644 --- a/libs/board/src/pieces/king/mod.rs +++ b/libs/board/src/pieces/king/mod.rs @@ -30,10 +30,6 @@ impl From for King { } impl PieceExt for King { - fn is_illegal_move(&self, _source: Square, _destination: Square, _board: Board) -> bool { - todo!() - } - fn get_attacks(&self, _piece: Bitboard, _board: Board) -> bitboard::Bitboard { todo!() } diff --git a/libs/board/src/pieces/king/white.rs b/libs/board/src/pieces/king/white.rs index f0ee8fa..78e4864 100644 --- a/libs/board/src/pieces/king/white.rs +++ b/libs/board/src/pieces/king/white.rs @@ -48,10 +48,6 @@ impl Debug for WhiteKing { } impl PieceExt for WhiteKing { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - King::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { King::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/knight/black.rs b/libs/board/src/pieces/knight/black.rs index 811a766..d64ced7 100644 --- a/libs/board/src/pieces/knight/black.rs +++ b/libs/board/src/pieces/knight/black.rs @@ -48,10 +48,6 @@ impl Debug for BlackKnight { } impl PieceExt for BlackKnight { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - Knight::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Knight::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/knight/mod.rs b/libs/board/src/pieces/knight/mod.rs index d5ddea5..f0f57cc 100644 --- a/libs/board/src/pieces/knight/mod.rs +++ b/libs/board/src/pieces/knight/mod.rs @@ -31,10 +31,6 @@ impl From for Knight { } impl PieceExt for Knight { - fn is_illegal_move(&self, _source: Square, _destination: Square, _board: Board) -> bool { - todo!() - } - fn get_attacks(&self, _piece: Bitboard, _board: Board) -> bitboard::Bitboard { todo!() } diff --git a/libs/board/src/pieces/knight/white.rs b/libs/board/src/pieces/knight/white.rs index fc824b9..a0e6e8d 100644 --- a/libs/board/src/pieces/knight/white.rs +++ b/libs/board/src/pieces/knight/white.rs @@ -48,10 +48,6 @@ impl Debug for WhiteKnight { } impl PieceExt for WhiteKnight { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - Knight::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Knight::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/mod.rs b/libs/board/src/pieces/mod.rs index 16f5e6a..e83f708 100644 --- a/libs/board/src/pieces/mod.rs +++ b/libs/board/src/pieces/mod.rs @@ -1,18 +1,15 @@ mod bishop; -mod black_pieces; mod king; mod knight; mod pawn; pub mod piece; mod queen; mod rook; -mod white_pieces; -use self::{piece::*, white_pieces::WhitePieces}; +use self::piece::*; use api::Color; pub use bishop::{black::*, white::*, Bishop}; pub use bitboard::{Bitboard, BitboardExt}; -use black_pieces::BlackPieces; pub use king::{black::*, white::*, King}; pub use knight::{black::*, white::*, Knight}; pub use pawn::{black::*, white::*, Pawn, PawnExt}; @@ -20,6 +17,54 @@ pub use queen::{black::*, white::*, Queen}; pub use rook::{black::*, white::*, Rook}; pub use std::{collections::HashMap, fmt::Debug, sync::LazyLock, vec}; +macro_rules! generate_pieces_struct { + ($pieces:ident, $($field_name:ident : $field_name_mut:ident : $field_type:ty),*) => { + #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] + pub struct $pieces { + $( + $field_name: $field_type, + )* + } + impl $pieces { + $( + pub fn $field_name(&self) -> $field_type { + self.$field_name + } + pub fn $field_name_mut(&mut self) -> &mut $field_type { + &mut self.$field_name + } + )* + } + impl From<$pieces> for Bitboard { + fn from(val: $pieces) -> Self { + Bitboard::merge_many(vec![ + $(val.$field_name.bitboard(),)* + ]) + } + } + }; +} + +generate_pieces_struct!( + WhitePieces, + rook: rook_mut: WhiteRook, + knight: knight_mut: WhiteKnight, + bishop: bishop_mut: WhiteBishop, + queen: queen_mut: WhiteQueen, + king: king_mut: WhiteKing, + pawn: pawn_mut: WhitePawn +); + +generate_pieces_struct!( + BlackPieces, + rook: rook_mut: BlackRook, + knight: knight_mut: BlackKnight, + bishop: bishop_mut: BlackBishop, + queen: queen_mut: BlackQueen, + king: king_mut: BlackKing, + pawn: pawn_mut: BlackPawn +); + pub const NUM_COLOR_PIECES: usize = 6; pub const NUM_COLORS: usize = 2; pub const NUM_PIECES: usize = NUM_COLORS * NUM_COLOR_PIECES; @@ -86,8 +131,6 @@ pub struct Pieces { empty_squares: Bitboard, } -pub trait PiecesExt {} - impl Pieces { pub fn get_pieces(&self, color: Color) -> Bitboard { if color.is_white() { @@ -201,14 +244,6 @@ impl Pieces { }; } - pub fn set_occupied_squares(&mut self, occupied_squares: Bitboard) { - self.occupied_squares = occupied_squares; - } - - pub fn set_empty_squares(&mut self, empty_squares: Bitboard) { - self.empty_squares = empty_squares; - } - pub fn update_occupied_squares(&mut self) { self.set_occupied_squares(Bitboard::merge_many(self.get_all_pieces().to_vec())); } @@ -217,6 +252,14 @@ impl Pieces { let occupied_squares = self.occupied_squares(); self.set_empty_squares(!occupied_squares); } + + fn set_occupied_squares(&mut self, occupied_squares: Bitboard) { + self.occupied_squares = occupied_squares; + } + + fn set_empty_squares(&mut self, empty_squares: Bitboard) { + self.empty_squares = empty_squares; + } } impl From<[[u8; 8]; 8]> for Pieces { @@ -252,5 +295,3 @@ impl From for Bitboard { Bitboard::merge_many(vec![value.white_pieces().into(), value.black_pieces().into()]) } } - -impl PiecesExt for Pieces {} diff --git a/libs/board/src/pieces/pawn/black.rs b/libs/board/src/pieces/pawn/black.rs index b11506c..0b2df3f 100644 --- a/libs/board/src/pieces/pawn/black.rs +++ b/libs/board/src/pieces/pawn/black.rs @@ -51,10 +51,6 @@ impl Debug for BlackPawn { } impl PieceExt for BlackPawn { - fn is_illegal_move(&self, _source: Square, _destination: Square, _board: Board) -> bool { - todo!() - } - #[inline(always)] fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Pawn::from(*self).get_attacks(piece, board) diff --git a/libs/board/src/pieces/pawn/mod.rs b/libs/board/src/pieces/pawn/mod.rs index ad03cd2..d5b7196 100644 --- a/libs/board/src/pieces/pawn/mod.rs +++ b/libs/board/src/pieces/pawn/mod.rs @@ -74,11 +74,8 @@ pub trait PawnExt: PieceExt { fn get_single_pushable_pawns(&self, empty_squares: Bitboard) -> Bitboard; fn get_double_pushable_pawns(&self, empty_squares: Bitboard) -> Bitboard; fn get_promotion_pieces(&self) -> [Piece; 4]; - fn march(&mut self, source: Square, destination: Square, board: &mut Board) -> Result<(), Box> { - if self.is_illegal_move(source, destination, *board) { - return Err(Box::new(PawnError::Illegal)); - } - Ok(()) + fn march(&mut self, _source: Square, _destination: Square, _board: &mut Board) -> Result<(), Box> { + todo!() } fn get_push_targets(&self, pawn: Bitboard, empty_squares: Bitboard) -> Bitboard { self.get_single_push_targets(pawn, empty_squares) | self.get_double_push_targets(pawn, empty_squares) @@ -162,13 +159,6 @@ impl Verify for Pawn { } impl PieceExt for Pawn { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - match self { - Pawn::Black(pawn) => pawn.is_illegal_move(source, destination, board), - Pawn::White(pawn) => pawn.is_illegal_move(source, destination, board), - } - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { let enpassant_mask = board.fen().enpassant_mask(); let west_attacks = self.get_west_attacks(piece); diff --git a/libs/board/src/pieces/pawn/white.rs b/libs/board/src/pieces/pawn/white.rs index 9993c69..f5b1022 100644 --- a/libs/board/src/pieces/pawn/white.rs +++ b/libs/board/src/pieces/pawn/white.rs @@ -51,10 +51,6 @@ impl Debug for WhitePawn { } impl PieceExt for WhitePawn { - fn is_illegal_move(&self, _source: Square, _destination: Square, _board: Board) -> bool { - todo!() - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Pawn::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/piece.rs b/libs/board/src/pieces/piece.rs index b06bcf1..2dff516 100644 --- a/libs/board/src/pieces/piece.rs +++ b/libs/board/src/pieces/piece.rs @@ -20,7 +20,6 @@ pub enum PieceError { } pub trait PieceExt: Verify { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool; fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard; fn remove_friendly_pieces(&self, piece: Bitboard, board: Board) -> Bitboard { let friendly_pieces: Bitboard = if board.fen().is_white() { @@ -238,17 +237,6 @@ impl Piece { } impl PieceExt for Piece { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - match self { - Piece::Rook(rook) => rook.is_illegal_move(source, destination, board), - Piece::Knight(knight) => knight.is_illegal_move(source, destination, board), - Piece::Bishop(bishop) => bishop.is_illegal_move(source, destination, board), - Piece::Queen(queen) => queen.is_illegal_move(source, destination, board), - Piece::King(king) => king.is_illegal_move(source, destination, board), - Piece::Pawn(pawn) => pawn.is_illegal_move(source, destination, board), - } - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { match self { Piece::Rook(rook) => rook.get_attacks(piece, board), diff --git a/libs/board/src/pieces/queen/black.rs b/libs/board/src/pieces/queen/black.rs index d04aa59..66bd678 100644 --- a/libs/board/src/pieces/queen/black.rs +++ b/libs/board/src/pieces/queen/black.rs @@ -48,10 +48,6 @@ impl Debug for BlackQueen { } impl PieceExt for BlackQueen { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - Queen::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Queen::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/queen/mod.rs b/libs/board/src/pieces/queen/mod.rs index 64a552c..751048d 100644 --- a/libs/board/src/pieces/queen/mod.rs +++ b/libs/board/src/pieces/queen/mod.rs @@ -30,10 +30,6 @@ impl From for Queen { } impl PieceExt for Queen { - fn is_illegal_move(&self, _source: Square, _destination: Square, _board: Board) -> bool { - todo!() - } - fn get_attacks(&self, _piece: Bitboard, _board: Board) -> bitboard::Bitboard { todo!() } diff --git a/libs/board/src/pieces/queen/white.rs b/libs/board/src/pieces/queen/white.rs index 87da0dd..04c8d40 100644 --- a/libs/board/src/pieces/queen/white.rs +++ b/libs/board/src/pieces/queen/white.rs @@ -48,10 +48,6 @@ impl Debug for WhiteQueen { } impl PieceExt for WhiteQueen { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - Queen::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Queen::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/rook/black.rs b/libs/board/src/pieces/rook/black.rs index 01358ba..c17aa82 100644 --- a/libs/board/src/pieces/rook/black.rs +++ b/libs/board/src/pieces/rook/black.rs @@ -48,10 +48,6 @@ impl Debug for BlackRook { } impl PieceExt for BlackRook { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - Rook::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Rook::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/rook/mod.rs b/libs/board/src/pieces/rook/mod.rs index 6622326..a836028 100644 --- a/libs/board/src/pieces/rook/mod.rs +++ b/libs/board/src/pieces/rook/mod.rs @@ -30,10 +30,6 @@ impl From for Rook { } impl PieceExt for Rook { - fn is_illegal_move(&self, _source: Square, _destination: Square, _board: Board) -> bool { - todo!() - } - fn get_attacks(&self, _piece: Bitboard, _board: Board) -> bitboard::Bitboard { todo!() } diff --git a/libs/board/src/pieces/rook/white.rs b/libs/board/src/pieces/rook/white.rs index 90009ce..34f1a5d 100644 --- a/libs/board/src/pieces/rook/white.rs +++ b/libs/board/src/pieces/rook/white.rs @@ -48,10 +48,6 @@ impl Debug for WhiteRook { } impl PieceExt for WhiteRook { - fn is_illegal_move(&self, source: Square, destination: Square, board: Board) -> bool { - Rook::from(*self).is_illegal_move(source, destination, board) - } - fn get_attacks(&self, piece: Bitboard, board: Board) -> Bitboard { Rook::from(*self).get_attacks(piece, board) } diff --git a/libs/board/src/pieces/white_pieces.rs b/libs/board/src/pieces/white_pieces.rs deleted file mode 100644 index 484f5eb..0000000 --- a/libs/board/src/pieces/white_pieces.rs +++ /dev/null @@ -1,75 +0,0 @@ -use super::{WhiteBishop, WhiteKing, WhiteKnight, WhitePawn, WhiteQueen, WhiteRook}; -use bitboard::{Bitboard, BitboardExt}; - -#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] -pub struct WhitePieces { - rook: WhiteRook, - knight: WhiteKnight, - bishop: WhiteBishop, - queen: WhiteQueen, - king: WhiteKing, - pawn: WhitePawn, -} - -impl WhitePieces { - pub fn rook(&self) -> WhiteRook { - self.rook - } - - pub fn rook_mut(&mut self) -> &mut WhiteRook { - &mut self.rook - } - - pub fn knight(&self) -> WhiteKnight { - self.knight - } - - pub fn knight_mut(&mut self) -> &mut WhiteKnight { - &mut self.knight - } - - pub fn bishop(&self) -> WhiteBishop { - self.bishop - } - - pub fn bishop_mut(&mut self) -> &mut WhiteBishop { - &mut self.bishop - } - - pub fn queen(&self) -> WhiteQueen { - self.queen - } - - pub fn queen_mut(&mut self) -> &mut WhiteQueen { - &mut self.queen - } - - pub fn king(&self) -> WhiteKing { - self.king - } - - pub fn king_mut(&mut self) -> &mut WhiteKing { - &mut self.king - } - - pub fn pawn(&self) -> WhitePawn { - self.pawn - } - - pub fn pawn_mut(&mut self) -> &mut WhitePawn { - &mut self.pawn - } -} - -impl From for Bitboard { - fn from(val: WhitePieces) -> Self { - Bitboard::merge_many(vec![ - val.rook.bitboard(), - val.knight.bitboard(), - val.bishop.bitboard(), - val.queen.bitboard(), - val.king.bitboard(), - val.pawn.bitboard(), - ]) - } -} From d9f7b0dad196b01414e16405510ca02a32e6f063 Mon Sep 17 00:00:00 2001 From: Clemens Horn Date: Fri, 16 Feb 2024 09:04:10 +0100 Subject: [PATCH 6/6] ci(repo): codecov action update test --- .github/workflows/ci.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4ee1a74..86e46b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -109,11 +109,12 @@ jobs: run: pnpm coverage:affected --nx-bail --base=$NX_BASE --head=$NX_HEAD --exclude gui - name: Upload coverage reports to Codecov - uses: codecov/codecov-action@v3 if: matrix.platform == 'ubuntu-latest' - env: + uses: codecov/codecov-action@v4 + with: + fail_ci_if_error: true + verbose: true token: ${{ secrets.CODECOV_TOKEN }} - slug: ${{ github.repository }} cleanup: name: Cleanup