iterator fixes

This commit is contained in:
2025-02-06 16:13:11 -05:00
parent 1bed0b61ad
commit 3681fd7e41
4 changed files with 35 additions and 41 deletions

View File

@@ -1,8 +1,4 @@
use crate::{
agent::Agent,
board::{Board, BOARD_SIZE},
piece::Piece,
};
use crate::{agent::Agent, board::Board, piece::Piece};
pub struct ComplexAgent {
color: Piece,
@@ -22,7 +18,10 @@ impl Move {
if i == 0 {
return;
}
self.next_move = problem_space(&self.board, !self.color);
if self.next_move.is_empty() {
self.next_move = problem_space(&self.board, !self.color).collect();
}
self.next_move
.iter_mut()
.for_each(|x| x.populate_next_moves(i - 1));
@@ -47,37 +46,33 @@ impl Move {
}
}
fn problem_space(board: &Board, color: Piece) -> Vec<Move> {
(0..BOARD_SIZE)
.flat_map(|i| {
(0..BOARD_SIZE)
.map(|j| (i, j))
.collect::<Vec<(usize, usize)>>()
})
.flat_map(|(i, j)| board.what_if(i, j, color).map(|x| (i, j, x)))
.map(|(i, j, (board, captured))| Move {
i,
j,
captured,
color,
board,
next_move: Vec::new(),
})
.collect()
fn problem_space(board: &Board, color: Piece) -> Box<dyn Iterator<Item = Move> + '_> {
Box::new(
board
.possible_moves(color)
.flat_map(move |(i, j)| board.what_if(i, j, color).map(|x| (i, j, x)))
.map(move |(i, j, (board, captured))| Move {
i,
j,
captured,
color,
board,
next_move: Vec::new(),
}),
)
}
impl Agent for ComplexAgent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
const LOOPS: usize = 5;
problem_space(board, self.color)
.into_iter()
.map(|mut x| {
x.populate_next_moves(LOOPS);
x
})
.map(|m| ((m.i, m.j), m.value(self.color)))
.max_by_key(|&(_, c)| c)
.map(|(x, _)| x)
.max_by_key(|a| a.1)
.map(|a| a.0)
}
fn name(&self) -> &'static str {