Skip to content

Commit

Permalink
use options in for moves
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiaha committed Nov 17, 2024
1 parent ac2d50f commit 201d5b3
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl fmt::Debug for Bitboard {
let mut result = String::new();
for i in (0..=56).rev().step_by(8) {
for j in 0..8 {
result.push_str(format!("{} ", self.0 >> (i + j) & 1).as_ref());
result.push_str(format!("{} ", self.0 >> (i + j) & 1).as_str());
}
result.push('\n');
}
Expand Down
46 changes: 24 additions & 22 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl Board {
self.is_attacked(self.bitboard_of(self.ctm, PieceType::King).lsb())
}

pub fn peek(&self) -> Move {
pub fn peek(&self) -> Option<Move> {
self.history[self.ply].moov()
}

Expand Down Expand Up @@ -233,7 +233,6 @@ impl Board {

self.history[self.ply] = HistoryEntry::default()
.with_entry(self.history[self.ply - 1].entry())
.with_moov(Move::NULL)
.with_half_move_counter(self.history[self.ply - 1].half_move_counter() + 1)
.with_plies_from_null(0)
.with_material_hash(self.history[self.ply - 1].material_hash());
Expand Down Expand Up @@ -311,7 +310,7 @@ impl Board {
};
self.history[self.ply] = HistoryEntry::default()
.with_entry(self.history[self.ply - 1].entry() | m.to_sq().bb() | m.from_sq().bb())
.with_moov(m)
.with_moov(Some(m))
.with_half_move_counter(half_move_counter)
.with_plies_from_null(self.history[self.ply - 1].plies_from_null() + 1)
.with_captured(captured)
Expand All @@ -321,11 +320,11 @@ impl Board {
self.hasher.update_color();
}

pub fn pop(&mut self) -> Move {
pub fn pop(&mut self) -> Option<Move> {
self.ctm = !self.ctm;
self.hasher.update_color();

let m = self.history[self.ply].moov();
let m = self.history[self.ply].moov()?;
match m.flags() {
MoveFlags::Quiet => {
self.move_piece_quiet(m.to_sq(), m.from_sq());
Expand Down Expand Up @@ -361,7 +360,7 @@ impl Board {
self.set_piece_at(
self.history[self.ply]
.captured()
.expect("Tried to revert a capture move with no capture"),
.expect("Tried to revert a capture move with no capture."),
m.to_sq(),
);
}
Expand All @@ -370,13 +369,13 @@ impl Board {
self.set_piece_at(
self.history[self.ply]
.captured()
.expect("Tried to revert a capture move with no capture"),
.expect("Tried to revert a capture move with no capture."),
m.to_sq(),
);
}
}
self.ply -= 1;
m
Some(m)
}

pub fn generate_legal_moves<const QUIET: bool>(&self, moves: &mut MoveList) {
Expand Down Expand Up @@ -490,21 +489,22 @@ impl Board {
}
1 => {
let checker_square = checkers.lsb();
let pt = self.piece_type_at(checker_square).unwrap();
let pt = self
.piece_type_at(checker_square)
.expect(format!("No checker at {}", checker_square).as_str());
match pt {
PieceType::Pawn | PieceType::Knight => {
///////////////////////////////////////////////////////////////////
// If the checkers is a pawn, we have to look out for ep moves
// that can capture it.
///////////////////////////////////////////////////////////////////
let epsq = self.history[self.ply].epsq();
if pt == PieceType::Pawn
&& checkers
== self.history[self.ply]
.epsq()
.map_or(Bitboard::ZERO, |sq| sq.bb())
.shift(Direction::South.relative(us))
&& epsq.is_some_and(|epsq| {
checkers == epsq.bb().shift(Direction::South.relative(us))
})
{
let epsq = self.history[self.ply].epsq().unwrap();
let epsq = epsq.expect("No epsq found for checker.");
let pawns = attacks::pawn_attacks_sq(epsq, them)
& self.bitboard_of(us, PieceType::Pawn)
& not_pinned;
Expand Down Expand Up @@ -630,7 +630,9 @@ impl Board {
///////////////////////////////////////////////////////////////////
let pinned_pieces = !(not_pinned | self.bitboard_of(us, PieceType::Knight));
for sq in pinned_pieces {
let pt = self.piece_type_at(sq).unwrap();
let pt = self.piece_type_at(sq).expect(
format!("Unexpected None for piece type at square {}.", sq).as_str(),
);
let attacks_along_pin =
attacks::attacks(pt, sq, all) & Bitboard::line(our_king, sq);
if QUIET {
Expand Down Expand Up @@ -1075,18 +1077,18 @@ impl fmt::Display for Board {
match self.board[sq.index()] {
Some(pc) => {
if empty_squares != 0 {
board_str.push_str(empty_squares.to_string().as_ref());
board_str.push_str(empty_squares.to_string().as_str());
empty_squares = 0;
}
board_str.push_str(pc.to_string().as_ref());
board_str.push_str(pc.to_string().as_str());
}
None => {
empty_squares += 1;
}
}
}
if empty_squares != 0 {
board_str.push_str(empty_squares.to_string().as_ref());
board_str.push_str(empty_squares.to_string().as_str());
}
if rank != Rank::One {
board_str.push('/');
Expand Down Expand Up @@ -1159,7 +1161,7 @@ pub struct HistoryEntry {
entry: Bitboard,
captured: Option<Piece>,
epsq: Option<SQ>,
moov: Move,
moov: Option<Move>,
material_hash: Hash,
half_move_counter: u16,
plies_from_null: u16,
Expand All @@ -1170,7 +1172,7 @@ impl HistoryEntry {
self.entry
}

pub fn moov(&self) -> Move {
pub fn moov(&self) -> Option<Move> {
self.moov
}

Expand Down Expand Up @@ -1199,7 +1201,7 @@ impl HistoryEntry {
*self
}

pub fn with_moov(&mut self, moov: Move) -> Self {
pub fn with_moov(&mut self, moov: Option<Move>) -> Self {
self.moov = moov;
*self
}
Expand Down
18 changes: 4 additions & 14 deletions src/moov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl Move {
MoveFlags::from(((self.0 >> 12) & 0xf) as u8)
}

pub fn move_int(&self) -> MoveInt {
self.0
}

pub fn is_quiet(&self) -> bool {
(self.0 >> 12) & 0b1100 == 0
}
Expand All @@ -49,10 +53,6 @@ impl Move {
pub fn is_castling(&self) -> bool {
matches!(self.flags(), MoveFlags::OO | MoveFlags::OOO)
}

pub fn is_null(&self) -> bool {
*self == Move::NULL
}
}

impl From<MoveInt> for Move {
Expand All @@ -73,10 +73,6 @@ impl fmt::Display for Move {
}
}

impl Move {
pub const NULL: Self = Self(0);
}

#[derive(Debug, PartialEq, Eq)]
pub enum MoveFlags {
Quiet = 0b0000,
Expand All @@ -95,12 +91,6 @@ pub enum MoveFlags {
PcQueen = 0b1111,
}

impl Default for MoveFlags {
fn default() -> Self {
MoveFlags::Quiet
}
}

impl From<u8> for MoveFlags {
fn from(n: u8) -> Self {
unsafe { std::mem::transmute::<u8, Self>(n) }
Expand Down
27 changes: 15 additions & 12 deletions src/move_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const MAX_MOVES: usize = 254;
const MAX_MOVES: usize = 255;

pub struct MoveList {
moves: [Move; MAX_MOVES],
moves: [Option<Move>; MAX_MOVES],
pub scores: [Value; MAX_MOVES],
idx: usize,
len: usize,
Expand All @@ -30,7 +30,7 @@ pub struct MoveList {
impl MoveList {
pub fn new() -> Self {
Self {
moves: [Move::NULL; MAX_MOVES],
moves: [None; MAX_MOVES],
scores: [0; MAX_MOVES],
idx: 0,
len: 0,
Expand All @@ -54,31 +54,31 @@ impl MoveList {
}

pub fn contains(&self, m: Move) -> bool {
self.moves[..self.len].contains(&m)
self.moves[..self.len].contains(&Some(m))
}

pub fn push(&mut self, m: Move) {
self.moves[self.len] = m;
self.moves[self.len] = Some(m);
self.len += 1;
}

pub fn make_q(&mut self, from_sq: SQ, to: Bitboard) {
for to_sq in to {
self.moves[self.len] = Move::new(from_sq, to_sq, MoveFlags::Quiet);
self.moves[self.len] = Some(Move::new(from_sq, to_sq, MoveFlags::Quiet));
self.len += 1;
}
}

pub fn make_c(&mut self, from_sq: SQ, to: Bitboard) {
for to_sq in to {
self.moves[self.len] = Move::new(from_sq, to_sq, MoveFlags::Capture);
self.moves[self.len] = Some(Move::new(from_sq, to_sq, MoveFlags::Capture));
self.len += 1;
}
}

pub fn make_dp(&mut self, from_sq: SQ, to: Bitboard) {
for to_sq in to {
self.moves[self.len] = Move::new(from_sq, to_sq, MoveFlags::DoublePush);
self.moves[self.len] = Some(Move::new(from_sq, to_sq, MoveFlags::DoublePush));
self.len += 1;
}
}
Expand All @@ -91,7 +91,7 @@ impl MoveList {
MoveFlags::PcRook,
MoveFlags::PcBishop,
] {
self.moves[self.len] = Move::new(from_sq, to_sq, flag);
self.moves[self.len] = Some(Move::new(from_sq, to_sq, flag));
self.len += 1;
}
}
Expand All @@ -117,7 +117,7 @@ impl MoveList {

let idx = self.idx;
self.idx += 1;
Some(self.moves[idx])
self.moves[idx]
}
}

Expand All @@ -132,23 +132,26 @@ impl Iterator for MoveList {

let idx = self.idx;
self.idx += 1;
Some(self.moves[idx])
self.moves[idx]
}
}

impl Index<usize> for MoveList {
type Output = Move;

fn index(&self, i: usize) -> &Self::Output {
&self.moves[i]
self.moves[i]
.as_ref()
.expect("Tried to access an empty slot in MoveList")
}
}

impl fmt::Debug for MoveList {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut result = String::from('[');
for i in 0..self.len {
result.push_str(format!("{}, ", self.moves[i]).as_ref());
let m = self.moves[i].expect("None found in MoveList");
result.push_str(format!("{}, ", m).as_str());
}
result.push(']');
write!(f, "{}", result)
Expand Down
Loading

0 comments on commit 201d5b3

Please sign in to comment.