Implement make_move function on engine
This commit is contained in:
16
rs/src/ui.rs
16
rs/src/ui.rs
@@ -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()),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user