diff --git a/position/src/position.rs b/position/src/position.rs index a5f7fb2..ea94971 100644 --- a/position/src/position.rs +++ b/position/src/position.rs @@ -2,10 +2,152 @@ mod captures; mod make_move; -mod position; mod unmake_move; -pub use { - make_move::{MakeMoveError, ValidateMove}, - position::Position, +pub use make_move::{MakeMoveError, ValidateMove}; + +use crate::move_record::MoveRecord; +use captures::CapturesList; +use chessfriend_bitboard::BitBoard; +use chessfriend_board::{ + display::DiagramFormatter, fen::ToFenStr, Board, PlacePieceError, PlacePieceStrategy, }; +use chessfriend_core::{Color, Piece, Square}; +use std::{cell::OnceCell, fmt}; + +#[must_use] +#[derive(Clone, Debug, Default, Eq)] +pub struct Position { + pub board: Board, + pub(crate) moves: Vec, + pub(crate) captures: CapturesList, +} + +impl Position { + pub fn empty() -> Self { + Position::default() + } + + /// Return a starting position. + pub fn starting() -> Self { + Self { + board: Board::starting(), + ..Default::default() + } + } + + pub fn new(board: Board) -> Self { + Self { + board, + ..Default::default() + } + } +} + +impl Position { + /// Place a piece on the board. + /// + /// ## Errors + /// + /// See [`chessfriend_board::Board::place_piece`]. + pub fn place_piece( + &mut self, + piece: Piece, + square: Square, + strategy: PlacePieceStrategy, + ) -> Result<(), PlacePieceError> { + self.board.place_piece(piece, square, strategy) + } + + #[must_use] + pub fn get_piece(&self, square: Square) -> Option { + self.board.get_piece(square) + } + + pub fn remove_piece(&mut self, square: Square) -> Option { + self.board.remove_piece(square) + } +} + +impl Position { + pub fn sight(&self, square: Square) -> BitBoard { + self.board.sight(square) + } + + pub fn movement(&self, square: Square) -> BitBoard { + self.board.movement(square) + } +} + +impl Position { + pub fn active_sight(&self) -> BitBoard { + self.board.active_sight() + } + + /// A [`BitBoard`] of all squares the given color can see. + pub fn friendly_sight(&self, color: Color) -> BitBoard { + self.board.friendly_sight(color) + } + + /// A [`BitBoard`] of all squares visible by colors that oppose the given color. + pub fn active_color_opposing_sight(&self) -> BitBoard { + self.board.active_color_opposing_sight() + } +} + +impl Position { + pub fn display(&self) -> DiagramFormatter { + self.board.display() + } +} + +impl ToFenStr for Position { + type Error = ::Error; + + fn to_fen_str(&self) -> Result { + self.board.to_fen_str() + } +} + +impl PartialEq for Position { + fn eq(&self, other: &Self) -> bool { + self.board == other.board + } +} + +impl fmt::Display for Position { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.board.display())?; + + if !self.captures.is_empty() { + write!(f, "\n\n{}", self.captures)?; + } + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{test_position, Position}; + use chessfriend_core::piece; + + #[test] + fn piece_on_square() { + let pos = test_position![ + Black Bishop on F7, + ]; + + let piece = pos.board.get_piece(Square::F7); + assert_eq!(piece, Some(piece!(Black Bishop))); + } + + #[test] + fn piece_in_starting_position() { + let pos = test_position!(starting); + + assert_eq!(pos.board.get_piece(Square::H1), Some(piece!(White Rook))); + assert_eq!(pos.board.get_piece(Square::A8), Some(piece!(Black Rook))); + } +} diff --git a/position/src/position/position.rs b/position/src/position/position.rs index 89596be..80e9f65 100644 --- a/position/src/position/position.rs +++ b/position/src/position/position.rs @@ -1,93 +1,9 @@ // Eryn Wells -use super::captures::CapturesList; -use crate::move_record::MoveRecord; -use chessfriend_bitboard::BitBoard; -use chessfriend_board::{ - display::DiagramFormatter, fen::ToFenStr, Board, PlacePieceError, PlacePieceStrategy, -}; -use chessfriend_core::{Color, Piece, Square}; -use std::{cell::OnceCell, fmt}; - -#[must_use] -#[derive(Clone, Debug, Default, Eq)] -pub struct Position { - pub board: Board, - pub(crate) moves: Vec, - pub(crate) captures: CapturesList, -} - -impl Position { - pub fn empty() -> Self { - Position::default() - } - - /// Return a starting position. - pub fn starting() -> Self { - Self { - board: Board::starting(), - ..Default::default() - } - } - - pub fn new(board: Board) -> Self { - Self { - board, - ..Default::default() - } - } -} - -impl Position { - /// Place a piece on the board. - /// - /// ## Errors - /// - /// See [`chessfriend_board::Board::place_piece`]. - pub fn place_piece( - &mut self, - piece: Piece, - square: Square, - strategy: PlacePieceStrategy, - ) -> Result<(), PlacePieceError> { - self.board.place_piece(piece, square, strategy) - } - - #[must_use] - pub fn get_piece(&self, square: Square) -> Option { - self.board.get_piece(square) - } - - pub fn remove_piece(&mut self, square: Square) -> Option { - self.board.remove_piece(square) - } -} - -impl Position { - pub fn sight(&self, square: Square) -> BitBoard { - self.board.sight(square) - } - - pub fn movement(&self, square: Square) -> BitBoard { - self.board.movement(square) - } -} - -impl Position { - pub fn active_sight(&self) -> BitBoard { - self.board.active_sight() - } - - /// A [`BitBoard`] of all squares the given color can see. - pub fn friendly_sight(&self, color: Color) -> BitBoard { - self.board.friendly_sight(color) - } - - /// A [`BitBoard`] of all squares visible by colors that oppose the given color. - pub fn active_color_opposing_sight(&self) -> BitBoard { - self.board.active_color_opposing_sight() - } -} +/*** + * Keeping this code around for a little while because it might still come in + * handy, but it should be considered dead. + ***/ /* impl Position { @@ -166,62 +82,8 @@ impl Position { } */ -impl Position { - pub fn display(&self) -> DiagramFormatter { - self.board.display() - } -} - -impl ToFenStr for Position { - type Error = ::Error; - - fn to_fen_str(&self) -> Result { - self.board.to_fen_str() - } -} - -impl PartialEq for Position { - fn eq(&self, other: &Self) -> bool { - self.board == other.board - } -} - -impl fmt::Display for Position { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.board.display())?; - - if !self.captures.is_empty() { - write!(f, "\n\n{}", self.captures)?; - } - - Ok(()) - } -} - #[cfg(test)] mod tests { - use super::*; - use crate::{test_position, Position}; - use chessfriend_core::piece; - - #[test] - fn piece_on_square() { - let pos = test_position![ - Black Bishop on F7, - ]; - - let piece = pos.board.get_piece(Square::F7); - assert_eq!(piece, Some(piece!(Black Bishop))); - } - - #[test] - fn piece_in_starting_position() { - let pos = test_position!(starting); - - assert_eq!(pos.board.get_piece(Square::H1), Some(piece!(White Rook))); - assert_eq!(pos.board.get_piece(Square::A8), Some(piece!(Black Rook))); - } - // #[test] // fn king_is_in_check() { // let pos = position![