clean options and implement undo

This commit is contained in:
Baptiste Fouques 2023-02-03 13:55:31 +01:00
parent 256c56d841
commit fb9e31c9af
2 changed files with 47 additions and 40 deletions

View File

@ -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<Duration>, Option<Duration>) =
(Some(Duration::from_secs(120 * 60)), None);
@ -60,12 +60,9 @@ pub enum Player {
Engine { elo: Option<u32> },
}
pub enum GameOption {
WhiteTotalTime { value: Option<Duration> },
BlackTotalTime { value: Option<Duration> },
WhiteIncrement { value: Option<Duration> },
BlackIncrement { value: Option<Duration> },
WhitePlayer { value: Player },
BlackPlayer { value: Player },
TotalTime { color: Color, value: Option<Duration> },
Increment { color: Color, value: Option<Duration> },
Player {color: Color, value: Player},
}
impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
@ -88,41 +85,26 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
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<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
}
}
pub fn back_move(&mut self) -> Option<ChessMove> {
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);

View File

@ -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();