[position] Move capture list to a CapturesList struct
This one has a custom Display implementation for easier integration with Positon's.
This commit is contained in:
parent
eb6f2000a9
commit
e3ca466737
4 changed files with 53 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
mod captures;
|
||||
mod make_move;
|
||||
mod position;
|
||||
|
||||
|
|
42
position/src/position/captures.rs
Normal file
42
position/src/position/captures.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
use chessfriend_core::{Color, Piece};
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub(crate) struct CapturesList([Vec<Piece>; Color::NUM]);
|
||||
|
||||
impl CapturesList {
|
||||
pub fn push(&mut self, color: Color, piece: Piece) {
|
||||
self.0[color as usize].push(piece);
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.iter().all(Vec::is_empty)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for CapturesList {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut is_first = true;
|
||||
for color in Color::into_iter() {
|
||||
if is_first {
|
||||
is_first = false;
|
||||
} else {
|
||||
writeln!(f)?;
|
||||
}
|
||||
|
||||
write!(f, "{color}: ")?;
|
||||
|
||||
let captures = &self.0[color as usize];
|
||||
for piece in captures {
|
||||
write!(f, "{piece}")?;
|
||||
}
|
||||
|
||||
write!(f, "")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -172,7 +172,7 @@ impl Position {
|
|||
.ok_or(MakeMoveError::NoCapturePiece(capture_square))?;
|
||||
|
||||
// Register the capture
|
||||
self.captures[piece.color as usize].push(captured_piece);
|
||||
self.captures.push(piece.color, captured_piece);
|
||||
|
||||
self.remove_piece(origin_square).unwrap();
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use super::captures::CapturesList;
|
||||
use crate::move_record::MoveRecord;
|
||||
use chessfriend_bitboard::BitBoard;
|
||||
use chessfriend_board::{
|
||||
|
@ -13,7 +14,7 @@ use std::{cell::OnceCell, fmt};
|
|||
pub struct Position {
|
||||
pub board: Board,
|
||||
pub(crate) moves: Vec<MoveRecord>,
|
||||
pub(crate) captures: [Vec<Piece>; Color::NUM],
|
||||
pub(crate) captures: CapturesList,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
|
@ -197,7 +198,13 @@ impl PartialEq for Position {
|
|||
|
||||
impl fmt::Display for Position {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.board.display())
|
||||
write!(f, "{}", self.board.display())?;
|
||||
|
||||
if !self.captures.is_empty() {
|
||||
write!(f, "\n\n{}", self.captures)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue