[board, moves] Make Board::castling_rights and Board::en_passant_target private
Make the struct fields private and export getters and various setters for manipulating the data. Update all the references to these fields to use the getters and setters instead.
This commit is contained in:
parent
eaab34587c
commit
404212363e
10 changed files with 96 additions and 51 deletions
|
@ -7,7 +7,7 @@ use crate::{
|
|||
PieceSet,
|
||||
};
|
||||
use chessfriend_bitboard::BitBoard;
|
||||
use chessfriend_core::{Color, Piece, Shape, Square};
|
||||
use chessfriend_core::{Color, Piece, Shape, Square, Wing};
|
||||
|
||||
pub type HalfMoveClock = u32;
|
||||
pub type FullMoveClock = u32;
|
||||
|
@ -16,8 +16,8 @@ pub type FullMoveClock = u32;
|
|||
pub struct Board {
|
||||
active_color: Color,
|
||||
pieces: PieceSet,
|
||||
pub castling_rights: castle::Rights,
|
||||
pub en_passant_target: Option<Square>,
|
||||
castling_rights: castle::Rights,
|
||||
en_passant_target: Option<Square>,
|
||||
pub half_move_clock: HalfMoveClock,
|
||||
pub full_move_number: FullMoveClock,
|
||||
}
|
||||
|
@ -71,6 +71,60 @@ impl Board {
|
|||
self.active_color = color;
|
||||
}
|
||||
|
||||
impl Board {
|
||||
#[must_use]
|
||||
pub fn castling_rights(&self) -> castle::Rights {
|
||||
self.castling_rights
|
||||
}
|
||||
|
||||
pub fn set_castling_rights(&mut self, rights: castle::Rights) {
|
||||
self.castling_rights = rights;
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn active_color_has_castling_right(&self, wing: Wing) -> bool {
|
||||
self.color_has_castling_right(self.active_color, wing)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn color_has_castling_right(&self, color: Color, wing: Wing) -> bool {
|
||||
self.castling_rights.color_has_right(color, wing)
|
||||
}
|
||||
|
||||
pub fn grant_castling_right(&mut self, color: Color, wing: Wing) {
|
||||
self.castling_rights.grant(color, wing);
|
||||
}
|
||||
|
||||
pub fn revoke_all_castling_rights(&mut self) {
|
||||
self.castling_rights.revoke_all();
|
||||
}
|
||||
|
||||
pub fn revoke_castling_right(&mut self, color: Color, wing: Wing) {
|
||||
self.castling_rights.revoke(color, wing);
|
||||
}
|
||||
}
|
||||
|
||||
impl Board {
|
||||
/// Returns a copy of the current en passant square, if one exists.
|
||||
#[must_use]
|
||||
pub fn en_passant_target(&self) -> Option<Square> {
|
||||
self.en_passant_target
|
||||
}
|
||||
|
||||
pub fn set_en_passant_target(&mut self, square: Square) {
|
||||
self.set_en_passant_target_option(Some(square));
|
||||
}
|
||||
|
||||
pub fn set_en_passant_target_option(&mut self, square: Option<Square>) {
|
||||
self.en_passant_target = square;
|
||||
}
|
||||
|
||||
pub fn clear_en_passant_target(&mut self) {
|
||||
self.en_passant_target = None;
|
||||
}
|
||||
}
|
||||
|
||||
impl Board {
|
||||
#[must_use]
|
||||
pub fn get_piece(&self, square: Square) -> Option<Piece> {
|
||||
self.pieces.get(square)
|
||||
|
|
|
@ -46,7 +46,7 @@ impl Board {
|
|||
|
||||
let color = self.unwrap_color(color);
|
||||
|
||||
if !self.castling_rights.color_has_right(color, wing) {
|
||||
if !self.color_has_castling_right(color, wing) {
|
||||
return Err(CastleEvaluationError::NoRights { color, wing });
|
||||
}
|
||||
|
||||
|
@ -98,15 +98,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn king_on_starting_square_can_castle() {
|
||||
let pos = test_board!(
|
||||
let board = test_board!(
|
||||
White King on E1,
|
||||
White Rook on A1,
|
||||
White Rook on H1
|
||||
);
|
||||
|
||||
let rights = pos.castling_rights;
|
||||
assert!(rights.color_has_right(Color::White, Wing::KingSide));
|
||||
assert!(rights.color_has_right(Color::White, Wing::QueenSide));
|
||||
assert!(board.color_has_castling_right(Color::White, Wing::KingSide));
|
||||
assert!(board.color_has_castling_right(Color::White, Wing::QueenSide));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -123,7 +123,7 @@ impl ToFenStr for Board {
|
|||
(Color::Black, Wing::QueenSide),
|
||||
]
|
||||
.map(|(color, castle)| {
|
||||
if !self.castling_rights.color_has_right(color, castle) {
|
||||
if !self.color_has_castling_right(color, castle) {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
@ -146,7 +146,7 @@ impl ToFenStr for Board {
|
|||
write!(
|
||||
fen_string,
|
||||
" {}",
|
||||
self.en_passant_target
|
||||
self.en_passant_target()
|
||||
.map_or("-".to_string(), |square| square.to_string())
|
||||
)
|
||||
.map_err(ToFenStrError::FmtError)?;
|
||||
|
@ -230,14 +230,14 @@ impl FromFenStr for Board {
|
|||
.next()
|
||||
.ok_or(FromFenStrError::MissingField(Field::CastlingRights))?;
|
||||
if castling_rights == "-" {
|
||||
board.castling_rights.revoke_all();
|
||||
board.revoke_all_castling_rights();
|
||||
} else {
|
||||
for ch in castling_rights.chars() {
|
||||
match ch {
|
||||
'K' => board.castling_rights.grant(Color::White, Wing::KingSide),
|
||||
'Q' => board.castling_rights.grant(Color::White, Wing::QueenSide),
|
||||
'k' => board.castling_rights.grant(Color::Black, Wing::KingSide),
|
||||
'q' => board.castling_rights.grant(Color::Black, Wing::QueenSide),
|
||||
'K' => board.grant_castling_right(Color::White, Wing::KingSide),
|
||||
'Q' => board.grant_castling_right(Color::White, Wing::QueenSide),
|
||||
'k' => board.grant_castling_right(Color::Black, Wing::KingSide),
|
||||
'q' => board.grant_castling_right(Color::Black, Wing::QueenSide),
|
||||
_ => return Err(FromFenStrError::InvalidValue),
|
||||
};
|
||||
}
|
||||
|
@ -249,7 +249,7 @@ impl FromFenStr for Board {
|
|||
if en_passant_square != "-" {
|
||||
let square = Square::from_algebraic_str(en_passant_square)
|
||||
.map_err(FromFenStrError::ParseSquareError)?;
|
||||
board.en_passant_target = Some(square);
|
||||
board.set_en_passant_target(square);
|
||||
}
|
||||
|
||||
let half_move_clock = fields
|
||||
|
|
|
@ -14,7 +14,7 @@ macro_rules! test_board {
|
|||
$crate::PlacePieceStrategy::default());
|
||||
)*
|
||||
board.set_active_color(chessfriend_core::Color::$to_move);
|
||||
board.en_passant_target = Some(chessfriend_core::Square::$en_passant);
|
||||
board.set_en_passant_target(chessfriend_core::Square::$en_passant);
|
||||
|
||||
println!("{}", board.display());
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ impl Movement for Piece {
|
|||
|
||||
match self.shape {
|
||||
Shape::Pawn => {
|
||||
let en_passant_square: BitBoard = board.en_passant_target.into();
|
||||
let en_passant_square: BitBoard = board.en_passant_target().into();
|
||||
// Pawns can only move to squares they can see to capture.
|
||||
let sight = self.sight(square, board) & (opposing_occupancy | en_passant_square);
|
||||
let pushes = pawn_pushes(square.into(), self.color, board.occupancy());
|
||||
|
|
|
@ -79,7 +79,7 @@ impl Sight for Piece {
|
|||
|
||||
match self.shape {
|
||||
Shape::Pawn => {
|
||||
let en_passant_square: BitBoard = board.en_passant_target.into();
|
||||
let en_passant_square: BitBoard = board.en_passant_target().into();
|
||||
match self.color {
|
||||
Color::White => white_pawn_sight(&info, en_passant_square),
|
||||
Color::Black => black_pawn_sight(&info, en_passant_square),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue