This commit is contained in:
2025-01-28 15:05:32 -05:00
parent 565f638b1b
commit 9f6be9b669
8 changed files with 317 additions and 29 deletions

43
src/complexagent.rs Normal file
View File

@@ -0,0 +1,43 @@
use crate::{
agent::Agent,
board::{Board, BOARD_SIZE},
misc::{offsets, split_from},
piece::Piece,
};
use rand::{seq::IteratorRandom, Rng};
pub struct ComplexAgent {
color: Piece,
}
impl Agent for ComplexAgent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
(0..BOARD_SIZE)
.zip(0..BOARD_SIZE)
.filter(|&(i, j)| board.get(i, j) == &Some(!self.color()))
.flat_map(|(i, j)| offsets(i, j, 1))
.filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE)
.filter(|&(i, j)| board.get(i, j).is_none())
.choose(&mut rand::rng())
.or_else(|| {
(0..BOARD_SIZE)
.zip(0..BOARD_SIZE)
.filter(|&(i, j)| board.get(i, j).is_none())
.choose(&mut rand::rng())
})
}
fn name(&self) -> &'static str {
"Complex Agent"
}
fn color(&self) -> Piece {
self.color
}
}
impl ComplexAgent {
pub const fn new(color: Piece) -> Self {
Self { color }
}
}