[position] Remove position::position module

Move the implementation of Position to the top-level position.rs file.
This commit is contained in:
Eryn Wells 2025-05-27 11:59:42 -07:00
parent db489af50b
commit 085697d38f
2 changed files with 150 additions and 146 deletions

View file

@ -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<MoveRecord>,
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<Piece> {
self.board.get_piece(square)
}
pub fn remove_piece(&mut self, square: Square) -> Option<Piece> {
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 = <Board as ToFenStr>::Error;
fn to_fen_str(&self) -> Result<String, Self::Error> {
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)));
}
}

View file

@ -1,93 +1,9 @@
// Eryn Wells <eryn@erynwells.me>
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<MoveRecord>,
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<Piece> {
self.board.get_piece(square)
}
pub fn remove_piece(&mut self, square: Square) -> Option<Piece> {
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 = <Board as ToFenStr>::Error;
fn to_fen_str(&self) -> Result<String, Self::Error> {
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![