From fb9e31c9af4df87642db9c4b4e06894cceb5338a Mon Sep 17 00:00:00 2001 From: Baptiste Fouques Date: Fri, 3 Feb 2023 13:55:31 +0100 Subject: [PATCH] clean options and implement undo --- chess_uci/src/lib.rs | 79 ++++++++++++++++++++------------------ chess_uci_demo/src/main.rs | 8 ++-- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/chess_uci/src/lib.rs b/chess_uci/src/lib.rs index 8e09d35..f49963b 100644 --- a/chess_uci/src/lib.rs +++ b/chess_uci/src/lib.rs @@ -9,7 +9,7 @@ use std::io::*; use std::result::Result; use std::time::Duration; -use chess::{Board, ChessMove, Game}; +use chess::{Board, ChessMove, Game, Action}; const DEFAULT_TIME: (Option, Option) = (Some(Duration::from_secs(120 * 60)), None); @@ -60,12 +60,9 @@ pub enum Player { Engine { elo: Option }, } pub enum GameOption { - WhiteTotalTime { value: Option }, - BlackTotalTime { value: Option }, - WhiteIncrement { value: Option }, - BlackIncrement { value: Option }, - WhitePlayer { value: Player }, - BlackPlayer { value: Player }, + TotalTime { color: Color, value: Option }, + Increment { color: Color, value: Option }, + Player {color: Color, value: Player}, } impl UciEngine { @@ -88,41 +85,26 @@ impl UciEngine { pub fn game_option(&mut self, option: GameOption) -> GameOption { let old_value: GameOption; match option { - GameOption::WhiteTotalTime { value } => { - old_value = GameOption::WhiteTotalTime { - value: self.time[Color::White.to_index()].0, + GameOption::TotalTime { color, value } => { + old_value = GameOption::TotalTime { + color, + value: self.time[color.to_index()].0, }; - self.time[Color::White.to_index()].0 = value + self.time[color.to_index()].0 = value } - GameOption::BlackTotalTime { value } => { - old_value = GameOption::BlackTotalTime { - value: self.time[Color::Black.to_index()].0, + GameOption::Increment { color, value } => { + old_value = GameOption::Increment { + color, + value: self.time[color.to_index()].1, }; - self.time[Color::Black.to_index()].0 = value + self.time[color.to_index()].1 = value } - GameOption::WhiteIncrement { value } => { - old_value = GameOption::WhiteIncrement { - value: self.time[Color::White.to_index()].1, + GameOption::Player { color, value } => { + old_value = GameOption::Player { + color, + value: self.player[color.to_index()], }; - self.time[Color::White.to_index()].1 = value - } - GameOption::BlackIncrement { value } => { - old_value = GameOption::BlackIncrement { - value: self.time[Color::Black.to_index()].1, - }; - self.time[Color::Black.to_index()].1 = value - } - GameOption::WhitePlayer { value } => { - old_value = GameOption::WhitePlayer { - value: self.player[Color::White.to_index()], - }; - self.player[Color::White.to_index()] = value - } - GameOption::BlackPlayer { value } => { - old_value = GameOption::WhitePlayer { - value: self.player[Color::Black.to_index()], - }; - self.player[Color::Black.to_index()] = value + self.player[color.to_index()] = value } } @@ -183,6 +165,29 @@ impl UciEngine { } } + pub fn back_move(&mut self) -> Option { + let mut actions = self.game.actions().clone(); + let mut last_move = None; + loop{ + match actions.pop() { + Some(Action::MakeMove(last)) => { + last_move = Some(last); + break; + }, + None => break, + _ => continue, + } + } + self.new_game(); + for action in actions { + if let Action::MakeMove(chessmove) = action { + self.make_move(chessmove); + } + } + + last_move + } + fn terminate(&mut self, reason: &str) { self.uciok = false; info!("UCI termination: {}", reason); diff --git a/chess_uci_demo/src/main.rs b/chess_uci_demo/src/main.rs index d4baf99..4b5a8c8 100644 --- a/chess_uci_demo/src/main.rs +++ b/chess_uci_demo/src/main.rs @@ -1,7 +1,7 @@ extern crate chess; extern crate chess_uci; -use chess::ChessMove; +use chess::{ChessMove, Color}; use chess_uci::uci_command::EngineCommand; use chess_uci::*; @@ -28,10 +28,12 @@ pub fn main() { process.stdout.expect("Program stdout"), ); let mut uci = UciEngine::new(Some(sf_out), Some(sf_in)); - uci.game_option(GameOption::WhitePlayer { + uci.game_option(GameOption::Player { + color: Color::White, value: Player::Human { elo: None }, }); - uci.game_option(GameOption::BlackPlayer { + uci.game_option(GameOption::Player { + color: Color::Black, value: Player::Engine { elo: Some(1500) }, }); uci.init();