In Position::place_piece, take piece and square by value
Use values for BitBoards and other things too.
This commit is contained in:
parent
657a501cb5
commit
aac1a85507
1 changed files with 20 additions and 22 deletions
|
|
@ -1,10 +1,12 @@
|
||||||
// Eryn Wells <eryn@erynwells.me>
|
// Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
use super::Pieces;
|
use super::Pieces;
|
||||||
use crate::bitboard::BitBoard;
|
use crate::{
|
||||||
use crate::moves::Moves;
|
bitboard::BitBoard,
|
||||||
use crate::piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape};
|
moves::Moves,
|
||||||
use crate::Square;
|
piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape},
|
||||||
|
Square,
|
||||||
|
};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
|
@ -68,28 +70,24 @@ impl Position {
|
||||||
Position {
|
Position {
|
||||||
color_to_move: Color::White,
|
color_to_move: Color::White,
|
||||||
pieces_per_color: [
|
pieces_per_color: [
|
||||||
white_pieces.iter().fold(BitBoard::empty(), |a, b| a | b),
|
white_pieces.iter().fold(BitBoard::empty(), |a, b| a | *b),
|
||||||
black_pieces.iter().fold(BitBoard::empty(), |a, b| a | b),
|
black_pieces.iter().fold(BitBoard::empty(), |a, b| a | *b),
|
||||||
],
|
],
|
||||||
pieces_per_type: [white_pieces, black_pieces],
|
pieces_per_type: [white_pieces, black_pieces],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn place_piece(
|
pub fn place_piece(&mut self, piece: Piece, square: Square) -> Result<(), PiecePlacementError> {
|
||||||
&mut self,
|
|
||||||
piece: &Piece,
|
|
||||||
square: &Square,
|
|
||||||
) -> Result<(), PiecePlacementError> {
|
|
||||||
let type_bb = self.bitboard_for_piece_mut(piece);
|
let type_bb = self.bitboard_for_piece_mut(piece);
|
||||||
|
|
||||||
if type_bb.has_piece_at(&square) {
|
if type_bb.has_piece_at(square) {
|
||||||
return Err(PiecePlacementError::ExistsOnSquare);
|
return Err(PiecePlacementError::ExistsOnSquare);
|
||||||
}
|
}
|
||||||
|
|
||||||
type_bb.place_piece_at(&square);
|
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);
|
color_bb.place_piece_at(square);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -110,16 +108,16 @@ impl Position {
|
||||||
| self.pieces_per_color[Color::Black as usize])
|
| self.pieces_per_color[Color::Black as usize])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn bitboard_for_piece(&self, piece: Piece) -> &BitBoard {
|
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 {
|
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 {
|
pub(crate) fn bitboard_for_color(&self, color: Color) -> BitBoard {
|
||||||
&self.pieces_per_color[color as usize]
|
self.pieces_per_color[color as usize]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bitboard_for_color_mut(&mut self, color: Color) -> &mut BitBoard {
|
fn bitboard_for_color_mut(&mut self, color: Color) -> &mut BitBoard {
|
||||||
|
|
@ -131,7 +129,7 @@ impl Position {
|
||||||
for shape in Shape::iter() {
|
for shape in Shape::iter() {
|
||||||
let piece = Piece::new(color, *shape);
|
let piece = Piece::new(color, *shape);
|
||||||
let bb = self.bitboard_for_piece(piece);
|
let bb = self.bitboard_for_piece(piece);
|
||||||
if bb.has_piece_at(&sq) {
|
if bb.has_piece_at(sq) {
|
||||||
return Some(PlacedPiece::new(piece, sq));
|
return Some(PlacedPiece::new(piece, sq));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +183,7 @@ mod tests {
|
||||||
let square = Square::e4();
|
let square = Square::e4();
|
||||||
|
|
||||||
position
|
position
|
||||||
.place_piece(&piece, &square)
|
.place_piece(piece, square)
|
||||||
.expect("Unable to place white queen on e4");
|
.expect("Unable to place white queen on e4");
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
@ -198,7 +196,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
position
|
position
|
||||||
.place_piece(&piece, &square)
|
.place_piece(piece, square)
|
||||||
.expect_err("Placed white queen on e4 a second time?!");
|
.expect_err("Placed white queen on e4 a second time?!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue