[board] Implement ply and full move counters on Position
Pipe these numbers through the Builders
This commit is contained in:
parent
829d9af52c
commit
dbbf2d4c46
3 changed files with 63 additions and 2 deletions
|
@ -28,6 +28,7 @@ pub enum ValidatedMove {
|
||||||
promotion: Option<Shape>,
|
promotion: Option<Shape>,
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
en_passant_square: Option<Square>,
|
en_passant_square: Option<Square>,
|
||||||
|
increment_ply: bool,
|
||||||
},
|
},
|
||||||
Castle {
|
Castle {
|
||||||
castle: Castle,
|
castle: Castle,
|
||||||
|
@ -140,6 +141,7 @@ where
|
||||||
promotion: mv.promotion(),
|
promotion: mv.promotion(),
|
||||||
flags,
|
flags,
|
||||||
en_passant_square,
|
en_passant_square,
|
||||||
|
increment_ply: !(mv.is_capture() || piece.is_pawn()),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -150,6 +152,9 @@ impl<'p> Builder<'p, ValidatedMove> {
|
||||||
pub fn build(&self) -> Position {
|
pub fn build(&self) -> Position {
|
||||||
let player = self.position.player_to_move();
|
let player = self.position.player_to_move();
|
||||||
|
|
||||||
|
let updated_move_number =
|
||||||
|
self.position.move_number() + if player == Color::Black { 1 } else { 0 };
|
||||||
|
|
||||||
match self.move_to_make {
|
match self.move_to_make {
|
||||||
ValidatedMove::RegularMove {
|
ValidatedMove::RegularMove {
|
||||||
from_square,
|
from_square,
|
||||||
|
@ -159,6 +164,7 @@ impl<'p> Builder<'p, ValidatedMove> {
|
||||||
promotion,
|
promotion,
|
||||||
flags,
|
flags,
|
||||||
en_passant_square,
|
en_passant_square,
|
||||||
|
increment_ply,
|
||||||
} => {
|
} => {
|
||||||
let mut pieces = self.position.piece_bitboards().clone();
|
let mut pieces = self.position.piece_bitboards().clone();
|
||||||
|
|
||||||
|
@ -174,11 +180,19 @@ impl<'p> Builder<'p, ValidatedMove> {
|
||||||
pieces.move_piece(moving_piece.piece(), from_square, to_square);
|
pieces.move_piece(moving_piece.piece(), from_square, to_square);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let ply = if increment_ply {
|
||||||
|
self.position.ply_counter() + 1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
Position::new(
|
Position::new(
|
||||||
self.position.player_to_move().other(),
|
self.position.player_to_move().other(),
|
||||||
flags,
|
flags,
|
||||||
pieces,
|
pieces,
|
||||||
en_passant_square,
|
en_passant_square,
|
||||||
|
ply,
|
||||||
|
updated_move_number,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ValidatedMove::Castle {
|
ValidatedMove::Castle {
|
||||||
|
@ -202,7 +216,14 @@ impl<'p> Builder<'p, ValidatedMove> {
|
||||||
*pieces.bitboard_for_color_mut(player) &=
|
*pieces.bitboard_for_color_mut(player) &=
|
||||||
!(king_from | rook_from) | (king_to | rook_to);
|
!(king_from | rook_from) | (king_to | rook_to);
|
||||||
|
|
||||||
Position::new(player.other(), flags, pieces, None)
|
Position::new(
|
||||||
|
player.other(),
|
||||||
|
flags,
|
||||||
|
pieces,
|
||||||
|
None,
|
||||||
|
self.position.ply_counter() + 1,
|
||||||
|
updated_move_number,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ pub struct Builder {
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
pieces: BTreeMap<Square, Piece>,
|
pieces: BTreeMap<Square, Piece>,
|
||||||
kings: [Square; 2],
|
kings: [Square; 2],
|
||||||
|
ply_counter: u16,
|
||||||
|
move_number: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Builder {
|
impl Builder {
|
||||||
|
@ -28,6 +30,16 @@ impl Builder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ply_counter(&mut self, num: u16) -> &mut Self {
|
||||||
|
self.ply_counter = num;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn move_number(&mut self, num: u16) -> &mut Self {
|
||||||
|
self.move_number = num;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn place_piece(&mut self, piece: PlacedPiece) -> &mut Self {
|
pub fn place_piece(&mut self, piece: PlacedPiece) -> &mut Self {
|
||||||
let square = piece.square();
|
let square = piece.square();
|
||||||
|
|
||||||
|
@ -50,7 +62,14 @@ impl Builder {
|
||||||
.filter(Self::is_piece_placement_valid),
|
.filter(Self::is_piece_placement_valid),
|
||||||
);
|
);
|
||||||
|
|
||||||
Position::new(self.player_to_move, self.flags, pieces, None)
|
Position::new(
|
||||||
|
self.player_to_move,
|
||||||
|
self.flags,
|
||||||
|
pieces,
|
||||||
|
None,
|
||||||
|
self.ply_counter,
|
||||||
|
self.move_number,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,6 +101,8 @@ impl Default for Builder {
|
||||||
flags: Flags::default(),
|
flags: Flags::default(),
|
||||||
pieces: pieces,
|
pieces: pieces,
|
||||||
kings: [white_king_square, black_king_square],
|
kings: [white_king_square, black_king_square],
|
||||||
|
ply_counter: 0,
|
||||||
|
move_number: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,9 @@ pub struct Position {
|
||||||
pieces: PieceBitBoards,
|
pieces: PieceBitBoards,
|
||||||
en_passant_square: Option<Square>,
|
en_passant_square: Option<Square>,
|
||||||
sight: [OnceCell<BitBoard>; 2],
|
sight: [OnceCell<BitBoard>; 2],
|
||||||
|
|
||||||
|
half_move_counter: u16,
|
||||||
|
full_move_number: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Position {
|
impl Position {
|
||||||
|
@ -28,6 +31,8 @@ impl Position {
|
||||||
pieces: PieceBitBoards::default(),
|
pieces: PieceBitBoards::default(),
|
||||||
en_passant_square: None,
|
en_passant_square: None,
|
||||||
sight: [OnceCell::new(), OnceCell::new()],
|
sight: [OnceCell::new(), OnceCell::new()],
|
||||||
|
half_move_counter: 0,
|
||||||
|
full_move_number: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +62,8 @@ impl Position {
|
||||||
pieces: PieceBitBoards::new([white_pieces, black_pieces]),
|
pieces: PieceBitBoards::new([white_pieces, black_pieces]),
|
||||||
en_passant_square: None,
|
en_passant_square: None,
|
||||||
sight: [OnceCell::new(), OnceCell::new()],
|
sight: [OnceCell::new(), OnceCell::new()],
|
||||||
|
half_move_counter: 0,
|
||||||
|
full_move_number: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,6 +71,14 @@ impl Position {
|
||||||
self.color_to_move
|
self.color_to_move
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn move_number(&self) -> u16 {
|
||||||
|
self.full_move_number
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ply_counter(&self) -> u16 {
|
||||||
|
self.half_move_counter
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns true if the player has the right to castle on the given side of
|
/// Returns true if the player has the right to castle on the given side of
|
||||||
/// the board.
|
/// the board.
|
||||||
///
|
///
|
||||||
|
@ -191,6 +206,8 @@ impl Position {
|
||||||
flags: Flags,
|
flags: Flags,
|
||||||
pieces: PieceBitBoards,
|
pieces: PieceBitBoards,
|
||||||
en_passant_square: Option<Square>,
|
en_passant_square: Option<Square>,
|
||||||
|
half_move_counter: u16,
|
||||||
|
full_move_number: u16,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
color_to_move: player_to_move,
|
color_to_move: player_to_move,
|
||||||
|
@ -198,6 +215,8 @@ impl Position {
|
||||||
en_passant_square,
|
en_passant_square,
|
||||||
pieces,
|
pieces,
|
||||||
sight: [OnceCell::new(), OnceCell::new()],
|
sight: [OnceCell::new(), OnceCell::new()],
|
||||||
|
half_move_counter: 0,
|
||||||
|
full_move_number: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue