Improved diffuse agent

master
Mark 2024-03-05 10:18:19 -08:00
parent 3e080ad89d
commit fb69990b9d
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
1 changed files with 22 additions and 7 deletions

View File

@ -1,6 +1,6 @@
use anyhow::Result;
use super::{MinimizerAgent, RandomAgent};
use super::{MaximizerAgent, MinimizerAgent, SimpleMinimax};
use crate::{
board::{Board, PlayerAction},
util::Symb,
@ -9,9 +9,9 @@ use crate::{
/// A simple "operator diffusion" MINIMIZER agent.
///
/// Tries to keep operators as far apart as possible, denying large numbers.
pub struct DiffuseAgent {}
pub struct Diffuse {}
impl DiffuseAgent {
impl Diffuse {
/// Place a symbol on the board.
/// Assumes `symb` is not already on the board
fn step_symb(&self, board: &Board, symb: Symb) -> PlayerAction {
@ -25,8 +25,8 @@ impl DiffuseAgent {
// Set up initial distances
dist[0] = 1;
*dist.last_mut().unwrap() = 1;
for (i, o) in board.iter().enumerate() {
if let Some((s, _)) = o {
for (i, o) in board.get_board().iter().enumerate() {
if let Some(s) = o {
if s.is_op() {
dist[i] = 0
}
@ -68,7 +68,7 @@ impl DiffuseAgent {
}
}
impl MinimizerAgent for DiffuseAgent {
impl MinimizerAgent for Diffuse {
fn step_min(&mut self, board: &Board) -> Result<PlayerAction> {
let symb = [Symb::Minus, Symb::Times, Symb::Plus, Symb::Div]
.iter()
@ -78,7 +78,22 @@ impl MinimizerAgent for DiffuseAgent {
Ok(self.step_symb(board, *symb))
} else {
// No symbols available, play a random number
RandomAgent {}.step_min(board)
SimpleMinimax {}.step_min(board)
}
}
}
impl MaximizerAgent for Diffuse {
fn step_max(&mut self, board: &Board) -> Result<PlayerAction> {
let symb = [Symb::Minus, Symb::Times, Symb::Plus, Symb::Div]
.iter()
.find(|x| !board.contains(**x));
if let Some(symb) = symb {
Ok(self.step_symb(board, *symb))
} else {
// No symbols available, play a random number
SimpleMinimax {}.step_max(board)
}
}
}