Implement dummy chess solver

It chooses a random available move ¯\_(ツ)_/¯
This commit is contained in:
2021-12-22 22:03:17 +01:00
parent 95c0ccfbd2
commit 0f7de146b7
6 changed files with 142 additions and 11 deletions

View File

@@ -182,10 +182,9 @@ impl Ui {
let error = format!("No moves possible from {}", position);
Err(error)
}
Ok(positions) => Ok(positions
.iter()
.map(|pos| format!("{}", pos))
.collect()),
Ok(positions) => {
Ok(positions.map(|pos| format!("{}", pos)).collect())
}
}
} else {
Err("get_moves takes 1 arg".to_owned())
@@ -208,6 +207,29 @@ impl Ui {
Err("make_move takes 2 args".to_owned())
}
}
fn choose_move(&self, args: &[&str]) -> Result<String, String> {
if let [color_str] = args {
if let [color_char] = color_str.chars().collect::<Vec<char>>()[..]
{
match self.engine.choose_move(
&board::Color::parse(color_char).map_err(|_| {
format!("Couldn't parse color code {}", color_char)
})?,
) {
Some((source, target)) => {
Ok(format!("{},{}", source, target))
}
None => {
Err(format!("No move possible for {}", color_char))
}
}
} else {
Err(format!("Invalid color string {}", color_str))
}
} else {
Err("choose_move takes 1 arg".to_owned())
}
}
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
@@ -218,6 +240,7 @@ impl Ui {
"get_moves" => self.get_moves(&args),
"make_move" => self.make_move(&args),
"set_state" => self.set_state(&args),
"choose_move" => self.choose_move(&args),
"" => Err("No command given".to_owned()),
_ => Err("Invalid command".to_owned()),
}