Implement part of a new Builder using the type state pattern
The API for this is much easier to use.
This commit is contained in:
parent
f69c7d4c96
commit
0bedf2aa9f
1 changed files with 105 additions and 0 deletions
105
moves/src/builder.rs
Normal file
105
moves/src/builder.rs
Normal file
|
|
@ -0,0 +1,105 @@
|
||||||
|
// 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue