Add depth based logging

This commit is contained in:
stefiosif
2025-01-25 21:47:11 +02:00
parent 2ed92f9253
commit 2d9076cd1d
6 changed files with 133 additions and 34 deletions

View File

@@ -25,6 +25,7 @@ pub enum Command {
UciNewGame,
Position,
Go,
Stop,
Quit,
}
@@ -35,6 +36,7 @@ fn parse_command(parts: &mut SplitWhitespace) -> anyhow::Result<Command> {
Some("ucinewgame") => Ok(Command::UciNewGame),
Some("position") => Ok(Command::Position),
Some("go") => Ok(Command::Go),
Some("stop") => Ok(Command::Stop),
Some("quit") => Ok(Command::Quit),
_ => bail!("Unrecognised command"),
}
@@ -47,7 +49,7 @@ pub enum Response {
Info(String),
}
fn write_response(handle_out: &mut impl Write, response: &Response) -> anyhow::Result<()> {
pub fn write_response(handle_out: &mut impl Write, response: &Response) -> anyhow::Result<()> {
writeln!(handle_out, "{response}").map_err(|e| anyhow!(e))?;
handle_out.flush().map_err(|e| anyhow!(e))?;
Ok(())
@@ -180,36 +182,43 @@ pub fn uci_loop<R: BufRead, W: Write>(input: R, mut output: W) -> anyhow::Result
let line_str = line.unwrap_or_else(|_| "quit".to_string());
let mut parts = line_str.split_whitespace();
let command = parse_command(&mut parts)?;
let response = match command {
Command::Uci => Response::UciOk,
Command::IsReady => Response::ReadyOk,
match command {
Command::Uci => write_response(&mut output, &Response::UciOk)?,
Command::IsReady => write_response(&mut output, &Response::ReadyOk)?,
Command::UciNewGame => {
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())
if let Some(game) = params.game.as_mut() {
game.tt = TranspositionTable::new(MAX_TT_SIZE);
game.board = Board::startpos();
} else {
let game = Game::new();
params.add_game(game);
}
write_response(&mut output, &Response::Info("Clear cache".to_string()))?;
}
Command::Position => {
//TODO: doesnt have to create a new game every time, we can just update the game
params.add_game(uci_position(&mut parts)?);
Response::Info("Initialized position".to_string())
}
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) {
Ok(best_move) => Response::BestMove(best_move.parse_into_str()),
Err(e) => Response::Info(e.to_string()),
},
),
// TODO: Command::Stop => (),
Command::Go => {
if let Some(game) = params.game.as_mut() {
match uci_go(&mut parts, game) {
Ok(best_move) => write_response(
&mut output,
&Response::BestMove(best_move.parse_into_str()),
),
Err(e) => write_response(&mut output, &Response::Info(e.to_string())),
}?;
} else {
write_response(
&mut output,
&Response::Info("Failed to unwrap from UciParameter".to_string()),
)?;
};
}
Command::Stop => break,
Command::Quit => break,
};
write_response(&mut output, &response)?;
output.flush().map_err(|e| anyhow!(e))?
}