Use one TT per game, use time manger in ID
This commit is contained in:
@@ -4,9 +4,12 @@ use std::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
board::game::Game,
|
||||
board::{board::Color, game::Game},
|
||||
movegen::r#move::Move,
|
||||
search::{self, MAX_DEPTH, MOVE_TIME},
|
||||
search::{
|
||||
iterative_deepening, transposition_table::TranspositionTable, MAX_DEPTH, MAX_TT_SIZE,
|
||||
REMAINING_TIME_DEFAULT,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
@@ -62,6 +65,8 @@ struct UciParameters {
|
||||
movetime: Option<usize>,
|
||||
depth: Option<u8>,
|
||||
game: Option<Game>,
|
||||
wtime: Option<u128>,
|
||||
btime: Option<u128>,
|
||||
}
|
||||
|
||||
impl UciParameters {
|
||||
@@ -70,6 +75,8 @@ impl UciParameters {
|
||||
movetime: None,
|
||||
depth: None,
|
||||
game: None,
|
||||
wtime: None,
|
||||
btime: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,6 +88,14 @@ impl UciParameters {
|
||||
self.depth = Some(depth);
|
||||
}
|
||||
|
||||
fn add_wtime(&mut self, wtime: u128) {
|
||||
self.wtime = Some(wtime);
|
||||
}
|
||||
|
||||
fn add_btime(&mut self, btime: u128) {
|
||||
self.btime = Some(btime);
|
||||
}
|
||||
|
||||
fn add_game(&mut self, game: Game) {
|
||||
self.game = Some(game);
|
||||
}
|
||||
@@ -116,38 +131,41 @@ pub fn uci_position(position: &mut SplitWhitespace) -> Result<Game, String> {
|
||||
Ok(game)
|
||||
}
|
||||
|
||||
pub fn uci_go(go: &mut SplitWhitespace, game: &mut Game) -> Result<Move, String> {
|
||||
pub fn uci_go(
|
||||
go_iter: &mut SplitWhitespace,
|
||||
game: &mut Game,
|
||||
tt: &mut TranspositionTable,
|
||||
) -> Result<Move, String> {
|
||||
let mut params = UciParameters::new();
|
||||
while let Some(subcommand) = go.next() {
|
||||
while let Some(subcommand) = go_iter.next() {
|
||||
match subcommand {
|
||||
// TODO: Add new commands
|
||||
"wtime" => (),
|
||||
"btime" => (),
|
||||
"movestogo" => (),
|
||||
"depth" => {
|
||||
let depth_str = go.next().ok_or("Expected depth value")?;
|
||||
let depth = depth_str.parse::<u8>().map_err(|_| "Invalid depth value")?;
|
||||
params.add_depth(depth);
|
||||
}
|
||||
"movetime" => {
|
||||
let movetime_str = go.next().ok_or("Expected movetime value")?;
|
||||
let movetime = movetime_str
|
||||
.parse::<usize>()
|
||||
.map_err(|_| "Invalid movetime value")?;
|
||||
params.add_movetime(movetime);
|
||||
}
|
||||
"wtime" => params.add_wtime(parse_next(go_iter, "wtime")?),
|
||||
"btime" => params.add_btime(parse_next(go_iter, "btime")?),
|
||||
"depth" => params.add_depth(parse_next(go_iter, "depth")?),
|
||||
"movetime" => params.add_movetime(parse_next(go_iter, "movetime")?),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(
|
||||
search::iterative_deepening::iterative_deepening(game, MAX_DEPTH, MOVE_TIME)
|
||||
.expect("No move selected"),
|
||||
)
|
||||
let remaining_time = match game.current_player() {
|
||||
Color::White => params.wtime.unwrap_or(REMAINING_TIME_DEFAULT),
|
||||
Color::Black => params.btime.unwrap_or(REMAINING_TIME_DEFAULT),
|
||||
};
|
||||
|
||||
iterative_deepening::iterative_deepening(game, MAX_DEPTH, remaining_time, tt)
|
||||
.ok_or_else(|| "No move selected".to_string())
|
||||
}
|
||||
|
||||
fn parse_next<T: std::str::FromStr>(go_iter: &mut SplitWhitespace, val: &str) -> Result<T, String> {
|
||||
go_iter
|
||||
.next()
|
||||
.ok_or_else(|| format!("Expected {val}"))
|
||||
.and_then(|v| v.parse::<T>().map_err(|_| format!("Invalid {val}")))
|
||||
}
|
||||
|
||||
pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> Result<(), String> {
|
||||
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());
|
||||
@@ -156,15 +174,17 @@ pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> Result<(), Str
|
||||
let response = match command {
|
||||
Command::Uci => Response::UciOk,
|
||||
Command::IsReady => Response::ReadyOk,
|
||||
Command::UciNewGame => Response::Info("Clear cache".to_string()),
|
||||
Command::UciNewGame => {
|
||||
tt = TranspositionTable::new(MAX_TT_SIZE);
|
||||
Response::Info("Clear cache".to_string())
|
||||
}
|
||||
Command::Position => {
|
||||
params.add_game(uci_position(&mut parts)?);
|
||||
// dbg!(¶ms);
|
||||
Response::Info("Initialized position".to_string())
|
||||
}
|
||||
Command::Go => {
|
||||
if let Some(ref mut game) = params.game {
|
||||
let best_move = uci_go(&mut parts, game)?;
|
||||
let best_move = uci_go(&mut parts, game, &mut tt)?;
|
||||
Response::BestMove(best_move.parse_into_str())
|
||||
} else {
|
||||
Response::Info("Going?".to_string())
|
||||
@@ -188,6 +208,7 @@ 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;
|
||||
@@ -223,10 +244,11 @@ mod tests {
|
||||
#[test]
|
||||
fn test_uci_go() -> Result<(), String> {
|
||||
init_attacks();
|
||||
let mut tt = TranspositionTable::new(MAX_TT_SIZE);
|
||||
let mut game = from_fen(FEN_MATE_IN_1)?;
|
||||
let command_go = "go depth 2";
|
||||
let mut parts = command_go.split_whitespace();
|
||||
let response = uci_go(&mut parts, &mut game)?;
|
||||
let response = uci_go(&mut parts, &mut game, &mut tt)?;
|
||||
|
||||
assert_eq!(Move::new(Square::E3, Square::F2), response);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user