fix diagonals and user input

This commit is contained in:
2025-01-30 15:53:02 -05:00
parent db42964a47
commit 1fc731ac59
3 changed files with 52 additions and 4 deletions

View File

@@ -1,4 +1,6 @@
use crate::{board::Board, piece::Piece};
use std::io;
use std::io::prelude::*;
pub trait Agent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)>;
@@ -12,7 +14,23 @@ pub struct ManualAgent {
impl Agent for ManualAgent {
fn next_move(&mut self, _: &Board) -> Option<(usize, usize)> {
todo!("user input not implemented")
let stdin = io::stdin();
let mut input = String::new();
let mut got: Option<(usize, usize)> = None;
while got.is_none() {
input.clear();
stdin.lock().read_line(&mut input).ok()?;
let numbers = input
.split_whitespace()
.map(|i| usize::from_str_radix(i, 10).ok())
.collect::<Option<Vec<usize>>>();
if let Some(numbers) = numbers {
if numbers.len() == 2 {
got = Some((numbers[0], numbers[1]));
}
}
}
got
}
fn name(&self) -> &'static str {
@@ -23,3 +41,9 @@ impl Agent for ManualAgent {
self.color
}
}
impl ManualAgent {
pub const fn new(color: Piece) -> Self {
Self { color }
}
}