Implement make_move function on engine

This commit is contained in:
2021-12-14 21:12:59 +01:00
parent 27dce5f7a3
commit 4ae73410bd
2 changed files with 40 additions and 6 deletions

View File

@@ -120,6 +120,21 @@ impl Ui {
}
}
}
fn make_move(&mut self, args: &[&str]) -> Result<String, String> {
if args.len() != 2 {
return Err("make_move takes 2 args".to_owned());
};
let source = board::Position::parse(args[0])
.map_err(|_| format!("Error parsing source {}", args[0]))?;
let target = board::Position::parse(args[1])
.map_err(|_| format!("Error parsing target {}", args[1]))?;
match self.board.make_move(&source, target) {
Err(()) => {
Err(format!("Move not possible {}-{}", args[0], args[1]))
}
Ok(()) => Ok(self.board.to_string()),
}
}
fn handle_command(&mut self, s: &str) -> Result<String, String> {
let mut cmd = s.split(',');
// There will be at least an empty string => otherwise panic
@@ -128,6 +143,7 @@ impl Ui {
match cmd_type {
"get_state" => self.get_state(&args),
"get_moves" => self.get_moves(&args),
"make_move" => self.make_move(&args),
"" => Err("No command given".to_owned()),
_ => Err("Invalid command".to_owned()),
}