[board, explorer, position] Clean up naming of sight and movement methods

These methods have a prefix, either `sight` or `movement`, and then follow the conventions
for other "trio" clusters where there's an un-suffixed method that takes an
Option<Color>, a _active method that uses the active color, and a _unwrapped
method that takes a bare Color.
This commit is contained in:
Eryn Wells 2025-06-29 09:23:20 -07:00
parent e7fd65672d
commit a30553503f
4 changed files with 42 additions and 25 deletions

View file

@ -4,12 +4,12 @@
//! of squares a piece can move to. For all pieces except pawns, the Movement //! of squares a piece can move to. For all pieces except pawns, the Movement
//! set is equal to the Sight set. //! set is equal to the Sight set.
use crate::{sight::Sight, Board}; use crate::{Board, sight::Sight};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, Rank, Shape, Square, Wing}; use chessfriend_core::{Color, Piece, Rank, Shape, Square, Wing};
impl Board { impl Board {
pub fn movement(&self, square: Square) -> BitBoard { pub fn movement_piece(&self, square: Square) -> BitBoard {
if let Some(piece) = self.get_piece(square) { if let Some(piece) = self.get_piece(square) {
piece.movement(square, self) piece.movement(square, self)
} else { } else {
@ -93,7 +93,7 @@ fn pawn_pushes(pawn: BitBoard, color: Color, occupancy: BitBoard) -> BitBoard {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::pawn_pushes; use super::pawn_pushes;
use chessfriend_bitboard::{bitboard, BitBoard}; use chessfriend_bitboard::{BitBoard, bitboard};
use chessfriend_core::{Color, Square}; use chessfriend_core::{Color, Square};
#[test] #[test]

View file

@ -23,7 +23,7 @@ use std::ops::BitOr;
impl Board { impl Board {
/// Compute sight of the piece on the given square. /// Compute sight of the piece on the given square.
pub fn sight(&self, square: Square) -> BitBoard { pub fn sight_piece(&self, square: Square) -> BitBoard {
if let Some(piece) = self.get_piece(square) { if let Some(piece) = self.get_piece(square) {
piece.sight(square, self) piece.sight(square, self)
} else { } else {
@ -31,8 +31,23 @@ impl Board {
} }
} }
pub fn active_sight(&self) -> BitBoard { /// Calculate sight of all pieces of the given [`Color`]. If `color` is
self.friendly_sight(self.active_color()) /// `None`, calculate sight of the active color.
pub fn sight(&self, color: Option<Color>) -> BitBoard {
self.sight_unwrapped(self.unwrap_color(color))
}
/// Calculate sight of all pieces of the active color.
pub fn sight_active(&self) -> BitBoard {
self.sight_unwrapped(self.active_color())
}
/// Calculate sight of all pieces of the given [`Color`].
pub fn sight_unwrapped(&self, color: Color) -> BitBoard {
self.friendly_occupancy(color)
.occupied_squares_leading()
.map(|square| self.sight_piece(square))
.fold(BitBoard::EMPTY, BitOr::bitor)
} }
/// A [`BitBoard`] of all squares the given color can see. /// A [`BitBoard`] of all squares the given color can see.
@ -40,7 +55,7 @@ impl Board {
// TODO: Probably want to implement a caching layer here. // TODO: Probably want to implement a caching layer here.
self.friendly_occupancy(color) self.friendly_occupancy(color)
.occupied_squares(&IterationDirection::default()) .occupied_squares(&IterationDirection::default())
.map(|square| self.sight(square)) .map(|square| self.sight_piece(square))
.fold(BitBoard::EMPTY, BitOr::bitor) .fold(BitBoard::EMPTY, BitOr::bitor)
} }
@ -244,7 +259,7 @@ mod tests {
White King on E4, White King on E4,
); );
let sight = pos.active_sight(); let sight = pos.sight_active();
assert_eq!(sight, bitboard![E5 F5 F4 F3 E3 D3 D4 D5]); assert_eq!(sight, bitboard![E5 F5 F4 F3 E3 D3 D4 D5]);
} }
@ -267,8 +282,8 @@ mod tests {
mod pawn { mod pawn {
use crate::{sight::Sight, test_board}; use crate::{sight::Sight, test_board};
use chessfriend_bitboard::{bitboard, BitBoard}; use chessfriend_bitboard::{BitBoard, bitboard};
use chessfriend_core::{piece, Square}; use chessfriend_core::{Square, piece};
sight_test!(e4_pawn, piece!(White Pawn), Square::E4, bitboard![D5 F5]); sight_test!(e4_pawn, piece!(White Pawn), Square::E4, bitboard![D5 F5]);

View file

@ -65,7 +65,7 @@ fn command_line() -> Command {
) )
.subcommand( .subcommand(
Command::new("sight") Command::new("sight")
.arg(Arg::new("square").required(true)) .arg(Arg::new("square").required(false))
.about("Show sight of a piece on a square"), .about("Show sight of a piece on a square"),
) )
.subcommand( .subcommand(
@ -163,12 +163,12 @@ fn respond(line: &str, state: &mut State) -> anyhow::Result<CommandResult> {
.place_piece(piece, square, PlacePieceStrategy::default())?; .place_piece(piece, square, PlacePieceStrategy::default())?;
} }
Some(("sight", matches)) => { Some(("sight", matches)) => {
let square = matches let sight = if let Some(square) = matches.get_one::<String>("square") {
.get_one::<String>("square") let square: Square = square.parse()?;
.ok_or(CommandHandlingError::MissingArgument("square"))?; state.position.sight_piece(square)
let square = square.parse::<Square>()?; } else {
state.position.sight_active()
let sight = state.position.sight(square); };
let display = state.position.display().highlight(sight); let display = state.position.display().highlight(sight);
println!("\n{display}"); println!("\n{display}");
@ -331,7 +331,7 @@ fn do_movement_command(
.get_one::<Square>("square") .get_one::<Square>("square")
.ok_or(CommandHandlingError::MissingArgument("square"))?; .ok_or(CommandHandlingError::MissingArgument("square"))?;
let movement = state.position.movement(square); let movement = state.position.movement_piece(square);
let display = state.position.display().highlight(movement); let display = state.position.display().highlight(movement);
println!("\n{display}"); println!("\n{display}");

View file

@ -86,12 +86,14 @@ impl Position {
} }
impl Position { impl Position {
pub fn sight(&self, square: Square) -> BitBoard { /// Calculate sight of a piece on the provided [`Square`].
self.board.sight(square) pub fn sight_piece(&self, square: Square) -> BitBoard {
self.board.sight_piece(square)
} }
pub fn movement(&self, square: Square) -> BitBoard { /// Calculate movement of a piece on the provided [`Square`].
self.board.movement(square) pub fn movement_piece(&self, square: Square) -> BitBoard {
self.board.movement_piece(square)
} }
} }
@ -163,8 +165,8 @@ impl Position {
} }
impl Position { impl Position {
pub fn active_sight(&self) -> BitBoard { pub fn sight_active(&self) -> BitBoard {
self.board.active_sight() self.board.sight_active()
} }
/// A [`BitBoard`] of all squares the given color can see. /// A [`BitBoard`] of all squares the given color can see.
@ -285,7 +287,7 @@ impl Position {
} }
let target_bitboard: BitBoard = target.into(); let target_bitboard: BitBoard = target.into();
if !(self.movement(origin) & target_bitboard).is_populated() { if !(self.movement_piece(origin) & target_bitboard).is_populated() {
return None; return None;
} }