make complexagent so much better
This commit is contained in:
@@ -2,8 +2,10 @@ use crate::{agent::Agent, board::Board, piece::Piece};
|
||||
|
||||
pub struct ComplexAgent {
|
||||
color: Piece,
|
||||
curr_move: Option<Move>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Move {
|
||||
i: usize,
|
||||
j: usize,
|
||||
@@ -44,6 +46,10 @@ impl Move {
|
||||
|
||||
value
|
||||
}
|
||||
|
||||
pub const fn coords(&self) -> (usize, usize) {
|
||||
(self.i, self.j)
|
||||
}
|
||||
}
|
||||
|
||||
fn problem_space(board: &Board, color: Piece) -> Box<dyn Iterator<Item = Move> + '_> {
|
||||
@@ -65,14 +71,38 @@ fn problem_space(board: &Board, color: Piece) -> Box<dyn Iterator<Item = Move> +
|
||||
impl Agent for ComplexAgent {
|
||||
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
||||
const LOOPS: usize = 5;
|
||||
problem_space(board, self.color)
|
||||
.map(|mut x| {
|
||||
x.populate_next_moves(LOOPS);
|
||||
x
|
||||
})
|
||||
.map(|m| ((m.i, m.j), m.value(self.color)))
|
||||
.max_by_key(|a| a.1)
|
||||
.map(|a| a.0)
|
||||
|
||||
let curr_move: Move = self.curr_move.take().unwrap_or_else(|| Move {
|
||||
i: 0,
|
||||
j: 0,
|
||||
captured: 0,
|
||||
color: self.color,
|
||||
board: *board,
|
||||
next_move: vec![Move {
|
||||
i: 0,
|
||||
j: 0,
|
||||
captured: 0,
|
||||
color: !self.color,
|
||||
board: *board,
|
||||
next_move: problem_space(board, self.color).collect(),
|
||||
}],
|
||||
});
|
||||
|
||||
let mut other_player_move = curr_move
|
||||
.next_move
|
||||
.into_iter()
|
||||
.filter(|x| &x.board == board)
|
||||
.last()
|
||||
.unwrap();
|
||||
|
||||
other_player_move.populate_next_moves(LOOPS);
|
||||
|
||||
self.curr_move = other_player_move
|
||||
.next_move
|
||||
.into_iter()
|
||||
.max_by_key(|m| m.value(self.color));
|
||||
|
||||
self.curr_move.as_ref().map(Move::coords)
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
@@ -86,6 +116,9 @@ impl Agent for ComplexAgent {
|
||||
|
||||
impl ComplexAgent {
|
||||
pub fn new(color: Piece) -> Self {
|
||||
Self { color }
|
||||
Self {
|
||||
color,
|
||||
curr_move: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user