[board] Clean up interfaces of pieces and square structs
This commit is contained in:
parent
301fe1a4f4
commit
41421dddbb
5 changed files with 57 additions and 31 deletions
|
@ -26,15 +26,15 @@ impl BitBoard {
|
|||
}
|
||||
|
||||
pub fn has_piece_at(&self, sq: &Square) -> bool {
|
||||
(self.0 & (1 << sq.index)) > 0
|
||||
(self.0 & (1 << sq.index())) > 0
|
||||
}
|
||||
|
||||
pub fn place_piece_at(&mut self, sq: &Square) {
|
||||
self.0 |= 1 << sq.index
|
||||
self.0 |= 1 << sq.index()
|
||||
}
|
||||
|
||||
fn remove_piece_at(&mut self, sq: &Square) {
|
||||
self.0 &= !(1 << sq.index)
|
||||
self.0 &= !(1 << sq.index())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -111,8 +111,8 @@ pub enum PiecePlacementError {
|
|||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
pub struct Piece {
|
||||
pub color: Color,
|
||||
pub shape: Shape,
|
||||
color: Color,
|
||||
shape: Shape,
|
||||
}
|
||||
|
||||
macro_rules! piece_constructor {
|
||||
|
@ -137,6 +137,14 @@ impl Piece {
|
|||
piece_constructor!(rook, Rook);
|
||||
piece_constructor!(queen, Queen);
|
||||
piece_constructor!(king, King);
|
||||
|
||||
pub fn color(&self) -> Color {
|
||||
self.color
|
||||
}
|
||||
|
||||
pub fn shape(&self) -> Shape {
|
||||
self.shape
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Piece {
|
||||
|
@ -147,14 +155,22 @@ impl fmt::Display for Piece {
|
|||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
pub struct PlacedPiece {
|
||||
pub piece: Piece,
|
||||
pub square: Square,
|
||||
piece: Piece,
|
||||
square: Square,
|
||||
}
|
||||
|
||||
impl PlacedPiece {
|
||||
pub fn new(piece: Piece, square: Square) -> PlacedPiece {
|
||||
PlacedPiece { piece, square }
|
||||
}
|
||||
|
||||
pub fn piece(&self) -> Piece {
|
||||
self.piece
|
||||
}
|
||||
|
||||
pub fn square(&self) -> Square {
|
||||
self.square
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -24,7 +24,7 @@ impl<'a> fmt::Display for DiagramFormatter<'a> {
|
|||
for file in 0..8 {
|
||||
let square = Square::from_rank_file(rank, file).unwrap();
|
||||
match self.0.piece_on_square(square) {
|
||||
Some(placed_piece) => write!(output, "{} ", placed_piece.piece)?,
|
||||
Some(placed_piece) => write!(output, "{} ", placed_piece.piece())?,
|
||||
None => output.push_str(". "),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -88,7 +88,7 @@ impl Position {
|
|||
|
||||
type_bb.place_piece_at(&square);
|
||||
|
||||
let color_bb = &mut self.bitboard_for_color_mut(piece.color);
|
||||
let color_bb = &mut self.bitboard_for_color_mut(piece.color());
|
||||
color_bb.place_piece_at(&square);
|
||||
|
||||
Ok(())
|
||||
|
@ -110,11 +110,11 @@ impl Position {
|
|||
}
|
||||
|
||||
pub(crate) fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
||||
&self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
||||
&self.pieces_per_type[piece.color() as usize][piece.shape() as usize]
|
||||
}
|
||||
|
||||
fn bitboard_for_piece_mut(&mut self, piece: &Piece) -> &mut BitBoard {
|
||||
&mut self.pieces_per_type[piece.color as usize][piece.shape as usize]
|
||||
&mut self.pieces_per_type[piece.color() as usize][piece.shape() as usize]
|
||||
}
|
||||
|
||||
pub(crate) fn bitboard_for_color(&self, color: Color) -> &BitBoard {
|
||||
|
@ -188,7 +188,7 @@ mod tests {
|
|||
.expect("Unable to place white queen on e4");
|
||||
|
||||
assert_eq!(
|
||||
position.bitboard_for_color(piece.color).clone(),
|
||||
position.bitboard_for_color(piece.color()).clone(),
|
||||
BitBoard::new(1 << 28)
|
||||
);
|
||||
assert_eq!(
|
||||
|
|
|
@ -21,28 +21,12 @@ pub struct SquareOutOfBoundsError;
|
|||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
pub struct Square {
|
||||
pub rank: u8,
|
||||
pub file: u8,
|
||||
pub index: u8,
|
||||
rank: u8,
|
||||
file: u8,
|
||||
index: u8,
|
||||
}
|
||||
|
||||
impl Square {
|
||||
pub fn from_index(index: u8) -> Result<Square, SquareOutOfBoundsError> {
|
||||
if index >= 64 {
|
||||
return Err(SquareOutOfBoundsError);
|
||||
}
|
||||
|
||||
Ok(Square::from_index_unsafe(index))
|
||||
}
|
||||
|
||||
pub(crate) fn from_index_unsafe(index: u8) -> Square {
|
||||
Square {
|
||||
rank: index / 8,
|
||||
file: index % 8,
|
||||
index: index,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_rank_file(rank: u8, file: u8) -> Result<Square, SquareOutOfBoundsError> {
|
||||
if rank >= 8 || file >= 8 {
|
||||
return Err(SquareOutOfBoundsError);
|
||||
|
@ -59,6 +43,10 @@ impl Square {
|
|||
s.parse()
|
||||
}
|
||||
|
||||
pub fn rank_file(&self) -> (u8, u8) {
|
||||
(self.rank, self.file)
|
||||
}
|
||||
|
||||
pub fn neighbor(&self, direction: Direction) -> Option<Square> {
|
||||
match direction {
|
||||
Direction::North => Square::from_index(self.index + 8),
|
||||
|
@ -110,6 +98,28 @@ impl Square {
|
|||
}
|
||||
}
|
||||
|
||||
impl Square {
|
||||
pub(crate) fn from_index(index: u8) -> Result<Square, SquareOutOfBoundsError> {
|
||||
if index >= 64 {
|
||||
return Err(SquareOutOfBoundsError);
|
||||
}
|
||||
|
||||
Ok(Square::from_index_unsafe(index))
|
||||
}
|
||||
|
||||
pub(crate) fn from_index_unsafe(index: u8) -> Square {
|
||||
Square {
|
||||
rank: index / 8,
|
||||
file: index % 8,
|
||||
index: index,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn index(&self) -> u8 {
|
||||
self.index
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Square {
|
||||
type Err = ParseSquareError;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue