Move Castle to a castle module inside the move module. Implement into_index() on Castle to turn it into a usize.
85 lines
3 KiB
Rust
85 lines
3 KiB
Rust
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
use crate::{r#move::Castle, Color};
|
|
use std::fmt;
|
|
|
|
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
|
|
pub(super) struct Flags(u8);
|
|
|
|
impl Flags {
|
|
#[inline]
|
|
pub(super) fn player_has_right_to_castle_flag_offset(color: Color, castle: Castle) -> usize {
|
|
((color as usize) << 1) + castle.into_index()
|
|
}
|
|
|
|
pub(super) fn player_has_right_to_castle(&self, color: Color, castle: Castle) -> bool {
|
|
(self.0 & (1 << Self::player_has_right_to_castle_flag_offset(color, castle))) != 0
|
|
}
|
|
|
|
pub(super) fn set_player_has_right_to_castle_flag(&mut self, color: Color, castle: Castle) {
|
|
self.0 |= 1 << Self::player_has_right_to_castle_flag_offset(color, castle);
|
|
}
|
|
|
|
pub(super) fn clear_player_has_right_to_castle_flag(&mut self, color: Color, castle: Castle) {
|
|
self.0 &= !(1 << Self::player_has_right_to_castle_flag_offset(color, castle));
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Flags {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "Flags({:08b})", self.0)
|
|
}
|
|
}
|
|
|
|
impl Default for Flags {
|
|
fn default() -> Self {
|
|
Flags(0b00001111)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{r#move::Castle, Color};
|
|
|
|
#[test]
|
|
fn castle_flags() {
|
|
assert_eq!(
|
|
Flags::player_has_right_to_castle_flag_offset(Color::White, Castle::KingSide),
|
|
0
|
|
);
|
|
assert_eq!(
|
|
Flags::player_has_right_to_castle_flag_offset(Color::White, Castle::QueenSide),
|
|
1
|
|
);
|
|
assert_eq!(
|
|
Flags::player_has_right_to_castle_flag_offset(Color::Black, Castle::KingSide),
|
|
2
|
|
);
|
|
assert_eq!(
|
|
Flags::player_has_right_to_castle_flag_offset(Color::Black, Castle::QueenSide),
|
|
3
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn defaults() {
|
|
let mut flags: Flags = Default::default();
|
|
assert!(flags.player_has_right_to_castle(Color::White, Castle::KingSide));
|
|
assert!(flags.player_has_right_to_castle(Color::White, Castle::QueenSide));
|
|
assert!(flags.player_has_right_to_castle(Color::Black, Castle::KingSide));
|
|
assert!(flags.player_has_right_to_castle(Color::Black, Castle::QueenSide));
|
|
|
|
flags.clear_player_has_right_to_castle_flag(Color::White, Castle::QueenSide);
|
|
assert!(flags.player_has_right_to_castle(Color::White, Castle::KingSide));
|
|
assert!(!flags.player_has_right_to_castle(Color::White, Castle::QueenSide));
|
|
assert!(flags.player_has_right_to_castle(Color::Black, Castle::KingSide));
|
|
assert!(flags.player_has_right_to_castle(Color::Black, Castle::QueenSide));
|
|
|
|
flags.set_player_has_right_to_castle_flag(Color::White, Castle::QueenSide);
|
|
assert!(flags.player_has_right_to_castle(Color::White, Castle::KingSide));
|
|
assert!(flags.player_has_right_to_castle(Color::White, Castle::QueenSide));
|
|
assert!(flags.player_has_right_to_castle(Color::Black, Castle::KingSide));
|
|
assert!(flags.player_has_right_to_castle(Color::Black, Castle::QueenSide));
|
|
}
|
|
}
|