Use turn counts in 'ChessBoardBuilder'
This makes more sense from a user's perspective.
This commit is contained in:
parent
ef3a1e4695
commit
f4764f2174
|
@ -9,8 +9,9 @@ pub struct ChessBoardBuilder {
|
||||||
castle_rights: [CastleRights; Color::NUM_VARIANTS],
|
castle_rights: [CastleRights; Color::NUM_VARIANTS],
|
||||||
en_passant: Option<Square>,
|
en_passant: Option<Square>,
|
||||||
half_move_clock: u8,
|
half_move_clock: u8,
|
||||||
total_plies: u32,
|
|
||||||
side: Color,
|
side: Color,
|
||||||
|
// 1-based, a turn is *two* half-moves (i.e: both players have played).
|
||||||
|
turn_count: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChessBoardBuilder {
|
impl ChessBoardBuilder {
|
||||||
|
@ -20,8 +21,8 @@ impl ChessBoardBuilder {
|
||||||
castle_rights: [CastleRights::NoSide; 2],
|
castle_rights: [CastleRights::NoSide; 2],
|
||||||
en_passant: Default::default(),
|
en_passant: Default::default(),
|
||||||
half_move_clock: Default::default(),
|
half_move_clock: Default::default(),
|
||||||
total_plies: Default::default(),
|
|
||||||
side: Color::White,
|
side: Color::White,
|
||||||
|
turn_count: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,8 +46,8 @@ impl ChessBoardBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_total_plies(&mut self, plies: u32) -> &mut Self {
|
pub fn with_turn_count(&mut self, count: u32) -> &mut Self {
|
||||||
self.total_plies = plies;
|
self.turn_count = count;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,8 +91,8 @@ impl TryFrom<ChessBoardBuilder> for ChessBoard {
|
||||||
castle_rights,
|
castle_rights,
|
||||||
en_passant,
|
en_passant,
|
||||||
half_move_clock,
|
half_move_clock,
|
||||||
total_plies,
|
|
||||||
side,
|
side,
|
||||||
|
turn_count,
|
||||||
} = builder;
|
} = builder;
|
||||||
|
|
||||||
for square in Square::iter() {
|
for square in Square::iter() {
|
||||||
|
@ -103,6 +104,8 @@ impl TryFrom<ChessBoardBuilder> for ChessBoard {
|
||||||
combined_occupancy |= square;
|
combined_occupancy |= square;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let total_plies = (turn_count - 1) * 2 + if side == Color::White { 0 } else { 1 };
|
||||||
|
|
||||||
let board = ChessBoard {
|
let board = ChessBoard {
|
||||||
piece_occupancy,
|
piece_occupancy,
|
||||||
color_occupancy,
|
color_occupancy,
|
||||||
|
@ -146,7 +149,7 @@ mod test {
|
||||||
|
|
||||||
builder
|
builder
|
||||||
.with_half_move_clock(board.half_move_clock())
|
.with_half_move_clock(board.half_move_clock())
|
||||||
.with_total_plies(board.total_plies())
|
.with_turn_count(board.total_plies() / 2 + 1)
|
||||||
.with_current_player(board.current_player());
|
.with_current_player(board.current_player());
|
||||||
|
|
||||||
builder
|
builder
|
||||||
|
|
|
@ -154,9 +154,7 @@ impl FromFen for ChessBoard {
|
||||||
let full_move_counter = full_move_counter
|
let full_move_counter = full_move_counter
|
||||||
.parse::<u32>()
|
.parse::<u32>()
|
||||||
.map_err(|_| FenError::InvalidFen)?;
|
.map_err(|_| FenError::InvalidFen)?;
|
||||||
builder.with_total_plies(
|
builder.with_turn_count(full_move_counter);
|
||||||
(full_move_counter - 1) * 2 + if side == Color::White { 0 } else { 1 },
|
|
||||||
);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut rank: usize = 8;
|
let mut rank: usize = 8;
|
||||||
|
|
Loading…
Reference in a new issue