diff --git a/src/moov.rs b/src/moov.rs index 4a22fec..b79a55d 100644 --- a/src/moov.rs +++ b/src/moov.rs @@ -1,39 +1,44 @@ -use std::fmt; - use super::piece::*; use super::square::*; use super::types::*; +use std::fmt; +use std::num::NonZeroU16; -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)] -pub struct Move(MoveInt); +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct Move(NonZeroU16); impl Move { pub fn new(from_sq: SQ, to_sq: SQ, flags: MoveFlags) -> Self { - Self(((flags as MoveInt) << 12) | ((from_sq as MoveInt) << 6) | (to_sq as MoveInt)) + Self( + NonZeroU16::new( + (flags as MoveInt) << 12 | (from_sq as MoveInt) << 6 | (to_sq as MoveInt), + ) + .expect("MoveInt is zero."), + ) } pub fn to_sq(&self) -> SQ { - SQ::from((self.0 & 0x3f) as u8) + SQ::from((self.0.get() & 0x3f) as u8) } pub fn from_sq(&self) -> SQ { - SQ::from(((self.0 >> 6) & 0x3f) as u8) + SQ::from(((self.0.get() >> 6) & 0x3f) as u8) } pub fn flags(&self) -> MoveFlags { - MoveFlags::from(((self.0 >> 12) & 0xf) as u8) + MoveFlags::from(((self.0.get() >> 12) & 0xf) as u8) } pub fn move_int(&self) -> MoveInt { - self.0 + self.0.get() } pub fn is_quiet(&self) -> bool { - (self.0 >> 12) & 0b1100 == 0 + (self.0.get() >> 12) & 0b1100 == 0 } pub fn is_capture(&self) -> bool { - (self.0 >> 12) & 0b0100 != 0 + (self.0.get() >> 12) & 0b0100 != 0 } pub fn is_ep(&self) -> bool { @@ -57,7 +62,7 @@ impl Move { impl From for Move { fn from(m: MoveInt) -> Self { - Self(m) + Self(NonZero::new(m).expect("MoveInt is zero.")) } } diff --git a/src/square.rs b/src/square.rs index b8b4165..ef05346 100644 --- a/src/square.rs +++ b/src/square.rs @@ -26,7 +26,7 @@ impl SQ { } pub fn bb(self) -> Bitboard { - Self::SQUARES_BB[self as usize] + B!(1 << self as usize) } pub fn index(self) -> usize { diff --git a/src/tt.rs b/src/tt.rs index d7f83d7..a4767d3 100644 --- a/src/tt.rs +++ b/src/tt.rs @@ -14,7 +14,7 @@ use super::types::*; #[derive(Eq, PartialEq, Copy, Clone)] pub struct TTEntry { value: Value, - best_move: MoveInt, + best_move: Option, depth: Depth, flag: Bound, } @@ -22,7 +22,7 @@ pub struct TTEntry { impl TTEntry { pub fn new(value: Value, best_move: Option, depth: Depth, flag: Bound) -> Self { TTEntry { - best_move: best_move.map_or(0, |m| m.move_int()), + best_move, depth, value, flag, @@ -30,10 +30,7 @@ impl TTEntry { } pub fn best_move(&self) -> Option { - match self.best_move { - 0 => None, - 1.. => Some(Move::from(self.best_move)), - } + self.best_move } pub fn depth(&self) -> Depth { @@ -52,7 +49,7 @@ impl TTEntry { impl Default for TTEntry { fn default() -> Self { Self { - best_move: 0, + best_move: None, depth: 0, value: 0, flag: Bound::Exact, diff --git a/src/uci.rs b/src/uci.rs index 4ba878a..9f9c7da 100644 --- a/src/uci.rs +++ b/src/uci.rs @@ -199,5 +199,5 @@ static PERFT_RE: LazyLock = LazyLock::new(|| { (?P.*?) $", ) - .expect("Failed to compile perft regex.") + .expect("Failed to compile perft regex.") });