Add TT lookups in quiescence, move TT inside Game, remove redundant occupancy and time limit functions

This commit is contained in:
stefiosif
2025-01-18 13:01:06 +02:00
parent e9729cf95d
commit 824f8a37b5
9 changed files with 70 additions and 119 deletions

View File

@@ -7,7 +7,10 @@ use std::{
use anyhow::{anyhow, bail};
use crate::{
board::{board::Color, game::Game},
board::{
board::{Board, Color},
game::Game,
},
movegen::r#move::Move,
search::{
iterative_deepening, transposition_table::TranspositionTable, MAX_DEPTH, MAX_TT_SIZE,
@@ -137,11 +140,7 @@ pub fn uci_position(position: &mut SplitWhitespace) -> anyhow::Result<Game> {
Ok(game)
}
pub fn uci_go(
go_iter: &mut SplitWhitespace,
game: &mut Game,
tt: &mut TranspositionTable,
) -> anyhow::Result<Move> {
pub fn uci_go(go_iter: &mut SplitWhitespace, game: &mut Game) -> anyhow::Result<Move> {
let mut params = UciParameters::new();
while let Some(subcommand) = go_iter.next() {
match subcommand {
@@ -159,14 +158,12 @@ pub fn uci_go(
Color::Black => params.btime.unwrap_or(REMAINING_TIME_DEFAULT),
};
iterative_deepening::iterative_deepening(game, MAX_DEPTH, remaining_time, tt)?.ok_or_else(
|| {
anyhow!(
"No stored best move found. Time: {}",
remaining_time - time.elapsed().as_millis()
)
},
)
iterative_deepening::iterative_deepening(game, MAX_DEPTH, remaining_time)?.ok_or_else(|| {
anyhow!(
"No stored best move found. Time: {}",
remaining_time - time.elapsed().as_millis()
)
})
}
fn parse_next<T: std::str::FromStr>(go_iter: &mut SplitWhitespace, val: &str) -> anyhow::Result<T> {
@@ -178,7 +175,6 @@ fn parse_next<T: std::str::FromStr>(go_iter: &mut SplitWhitespace, val: &str) ->
pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> anyhow::Result<()> {
let mut params = UciParameters::new();
let mut tt = TranspositionTable::new(MAX_TT_SIZE);
for line in input.lines() {
let line_str = line.unwrap_or_else(|_| "quit".to_string());
@@ -188,7 +184,14 @@ pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> anyhow::Result
Command::Uci => Response::UciOk,
Command::IsReady => Response::ReadyOk,
Command::UciNewGame => {
tt = TranspositionTable::new(MAX_TT_SIZE);
params.game.as_mut().map_or_else(
|| Response::Info("Failed to unwrap from UciParameter".to_string()),
|game| {
game.tt = TranspositionTable::new(MAX_TT_SIZE);
game.board = Board::startpos();
Response::Info("Initialized new game".to_string())
},
);
Response::Info("Clear cache".to_string())
}
Command::Position => {
@@ -197,7 +200,7 @@ pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> anyhow::Result
}
Command::Go => params.game.as_mut().map_or_else(
|| Response::Info("Failed to unwrap from UciParameter".to_string()),
|game| match uci_go(&mut parts, game, &mut tt) {
|game| match uci_go(&mut parts, game) {
Ok(best_move) => Response::BestMove(best_move.parse_into_str()),
Err(e) => Response::Info(e.to_string()),
},
@@ -221,7 +224,6 @@ mod tests {
board::{fen::from_fen, square::Square},
interface::uci::{parse_command, Command},
movegen::{attack_generator::init_attacks, r#move::Move},
search::{transposition_table::TranspositionTable, MAX_TT_SIZE},
};
use super::uci_go;
@@ -257,11 +259,10 @@ mod tests {
#[test]
fn test_uci_go() -> anyhow::Result<()> {
init_attacks();
let mut tt = TranspositionTable::new(MAX_TT_SIZE);
let mut game = from_fen(FEN_MATE_IN_1).unwrap();
let command_go = "go depth 2";
let mut parts = command_go.split_whitespace();
let response = uci_go(&mut parts, &mut game, &mut tt)?;
let response = uci_go(&mut parts, &mut game)?;
assert_eq!(Move::new(Square::E3, Square::F2), response);