share rng thread

This commit is contained in:
Simon Gardling 2025-01-28 15:08:56 -05:00
parent f18fedd5cc
commit aab639c04d
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -4,26 +4,27 @@ use crate::{
misc::offsets,
piece::Piece,
};
use rand::seq::IteratorRandom;
use rand::{rngs::ThreadRng, seq::IteratorRandom};
pub struct ComplexAgent {
color: Piece,
rng: ThreadRng,
}
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()))
.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())
.choose(&mut self.rng)
.or_else(|| {
(0..BOARD_SIZE)
.zip(0..BOARD_SIZE)
.filter(|&(i, j)| board.get(i, j).is_none())
.choose(&mut rand::rng())
.choose(&mut self.rng)
})
}
@ -37,7 +38,10 @@ impl Agent for ComplexAgent {
}
impl ComplexAgent {
pub const fn new(color: Piece) -> Self {
Self { color }
pub fn new(color: Piece) -> Self {
Self {
color,
rng: rand::rng(),
}
}
}