[board] Implement a rather dumb Position::piece_on_square

This method does an iteration over all piece colors and shapes to find the piece on a given square.
This commit is contained in:
Eryn Wells 2023-12-26 21:34:01 -07:00
parent 49c5a0b51d
commit 17511e9d63

View file

@ -2,7 +2,7 @@
use super::Pieces;
use crate::bitboard::BitBoard;
use crate::piece::{Color, Piece, PiecePlacementError};
use crate::piece::{Color, Piece, PiecePlacementError, PlacedPiece, Shape};
use crate::Square;
use std::fmt;
use std::fmt::Write;
@ -98,6 +98,20 @@ impl Position {
&mut self.pieces_per_type[piece.color as usize][piece.shape as usize]
}
pub fn piece_on_square(&self, sq: Square) -> Option<PlacedPiece> {
for color in Color::iter() {
for shape in Shape::iter() {
let piece = Piece::new(color, shape);
let bb = self.bitboard_for_piece(&piece);
if bb.has_piece_at(&sq) {
return Some(PlacedPiece::new(piece, sq));
}
}
}
None
}
pub fn pieces(&self, color: Color) -> Pieces {
Pieces::new(&self, color)
}