[board] Replace active player and move properties on Board with MoveCounter instance
This commit is contained in:
parent
58cbe07136
commit
cd60a453aa
3 changed files with 24 additions and 49 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
// Eryn Wells <eryn@erynwells.me>
|
// Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
display::DiagramFormatter, piece_sets::PlacePieceError, Castle, EnPassant, Flags, PieceSet,
|
display::DiagramFormatter, piece_sets::PlacePieceError, Castle, EnPassant, Flags, MoveCounter,
|
||||||
|
PieceSet,
|
||||||
};
|
};
|
||||||
use chessfriend_bitboard::BitBoard;
|
use chessfriend_bitboard::BitBoard;
|
||||||
use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square};
|
use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square};
|
||||||
|
|
@ -9,12 +10,10 @@ use std::iter::Iterator;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq)]
|
#[derive(Clone, Debug, Eq)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
player_to_move: Color,
|
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
pieces: PieceSet,
|
pieces: PieceSet,
|
||||||
en_passant: Option<EnPassant>,
|
en_passant: Option<EnPassant>,
|
||||||
half_move_counter: u16,
|
pub move_counter: MoveCounter,
|
||||||
full_move_number: u16,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
|
|
@ -46,7 +45,6 @@ impl Board {
|
||||||
];
|
];
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
player_to_move: Color::White,
|
|
||||||
pieces: PieceSet::new([WHITE_PIECES, BLACK_PIECES]),
|
pieces: PieceSet::new([WHITE_PIECES, BLACK_PIECES]),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
|
|
@ -72,22 +70,7 @@ impl Board {
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn player_to_move(&self) -> Color {
|
pub fn player_to_move(&self) -> Color {
|
||||||
self.player_to_move
|
self.move_counter.active_color
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn move_number(&self) -> u16 {
|
|
||||||
self.full_move_number
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn ply_counter(&self) -> u16 {
|
|
||||||
self.half_move_counter
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn flags(&self) -> &Flags {
|
|
||||||
&self.flags
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the player has the right to castle on the given side
|
/// Returns `true` if the player has the right to castle on the given side
|
||||||
|
|
@ -199,12 +182,10 @@ impl Board {
|
||||||
impl Default for Board {
|
impl Default for Board {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
player_to_move: Color::default(),
|
|
||||||
flags: Flags::default(),
|
flags: Flags::default(),
|
||||||
pieces: PieceSet::default(),
|
pieces: PieceSet::default(),
|
||||||
en_passant: None,
|
en_passant: None,
|
||||||
half_move_counter: 0,
|
move_counter: MoveCounter::default(),
|
||||||
full_move_number: 1,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -212,9 +193,9 @@ impl Default for Board {
|
||||||
impl PartialEq for Board {
|
impl PartialEq for Board {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.pieces == other.pieces
|
self.pieces == other.pieces
|
||||||
&& self.player_to_move == other.player_to_move
|
|
||||||
&& self.flags == other.flags
|
&& self.flags == other.flags
|
||||||
&& self.en_passant == other.en_passant
|
&& self.en_passant == other.en_passant
|
||||||
|
&& self.move_counter == other.move_counter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,15 @@
|
||||||
// Eryn Wells <eryn@erynwells.me>
|
// Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
use crate::{piece_sets::Mailbox, Board, Castle, EnPassant, Flags, PieceSet};
|
use crate::{piece_sets::Mailbox, Board, Castle, EnPassant, Flags, MoveCounter, PieceSet};
|
||||||
use chessfriend_core::{piece, Color, PlacedPiece, Rank, Shape, Square};
|
use chessfriend_core::{piece, Color, PlacedPiece, Rank, Shape, Square};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Builder {
|
pub struct Builder {
|
||||||
player_to_move: Color,
|
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
pieces: Mailbox,
|
pieces: Mailbox,
|
||||||
kings: [Option<Square>; Color::NUM],
|
kings: [Option<Square>; Color::NUM],
|
||||||
en_passant: Option<EnPassant>,
|
en_passant: Option<EnPassant>,
|
||||||
ply_counter: u16,
|
move_counter: MoveCounter,
|
||||||
move_number: u16,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Builder {
|
impl Builder {
|
||||||
|
|
@ -28,28 +26,26 @@ impl Builder {
|
||||||
let black_king = board.king_square(Color::Black);
|
let black_king = board.king_square(Color::Black);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
player_to_move: board.player_to_move(),
|
|
||||||
flags: *board.flags(),
|
flags: *board.flags(),
|
||||||
pieces,
|
pieces,
|
||||||
kings: [Some(white_king), Some(black_king)],
|
kings: [Some(white_king), Some(black_king)],
|
||||||
en_passant: board.en_passant(),
|
en_passant: board.en_passant(),
|
||||||
ply_counter: board.ply_counter(),
|
move_counter: board.move_counter,
|
||||||
move_number: board.move_number(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_move(&mut self, player: Color) -> &mut Self {
|
pub fn to_move(&mut self, player: Color) -> &mut Self {
|
||||||
self.player_to_move = player;
|
self.move_counter.active_color = player;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ply_counter(&mut self, num: u16) -> &mut Self {
|
pub fn fullmove_number(&mut self, num: u16) -> &mut Self {
|
||||||
self.ply_counter = num;
|
self.move_counter.fullmove_number = num;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_number(&mut self, num: u16) -> &mut Self {
|
pub fn halfmove_number(&mut self, num: u16) -> &mut Self {
|
||||||
self.move_number = num;
|
self.move_counter.halfmove_number = num;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,14 +110,12 @@ impl Builder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Board::new(
|
Board {
|
||||||
self.player_to_move,
|
|
||||||
flags,
|
flags,
|
||||||
pieces,
|
pieces,
|
||||||
self.en_passant,
|
self.en_passant,
|
||||||
self.ply_counter,
|
move_counter: self.move_counter,
|
||||||
self.move_number,
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -149,13 +143,11 @@ impl Default for Builder {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
player_to_move: Color::White,
|
|
||||||
flags: Flags::default(),
|
flags: Flags::default(),
|
||||||
pieces,
|
pieces,
|
||||||
kings: [Some(white_king_square), Some(black_king_square)],
|
kings: [Some(white_king_square), Some(black_king_square)],
|
||||||
en_passant: None,
|
en_passant: None,
|
||||||
ply_counter: 0,
|
move_counter: MoveCounter::default(),
|
||||||
move_number: 1,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -132,8 +132,10 @@ impl ToFenStr for Board {
|
||||||
)
|
)
|
||||||
.map_err(ToFenStrError::FmtError)?;
|
.map_err(ToFenStrError::FmtError)?;
|
||||||
|
|
||||||
write!(fen_string, " {}", self.ply_counter()).map_err(ToFenStrError::FmtError)?;
|
write!(fen_string, " {}", self.move_counter.halfmove_number)
|
||||||
write!(fen_string, " {}", self.move_number()).map_err(ToFenStrError::FmtError)?;
|
.map_err(ToFenStrError::FmtError)?;
|
||||||
|
write!(fen_string, " {}", self.move_counter.fullmove_number)
|
||||||
|
.map_err(ToFenStrError::FmtError)?;
|
||||||
|
|
||||||
Ok(fen_string)
|
Ok(fen_string)
|
||||||
}
|
}
|
||||||
|
|
@ -246,7 +248,7 @@ impl FromFenStr for Board {
|
||||||
let half_move_clock: u16 = half_move_clock
|
let half_move_clock: u16 = half_move_clock
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(FromFenStrError::ParseIntError)?;
|
.map_err(FromFenStrError::ParseIntError)?;
|
||||||
builder.ply_counter(half_move_clock);
|
builder.halfmove_number(half_move_clock);
|
||||||
|
|
||||||
let full_move_counter = fields
|
let full_move_counter = fields
|
||||||
.next()
|
.next()
|
||||||
|
|
@ -254,7 +256,7 @@ impl FromFenStr for Board {
|
||||||
let full_move_counter: u16 = full_move_counter
|
let full_move_counter: u16 = full_move_counter
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(FromFenStrError::ParseIntError)?;
|
.map_err(FromFenStrError::ParseIntError)?;
|
||||||
builder.move_number(full_move_counter);
|
builder.fullmove_number(full_move_counter);
|
||||||
|
|
||||||
debug_assert_eq!(fields.next(), None);
|
debug_assert_eq!(fields.next(), None);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue