add back functionality

This commit is contained in:
2025-04-10 14:18:50 -04:00
parent 80db0a872d
commit fc8c2b8178
3 changed files with 83 additions and 39 deletions

View File

@@ -3,11 +3,18 @@ use rand::prelude::*;
use std::io;
use std::io::prelude::*;
#[derive(Debug, Clone, Copy)]
pub enum AgentMove {
CoordPair(CoordPair),
Back,
Skip,
}
#[allow(dead_code)]
// implements `Send` so we can use it in a multithreaded `EloArena`
pub trait Agent: Send {
/// Returns the move of an [`Agent`]
fn next_move(&mut self, board: &Board) -> Option<CoordPair>;
fn next_move(&mut self, board: &Board) -> AgentMove;
/// Returns the name of the [`Agent`]
fn name(&self) -> &'static str;
/// Returns the color the [`Agent`] is playing
@@ -28,16 +35,23 @@ impl ManualAgent {
#[allow(dead_code)]
impl Agent for ManualAgent {
fn next_move(&mut self, board: &Board) -> Option<CoordPair> {
fn next_move(&mut self, board: &Board) -> AgentMove {
let stdin = io::stdin();
let mut input = String::new();
println!("Your turn! ('Skip' to skip)");
println!("Your turn! ('Skip' to skip 'back' to go back a move)");
loop {
input.clear();
stdin.lock().read_line(&mut input).ok()?;
if stdin.lock().read_line(&mut input).is_err() {
return AgentMove::Skip;
};
if input.to_lowercase().trim() == "skip" {
// skips move
return None;
return AgentMove::Skip;
}
if input.to_lowercase().trim() == "back" {
// skips move
return AgentMove::Back;
}
let got = input
@@ -53,7 +67,7 @@ impl Agent for ManualAgent {
println!("Invalid move! Try again.");
continue;
}
return Some(got.into());
return AgentMove::CoordPair(got.into());
}
}
}
@@ -74,8 +88,11 @@ pub struct RandomAgent {
#[allow(dead_code)]
impl Agent for RandomAgent {
fn next_move(&mut self, board: &Board) -> Option<CoordPair> {
board.possible_moves(self.color).choose(&mut rand::rng())
fn next_move(&mut self, board: &Board) -> AgentMove {
match board.possible_moves(self.color).choose(&mut rand::rng()) {
Some(x) => AgentMove::CoordPair(x),
None => AgentMove::Skip,
}
}
fn name(&self) -> &'static str {