Skip to content

Commit

Permalink
Use NonZeroU16 in move
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiaha committed Nov 28, 2024
1 parent 775d41b commit c2331b5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
29 changes: 17 additions & 12 deletions src/moov.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -57,7 +62,7 @@ impl Move {

impl From<MoveInt> for Move {
fn from(m: MoveInt) -> Self {
Self(m)
Self(NonZero::new(m).expect("MoveInt is zero."))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions src/tt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@ use super::types::*;
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct TTEntry {
value: Value,
best_move: MoveInt,
best_move: Option<Move>,
depth: Depth,
flag: Bound,
}

impl TTEntry {
pub fn new(value: Value, best_move: Option<Move>, depth: Depth, flag: Bound) -> Self {
TTEntry {
best_move: best_move.map_or(0, |m| m.move_int()),
best_move,
depth,
value,
flag,
}
}

pub fn best_move(&self) -> Option<Move> {
match self.best_move {
0 => None,
1.. => Some(Move::from(self.best_move)),
}
self.best_move
}

pub fn depth(&self) -> Depth {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/uci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,5 @@ static PERFT_RE: LazyLock<Regex> = LazyLock::new(|| {
(?P<depth>.*?)
$",
)
.expect("Failed to compile perft regex.")
.expect("Failed to compile perft regex.")
});

0 comments on commit c2331b5

Please sign in to comment.