use crate::board::{PieceMapping, CapturedBits, Occupied};
pub struct PromotionLogic;
impl PromotionLogic {
pub fn promote_pawn(
mapping: &mut PieceMapping,
occupied: &mut Occupied,
captured_bits: &mut CapturedBits,
pawn_pid: u8,
color: u8,
dest: u8,
new_kind: u8, ) -> u8 {
mapping.remove_piece(pawn_pid);
*occupied &= !(1u64 << (dest as u64));
*captured_bits |= 1u32 << (pawn_pid as u32);
let new_pid = match (color, new_kind) {
(0, 0) => 14, (0, 1) => 12, (0, 2) => 10, (0, 3) => 8, (1, 0) => 30, (1, 1) => 28, (1, 2) => 26, (1, 3) => 24, _ => panic!("Invalid color ({}) or kind ({})", color, new_kind),
};
mapping.place_piece(new_pid, dest);
*occupied |= 1u64 << (dest as u64);
new_pid
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::board::encode_square;
#[test]
fn promotion_removes_pawn_and_places_queen() {
let mut mapping = PieceMapping::new_empty();
let mut occupied: u64 = 0;
let mut captured_bits: u32 = 0;
let pawn_pid = 4;
mapping.place_piece(pawn_pid, encode_square(6, 4)); occupied |= 1u64 << 52;
let new_pid = PromotionLogic::promote_pawn(
&mut mapping,
&mut occupied,
&mut captured_bits,
pawn_pid,
0, 60, 0, );
assert_eq!(new_pid, 14); assert_eq!(mapping.piece_square[pawn_pid as usize], None);
assert_eq!(mapping.piece_square[new_pid as usize], Some(60));
assert_eq!((occupied >> 60) & 1u64, 1u64);
assert_eq!(
captured_bits & (1u32 << (pawn_pid as u32)),
1u32 << (pawn_pid as u32)
);
}
}