othello/src/complexagent.rs
2025-01-28 16:20:01 -05:00

39 lines
975 B
Rust

use crate::{
agent::Agent,
board::{Board, BOARD_SIZE},
piece::Piece,
};
pub struct ComplexAgent {
color: Piece,
}
impl Agent for ComplexAgent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
(0..BOARD_SIZE)
.flat_map(|i| {
(0..BOARD_SIZE)
.map(|j| (i, j))
.collect::<Vec<(usize, usize)>>()
})
.map(|(i, j)| (i, j, board.move_would_propegate(i, j, self.color)))
.filter(|&(_, _, c)| c >= 0) // a `c` value of less than 0 implies the move is invalid: TODO! make this an enum or smth
.max_by_key(|&(_, _, c)| c)
.map(|(i, j, _)| (i, j)) // remove `c` and return the best move
}
fn name(&self) -> &'static str {
"Complex Agent"
}
fn color(&self) -> Piece {
self.color
}
}
impl ComplexAgent {
pub fn new(color: Piece) -> Self {
Self { color }
}
}