Refactor based on clippy, move tt to search module and call iterative deepening from uci_go

This commit is contained in:
stefiosif
2024-11-03 01:19:12 +02:00
parent 802bdcdb37
commit 1b28de5064
11 changed files with 67 additions and 92 deletions

View File

@@ -166,7 +166,9 @@ impl Game {
board.state.halfmove_clock = new_halfmove_clock;
}
let mv = move_parameters.mv.unwrap();
let mv = move_parameters
.mv
.expect("Expected move parameters from history stack");
let piece_at_dst = mailbox.piece_at(mv.dst).expect("Expected set piece");
match &mv.move_type {
MoveType::Quiet | MoveType::DoublePush => {

View File

@@ -1,7 +1,6 @@
use super::{
bitboard::{have_common_bit, square_to_bitboard},
board::{Board, PieceType},
square::Square,
};
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -12,8 +11,9 @@ pub struct Mailbox {
impl Mailbox {
pub fn from_board(board: &Board) -> Self {
let mut mailbox: [Option<PieceType>; 64] = [None; 64];
for square in Square::A1..=Square::H8 {
mailbox[square] = board
for (square, mailbox_square) in mailbox.iter_mut().enumerate() {
*mailbox_square = board
.white_pieces
.iter()
.chain(board.black_pieces.iter())

View File

@@ -6,5 +6,4 @@ pub mod history;
pub mod mailbox;
pub mod square;
pub mod state;
pub mod transposition_table;
pub mod zobrist;

View File

@@ -1,112 +0,0 @@
use crate::movegen::r#move::Move;
use super::zobrist::ZobristHash;
pub struct TranspositionTable {
positions: Vec<Option<TTEntry>>,
size: u64,
}
impl TranspositionTable {
pub fn new(size: u64) -> Self {
Self {
positions: vec![None; size as usize],
size,
}
}
pub fn lookup(&self, zobrist_hash: ZobristHash) -> Option<&TTEntry> {
let entry = self
.positions
.get((zobrist_hash.hash % self.size) as usize)
.and_then(|entry| entry.as_ref());
entry
}
pub fn insert(&mut self, tt_entry: TTEntry) {
let index = (tt_entry.hash.hash % self.size) as usize;
if let Some(stored) = &self.positions[index] {
if tt_entry.depth > stored.depth || matches!(tt_entry.bound, Bound::Exact) {
self.positions[index] = Some(tt_entry);
}
} else {
self.positions[index] = Some(tt_entry);
}
}
}
#[derive(Clone)]
pub struct TTEntry {
pub hash: ZobristHash,
pub depth: u8,
pub score: i32,
pub mv: Option<Move>,
pub bound: Bound,
}
impl TTEntry {
pub const fn new(
hash: ZobristHash,
depth: u8,
score: i32,
mv: Option<Move>,
bound: Bound,
) -> Self {
Self {
hash,
depth,
score,
mv,
bound,
}
}
}
#[derive(Clone)]
pub enum Bound {
Exact,
Lower,
Upper,
}
#[cfg(test)]
mod tests {
use crate::{
board::{fen::from_fen, transposition_table::TranspositionTable},
evaluation::{MAX_SCORE, MIN_SCORE},
movegen::attack_generator::init_attacks,
search,
};
const FEN: &str = "1r2k2r/2P1pq1p/2npb3/1p3ppP/p3P3/P2B1Q2/1P1PNPP1/R3K2R w KQk g6 0 1";
const FEN_WILL_BE: &str = "1r2k2r/2P1pq1p/2npb3/1B3ppP/p3P3/P4Q2/1P1PNPP1/R3K2R b KQk - 0 1";
const FEN_WONT_BE: &str = "1Q2k2r/2P1pq1p/2npb3/1p3ppP/p3P3/P2B4/1P1PNPP1/R3K2R b KQk - 0 1";
#[test]
fn test_transposition_table() -> Result<(), String> {
init_attacks();
let mut game = from_fen(FEN)?;
let mut tt = TranspositionTable::new(1000000);
let mut nodes = 0;
let time_now = std::time::Instant::now();
// fill Transposition Table
search::negamax::negamax(&mut game, MIN_SCORE, MAX_SCORE, 4, 0, &mut tt, &mut nodes);
dbg!(nodes);
dbg!(time_now.elapsed());
let will_be_hash = from_fen(FEN_WILL_BE)?.hash;
let tt_entry = tt.lookup(will_be_hash);
assert!(tt_entry.is_some());
let wont_be_hash = from_fen(FEN_WONT_BE)?.hash;
let tt_entry = tt.lookup(wont_be_hash);
assert!(tt_entry.is_none());
Ok(())
}
}