add command

This commit is contained in:
Baptiste Fouques 2023-02-20 17:25:24 +01:00
parent fb9e31c9af
commit 4dd643f66b
3 changed files with 27 additions and 21 deletions

View File

@ -153,8 +153,8 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
/// ///
/// uci.make_move(ChessMove::from_str("e2e4").expect("error converting e2e4")); /// uci.make_move(ChessMove::from_str("e2e4").expect("error converting e2e4"));
/// ``` /// ```
pub fn make_move(&mut self, chess_move: ChessMove) -> bool { pub fn make_move(&mut self, chess_move: &ChessMove) -> bool {
if self.game.make_move(chess_move) { if self.game.make_move(*chess_move) {
self.push(GuiCommand::Position { self.push(GuiCommand::Position {
position: Some(self.initial), position: Some(self.initial),
moves: self.game.actions().to_vec(), moves: self.game.actions().to_vec(),
@ -181,7 +181,7 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
self.new_game(); self.new_game();
for action in actions { for action in actions {
if let Action::MakeMove(chessmove) = action { if let Action::MakeMove(chessmove) = action {
self.make_move(chessmove); self.make_move(&chessmove);
} }
} }
@ -224,23 +224,29 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
pub fn exec(&mut self, command: &str) -> Result<EngineCommand, &'static str> { pub fn exec(&mut self, command: &str) -> Result<EngineCommand, &'static str> {
let uci_command = parse(&mut command.to_string()); let uci_command = parse(&mut command.to_string());
match uci_command.clone() { match uci_command.clone() {
Ok(EngineCommand::Id { id }) => self.update(&id), Ok(command) => self.exec_command(&command),
Ok(EngineCommand::UciOk) => self.uciok(),
Ok(EngineCommand::Opt { options: _ }) => {}
Ok(EngineCommand::Info { infos: _ })
| Ok(EngineCommand::BestMove {
best_move: _,
ponder: _,
})
| Ok(EngineCommand::ReadyOk) => {}
Ok(EngineCommand::Registration) | Ok(EngineCommand::CopyProtection) => {
unimplemented!("command not implemented")
}
Err(_) => warn!("Not a command"), Err(_) => warn!("Not a command"),
}; };
uci_command uci_command
} }
pub fn exec_command(&mut self, command: &EngineCommand) {
match command {
EngineCommand::Id { id } => self.update(&id),
EngineCommand::UciOk => self.uciok(),
EngineCommand::Opt { options: _ } => {}
EngineCommand::Info { infos: _ }
| EngineCommand::BestMove {
best_move: _,
ponder: _,
}
| EngineCommand::ReadyOk => {}
EngineCommand::Registration | EngineCommand::CopyProtection => {
unimplemented!("command not implemented")
}
}
}
/// Retrieve a line from the uci engine input stream and parse it /// Retrieve a line from the uci engine input stream and parse it
pub fn pull(&mut self) -> Option<EngineCommand> { pub fn pull(&mut self) -> Option<EngineCommand> {
if let Some(source) = &mut self.source { if let Some(source) = &mut self.source {
@ -327,11 +333,11 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
self.uciok self.uciok
} }
pub fn human_play(&mut self, chess_move: ChessMove) -> Result<ChessMove, &'static str> { pub fn human_play(&mut self, chess_move: &ChessMove) -> Result<ChessMove, &'static str> {
match self.player[self.side_to_move().to_index()] { match self.player[self.side_to_move().to_index()] {
Player::Human { .. } => { Player::Human { .. } => {
if self.make_move(chess_move) { if self.make_move(chess_move) {
Ok(chess_move) Ok(*chess_move)
} else { } else {
Err("Invalid move for human player") Err("Invalid move for human player")
} }

View File

@ -4,7 +4,7 @@ use std::{str::FromStr, time::Duration};
use chess::{Action, Board, ChessMove}; use chess::{Action, Board, ChessMove};
#[derive(Clone, Default)] #[derive(Clone, Default, Debug)]
pub struct Id { pub struct Id {
name: Option<String>, name: Option<String>,
author: Option<String>, author: Option<String>,
@ -63,7 +63,7 @@ impl fmt::Display for Opt {
} }
} }
#[derive(Clone)] #[derive(Clone, Debug)]
pub enum EngineCommand { pub enum EngineCommand {
Id { Id {
id: Id, id: Id,

View File

@ -58,7 +58,7 @@ pub fn main() {
uci.new_game(); uci.new_game();
uci.push_raw("d\n"); uci.push_raw("d\n");
uci.human_play(ChessMove::from_str("e2e4").expect("error converting e2e4")) uci.human_play(&ChessMove::from_str("e2e4").expect("error converting e2e4"))
.expect("can not make human move"); .expect("can not make human move");
uci.push_raw("d\n"); uci.push_raw("d\n");
@ -68,7 +68,7 @@ pub fn main() {
loop { loop {
match uci.pull() { match uci.pull() {
Some(EngineCommand::BestMove { best_move, .. }) => { Some(EngineCommand::BestMove { best_move, .. }) => {
uci.make_move(best_move); uci.make_move(&best_move);
break; break;
} }
Some(EngineCommand::Info { .. }) => continue, Some(EngineCommand::Info { .. }) => continue,