[explorer, position] Make Position.board private to the crate

Export a Position::board() method that returns a reference to the internal Board.
This commit is contained in:
Eryn Wells 2025-06-21 21:07:26 -07:00
parent 4ae1fd62b7
commit f84319272c
3 changed files with 12 additions and 7 deletions

View file

@ -187,7 +187,7 @@ fn respond(line: &str, state: &mut State) -> anyhow::Result<CommandResult> {
} }
fn do_flags_command(state: &mut State, _matches: &clap::ArgMatches) -> CommandResult { fn do_flags_command(state: &mut State, _matches: &clap::ArgMatches) -> CommandResult {
let board = &state.position.board; let board = state.position.board();
println!("Castling:"); println!("Castling:");
@ -390,7 +390,7 @@ fn main() -> Result<(), String> {
loop { loop {
if should_print_position { if should_print_position {
println!("{}", &state.position); println!("{}", &state.position);
println!("{} to move.", state.position.board.active_color()); println!("{} to move.", state.position.active_color());
} }
let readline = editor.readline("\n? "); let readline = editor.readline("\n? ");

View file

@ -24,7 +24,7 @@ use std::{collections::HashSet, fmt, sync::Arc};
#[must_use] #[must_use]
#[derive(Clone, Debug, Default, Eq)] #[derive(Clone, Debug, Default, Eq)]
pub struct Position { pub struct Position {
pub board: Board, pub(crate) board: Board,
pub(crate) moves: Vec<MoveRecord>, pub(crate) moves: Vec<MoveRecord>,
pub(crate) captures: CapturesList, pub(crate) captures: CapturesList,
@ -48,6 +48,11 @@ impl Position {
..Default::default() ..Default::default()
} }
} }
#[must_use]
pub fn board(&self) -> &Board {
&self.board
}
} }
impl Position { impl Position {

View file

@ -8,7 +8,7 @@
use chessfriend_core::Color; use chessfriend_core::Color;
use chessfriend_moves::{ use chessfriend_moves::{
assert_move_list, assert_move_list_contains, assert_move_list_does_not_contain, ply, Move, Move, assert_move_list, assert_move_list_contains, assert_move_list_does_not_contain, ply,
}; };
use chessfriend_position::test_position; use chessfriend_position::test_position;
use std::collections::HashSet; use std::collections::HashSet;
@ -107,7 +107,7 @@ fn en_passant_check_capture() {
White Pawn on D4, White Pawn on D4,
], D3); ], D3);
assert!(pos.board.active_color_is_in_check()); assert!(pos.board().active_color_is_in_check());
let generated_moves: HashSet<_> = pos.all_legal_moves(Some(Color::Black)).collect(); let generated_moves: HashSet<_> = pos.all_legal_moves(Some(Color::Black)).collect();
@ -123,7 +123,7 @@ fn en_passant_check_block() {
White Queen on F1, White Queen on F1,
], D3); ], D3);
assert!(pos.board.active_color_is_in_check()); assert!(pos.board().active_color_is_in_check());
let generated_moves: HashSet<_> = pos.all_legal_moves(Some(Color::Black)).collect(); let generated_moves: HashSet<_> = pos.all_legal_moves(Some(Color::Black)).collect();
@ -139,7 +139,7 @@ fn pinned_pieces_rook_cannot_move_out_of_pin() {
White King on C1, White King on C1,
]); ]);
assert!(!pos.board.active_color_is_in_check()); assert!(!pos.board().active_color_is_in_check());
let rook_moves: HashSet<_> = pos.all_legal_moves(None).collect(); let rook_moves: HashSet<_> = pos.all_legal_moves(None).collect();