From 37c3f8572f33874a55139307f4117947b3d8a00c Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 28 Jan 2025 16:44:28 -0500 Subject: [PATCH] things --- src/board.rs | 4 ---- src/complexagent.rs | 26 ++++++++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/board.rs b/src/board.rs index 5fe07a8..4935e15 100644 --- a/src/board.rs +++ b/src/board.rs @@ -63,10 +63,6 @@ impl Board { *self.get_mut(i, j) = Some(piece); } - pub fn move_would_propegate(&self, i: usize, j: usize, piece: Piece) -> i16 { - self.what_if(i, j, piece).map(|x| x.1 as i16).unwrap_or(-1) - } - pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Option<(Self, usize)> { let mut self_copy = *self; if self_copy.get(i, j).is_some() { diff --git a/src/complexagent.rs b/src/complexagent.rs index fe273ac..aef764d 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -8,18 +8,24 @@ pub struct ComplexAgent { color: Piece, } +fn problem_space(board: &Board, piece: Piece) -> Vec<(usize, usize, (Board, usize))> { + (0..BOARD_SIZE) + .flat_map(|i| { + (0..BOARD_SIZE) + .map(|j| (i, j)) + .collect::>() + }) + .flat_map(|(i, j)| board.what_if(i, j, piece).map(|x| (i, j, x))) + .map(|(i, j, (board, c))| (i, j, (board, c as usize))) + .collect() +} + 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::>() - }) - .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 + problem_space(board, self.color) + .iter() + .max_by_key(|(_, _, (_, c))| c) + .map(|&(i, j, (_, _))| (i, j)) } fn name(&self) -> &'static str {