diff --git a/src/complexagent.rs b/src/complexagent.rs index e6c3314..f726633 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use crate::{ agent::Agent, board::{Board, BOARD_SIZE}, @@ -8,7 +10,46 @@ pub struct ComplexAgent { color: Piece, } -fn problem_space(board: &Board, piece: Piece) -> Vec<(usize, usize, (Board, usize))> { +struct Move { + i: usize, + j: usize, + captured: usize, + color: Piece, + board: Board, + next_move: Vec, +} + +impl Move { + fn populate_next_moves(&mut self, i: usize) { + if i == 0 { + return; + } + self.next_move = problem_space(&self.board, !self.color); + self.next_move + .iter_mut() + .for_each(|x| x.populate_next_moves(i - 1)); + } + + fn value(&self, from_perspective_of: Piece) -> i64 { + let captured_value = if from_perspective_of != self.color { + -(self.captured as i64) + } else { + self.captured as i64 + }; + + let value = captured_value + + self + .next_move + .iter() + .map(|x| x.value(from_perspective_of)) + .max() + .unwrap_or(0); + + value + } +} + +fn problem_space(board: &Board, piece: Piece) -> Vec { (0..BOARD_SIZE) .flat_map(|i| { (0..BOARD_SIZE) @@ -16,27 +57,29 @@ fn problem_space(board: &Board, piece: Piece) -> Vec<(usize, usize, (Board, usiz .collect::>() }) .flat_map(|(i, j)| board.what_if(i, j, piece).map(|x| (i, j, x))) + .map(|(i, j, (b, c))| Move { + i, + j, + captured: c, + color: piece, + board: b, + next_move: Vec::new(), + }) .collect() } impl Agent for ComplexAgent { fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> { - problem_space(board, self.color) - .iter() - .map(|&(i, j, (board, c))| { - ( - i, - j, - c as i64 - // reduce # of adversarial moves - - problem_space(&board, self.color.flip()) - .into_iter() - .map(|(_, _, (_, c))| c as i64) - .count() as i64, - ) + const LOOPS: usize = 5; + problem_space(&board, self.color) + .into_iter() + .map(|mut x| { + x.populate_next_moves(LOOPS); + x }) - .max_by_key(|&(_, _, c)| c) - .map(|(i, j, _)| (i, j)) + .map(|m| ((m.i, m.j), m.value(self.color))) + .max_by_key(|&(_, c)| c) + .map(|(x, _)| x) } fn name(&self) -> &'static str { diff --git a/src/game.rs b/src/game.rs index 0a11e26..e47247d 100644 --- a/src/game.rs +++ b/src/game.rs @@ -53,7 +53,7 @@ impl Game { self.step(i); - std::thread::sleep(Duration::from_millis(200)); + // std::thread::sleep(Duration::from_millis(200)); } } }