actually go N deep

This commit is contained in:
Simon Gardling 2025-01-30 16:17:29 -05:00
parent 1fc731ac59
commit 472141533b
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 60 additions and 17 deletions

View File

@ -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<Move>,
}
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<Move> {
(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::<Vec<(usize, usize)>>()
})
.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 {

View File

@ -53,7 +53,7 @@ impl Game {
self.step(i);
std::thread::sleep(Duration::from_millis(200));
// std::thread::sleep(Duration::from_millis(200));
}
}
}