Merge UciGame and SerachParameters into UciParameters

This commit is contained in:
stefiosif
2024-08-04 11:28:08 +03:00
parent 191f7f9acb
commit 688a401a3d

View File

@@ -57,16 +57,18 @@ impl fmt::Display for Response {
} }
} }
struct SearchParameters { struct UciParameters {
movetime: Option<usize>, movetime: Option<usize>,
depth: Option<u8>, depth: Option<u8>,
game: Option<Game>,
} }
impl SearchParameters { impl UciParameters {
const fn new() -> Self { const fn new() -> Self {
Self { Self {
movetime: None, movetime: None,
depth: None, depth: None,
game: None,
} }
} }
@@ -77,9 +79,13 @@ impl SearchParameters {
fn add_depth(&mut self, depth: u8) { fn add_depth(&mut self, depth: u8) {
self.depth = Some(depth); self.depth = Some(depth);
} }
fn add_game(&mut self, game: Game) {
self.game = Some(game);
}
} }
impl Default for SearchParameters { impl Default for UciParameters {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
@@ -104,7 +110,7 @@ pub fn uci_position(mut position: SplitWhitespace) -> Result<Game, String> {
const MAX_DEPTH: u8 = 5; const MAX_DEPTH: u8 = 5;
pub fn uci_go(mut go: SplitWhitespace, game: &mut Game) -> Result<Move, String> { pub fn uci_go(mut go: SplitWhitespace, game: &mut Game) -> Result<Move, String> {
let mut params = SearchParameters::new(); let mut params = UciParameters::new();
while let Some(subcommand) = go.next() { while let Some(subcommand) = go.next() {
match subcommand { match subcommand {
"depth" => params.add_depth(go.next().unwrap().parse::<u8>().ok().unwrap()), "depth" => params.add_depth(go.next().unwrap().parse::<u8>().ok().unwrap()),
@@ -117,32 +123,12 @@ pub fn uci_go(mut go: SplitWhitespace, game: &mut Game) -> Result<Move, String>
Ok(Move::new(0, 0)) Ok(Move::new(0, 0))
} }
struct UciGame {
game: Option<Game>,
}
impl UciGame {
const fn new() -> Self {
Self { game: None }
}
fn set_game_state(&mut self, game: Game) {
self.game = Some(game);
}
}
impl Default for UciGame {
fn default() -> Self {
Self::new()
}
}
pub fn uci_loop() -> Result<(), String> { pub fn uci_loop() -> Result<(), String> {
let stdin = io::stdin(); let stdin = io::stdin();
let stdout = io::stdout(); let stdout = io::stdout();
let handle_in = stdin.lock(); let handle_in = stdin.lock();
let mut handle_out = stdout.lock(); let mut handle_out = stdout.lock();
let mut uci_game = UciGame::new(); let mut params = UciParameters::new();
for line in handle_in.lines() { for line in handle_in.lines() {
let line_str = line.unwrap_or_else(|_| "quit".to_string()); let line_str = line.unwrap_or_else(|_| "quit".to_string());
@@ -152,11 +138,11 @@ pub fn uci_loop() -> Result<(), String> {
Command::Uci => Response::UciOk, Command::Uci => Response::UciOk,
Command::IsReady => Response::ReadyOk, Command::IsReady => Response::ReadyOk,
Command::Position => { Command::Position => {
uci_game.set_game_state(uci_position(parts)?); params.add_game(uci_position(parts)?);
Response::Info("Initialized position.".to_string()) Response::Info("Initialized position.".to_string())
} }
Command::Go => { Command::Go => {
if let Some(ref mut game) = uci_game.game { if let Some(ref mut game) = params.game {
let best_move = uci_go(parts, game)?; let best_move = uci_go(parts, game)?;
Response::BestMove(best_move.parse_into_str()) Response::BestMove(best_move.parse_into_str())
} else { } else {