[board] Implement a position! macro for creating Positions
For example: ``` let position = position![ White King on E4, Black Rook on E8, ]; ```
This commit is contained in:
parent
94ab64d277
commit
7c80c61690
2 changed files with 21 additions and 0 deletions
|
@ -4,6 +4,7 @@ mod bitboard;
|
|||
mod moves;
|
||||
#[macro_use]
|
||||
pub mod piece;
|
||||
#[macro_use]
|
||||
pub mod position;
|
||||
mod square;
|
||||
|
||||
|
|
|
@ -18,6 +18,15 @@ pub(crate) enum BoardSide {
|
|||
Queen,
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! position {
|
||||
[$($color:ident $shape:ident on $square:ident,)*] => {
|
||||
Position::from_iter([
|
||||
$(piece!($color $shape on $square)),*
|
||||
].into_iter())
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, Hash, PartialEq)]
|
||||
pub struct Position {
|
||||
color_to_move: Color,
|
||||
|
@ -199,6 +208,17 @@ impl Default for Position {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromIterator<PlacedPiece> for Position {
|
||||
fn from_iter<T: IntoIterator<Item = PlacedPiece>>(iter: T) -> Self {
|
||||
let mut position = Position::empty();
|
||||
for placed_piece in iter {
|
||||
_ = position.place_piece(placed_piece.piece(), placed_piece.square());
|
||||
}
|
||||
|
||||
position
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Position {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut output = String::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue