106 lines
2.1 KiB
Rust
106 lines
2.1 KiB
Rust
|
// Eryn Wells <eryn@erynwells.me>
|
||
|
|
||
|
use crate::{castle, Move};
|
||
|
use chessfriend_core::{PlacedPiece, Square};
|
||
|
|
||
|
pub trait Style {}
|
||
|
|
||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||
|
pub struct Builder<S: Style> {
|
||
|
style: S,
|
||
|
}
|
||
|
|
||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||
|
pub struct Null;
|
||
|
|
||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||
|
pub struct Push {
|
||
|
from: Option<Square>,
|
||
|
to: Option<Square>,
|
||
|
}
|
||
|
|
||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||
|
pub struct Capture {
|
||
|
push: Push,
|
||
|
capture: Option<Square>,
|
||
|
}
|
||
|
|
||
|
pub struct Castle {
|
||
|
castle: castle::Castle,
|
||
|
}
|
||
|
|
||
|
impl Style for Null {}
|
||
|
impl Style for Push {}
|
||
|
impl Style for Capture {}
|
||
|
impl Style for Castle {}
|
||
|
|
||
|
impl Builder<Null> {
|
||
|
pub fn new() -> Self {
|
||
|
Self { style: Null }
|
||
|
}
|
||
|
|
||
|
pub fn piece(piece: &PlacedPiece) -> Builder<Push> {
|
||
|
Builder {
|
||
|
style: Push {
|
||
|
from: Some(piece.square()),
|
||
|
to: None,
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn castling(castle: castle::Castle) -> Builder<Castle> {
|
||
|
Builder {
|
||
|
style: Castle { castle },
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn build(&self) -> Move {
|
||
|
Move(0)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Builder<Push> {
|
||
|
pub fn from(mut self, square: Square) -> Self {
|
||
|
self.style.from = Some(square);
|
||
|
self
|
||
|
}
|
||
|
|
||
|
pub fn to(mut self, square: Square) -> Self {
|
||
|
self.style.to = Some(square);
|
||
|
self
|
||
|
}
|
||
|
|
||
|
pub fn capturing(self, square: Square) -> Builder<Capture> {
|
||
|
Builder {
|
||
|
style: Capture {
|
||
|
push: self.style,
|
||
|
capture: Some(square),
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn capturing_piece(self, piece: PlacedPiece) -> Builder<Capture> {
|
||
|
Builder {
|
||
|
style: Capture {
|
||
|
push: self.style,
|
||
|
capture: Some(piece.square()),
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Builder<Castle> {
|
||
|
pub fn build(&self) -> Move {
|
||
|
Move(self.style.into_bits())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Castle {
|
||
|
fn into_bits(&self) -> u16 {
|
||
|
match self.castle {
|
||
|
castle::Castle::KingSide => 0b10,
|
||
|
castle::Castle::QueenSide => 0b11,
|
||
|
}
|
||
|
}
|
||
|
}
|