abstract away more coordinates
This commit is contained in:
14
src/agent.rs
14
src/agent.rs
@@ -1,4 +1,4 @@
|
||||
use crate::repr::{Board, Coord, Piece};
|
||||
use crate::repr::{Board, CoordAxis, CoordPair, Piece};
|
||||
use rand::prelude::*;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
@@ -6,7 +6,7 @@ use std::io::prelude::*;
|
||||
#[allow(dead_code)]
|
||||
pub trait Agent {
|
||||
/// Returns the move of an [`Agent`]
|
||||
fn next_move(&mut self, board: &Board) -> Option<(Coord, Coord)>;
|
||||
fn next_move(&mut self, board: &Board) -> Option<CoordPair>;
|
||||
/// Returns the name of the [`Agent`]
|
||||
fn name(&self) -> &'static str;
|
||||
/// Returns the color the [`Agent`] is playing
|
||||
@@ -27,7 +27,7 @@ impl ManualAgent {
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Agent for ManualAgent {
|
||||
fn next_move(&mut self, board: &Board) -> Option<(Coord, Coord)> {
|
||||
fn next_move(&mut self, board: &Board) -> Option<CoordPair> {
|
||||
let stdin = io::stdin();
|
||||
let mut input = String::new();
|
||||
println!("Your turn! ('Skip' to skip)");
|
||||
@@ -44,15 +44,15 @@ impl Agent for ManualAgent {
|
||||
.map(str::parse)
|
||||
.map(Result::ok)
|
||||
.collect::<Option<Vec<_>>>()
|
||||
.and_then(|x| -> Option<[Coord; 2]> { x.try_into().ok() })
|
||||
.and_then(|x| -> Option<[CoordAxis; 2]> { x.try_into().ok() })
|
||||
.map(|x| (x[0], x[1]));
|
||||
|
||||
if let Some(got) = got {
|
||||
if board.possible_moves(self.color).all(|x| x != got) {
|
||||
if board.possible_moves(self.color).all(|x| x != got.into()) {
|
||||
println!("Invalid move! Try again.");
|
||||
continue;
|
||||
}
|
||||
return Some(got);
|
||||
return Some(got.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,7 @@ pub struct RandomAgent {
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Agent for RandomAgent {
|
||||
fn next_move(&mut self, board: &Board) -> Option<(Coord, Coord)> {
|
||||
fn next_move(&mut self, board: &Board) -> Option<CoordPair> {
|
||||
board.possible_moves(self.color).choose(&mut rand::rng())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user