improve manual agent code

This commit is contained in:
Simon Gardling 2025-02-04 15:49:06 -05:00
parent f63d8f4415
commit 7c74d45618
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -16,22 +16,23 @@ impl Agent for ManualAgent {
fn next_move(&mut self, _: &Board) -> Option<(usize, usize)> { fn next_move(&mut self, _: &Board) -> Option<(usize, usize)> {
let stdin = io::stdin(); let stdin = io::stdin();
let mut input = String::new(); let mut input = String::new();
let mut got: Option<(usize, usize)> = None; loop {
while got.is_none() {
input.clear(); input.clear();
stdin.lock().read_line(&mut input).ok()?; stdin.lock().read_line(&mut input).ok()?;
let numbers = input
let got = input
.split_whitespace() .split_whitespace()
.map(|i| i.parse::<usize>().ok()) .map(str::parse)
.collect::<Option<Vec<usize>>>(); .map(Result::ok)
if let Some(numbers) = numbers { .collect::<Option<Vec<_>>>()
if numbers.len() == 2 { .and_then(|x| -> Option<[usize; 2]> { x.try_into().ok() })
got = Some((numbers[0], numbers[1])); .map(|x| (x[0], x[1]));
if got.is_some() {
return got;
} }
} }
} }
got
}
fn name(&self) -> &'static str { fn name(&self) -> &'static str {
"Manual Agent" "Manual Agent"