diff --git a/src/board.rs b/src/board.rs index 8dee53a..842c3fd 100644 --- a/src/board.rs +++ b/src/board.rs @@ -63,21 +63,38 @@ impl Board { *self.get_mut(i, j) = Some(piece); } - pub fn test_prop(&self, i: usize, j: usize, piece: Piece) -> bool { + 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.clone(); if self_copy.get(i, j).is_some() { - return false; + return None; } - self_copy.place_and_prop_unchecked(i, j, piece) + self_copy.place_unchecked(i, j, piece); + let how_many_prop = self_copy.propegate_from(i, j); + Some((self_copy, how_many_prop)) + } + + pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), String> { + let what_if_result = self.what_if(i, j, piece); + if let Some(what_if_result) = what_if_result { + if what_if_result.1 > 0 { + *self = what_if_result.0; + return Ok(()); + } + } + Err(format!("move would not propegate")) } /// Propegate piece captures originating from (i, j) /// DO NOT USE THIS ALONE, this should be called as a part of /// [`Board::place`] or [`Board::place_and_prop_unchecked`] - fn propegate_from(&mut self, i: usize, j: usize) -> bool { + fn propegate_from(&mut self, i: usize, j: usize) -> usize { // returns if that place is empty let Some(starting_color) = *self.get(i, j) else { - return false; + return 0; }; // Create all chains from the piece being propegated from in `i` and `j` coordinates @@ -92,6 +109,8 @@ impl Board { .map(|range| range.into_iter().map(|j| (i, j)).collect()), ); + let mut captured: usize = 0; + for chain in chains { for (chain_length, &(new_i, new_j)) in chain.iter().enumerate() { let Some(piece) = self.get(new_i, new_j) else { @@ -107,34 +126,17 @@ impl Board { // fill all opposite colors with this color for &(i_o, j_o) in history { - self.place_and_prop_unchecked(i_o, j_o, starting_color); + self.place_unchecked(i_o, j_o, starting_color); + captured += self.propegate_from(i_o, j_o) + 1; } - return true; } // either the other pieces were replaced, or this was an invalid chain, // in both cases, the loop needs to be breaked - return false; + break; } } } - false - } - - fn place_and_prop_unchecked(&mut self, i: usize, j: usize, piece: Piece) -> bool { - self.place_unchecked(i, j, piece); - self.propegate_from(i, j) - } - - /// Place a piece on the [`Board`] - /// Returns an error if there already exists a piece there - pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), String> { - if self.get(i, j).is_some() { - return Err(format!("Cannot place on ({},{}), piece exists", i, j)); - } else { - self.place_and_prop_unchecked(i, j, piece); - } - - Ok(()) + captured } } diff --git a/src/complexagent.rs b/src/complexagent.rs index ae36b77..f985ddb 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -14,13 +14,15 @@ pub struct ComplexAgent { impl Agent for ComplexAgent { fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> { (0..BOARD_SIZE) - .zip(0..BOARD_SIZE) - .filter(|&(i, j)| board.test_prop(i, j, self.color)) - // .filter(|&(i, j)| board.get(i, j) == &Some(!self.color)) - // .flat_map(|(i, j)| offsets(i, j, 1)) - // .filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE) - // .filter(|&(i, j)| board.get(i, j).is_none()) - .choose(&mut self.rng) + .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) + .max_by_key(|&(_, _, c)| c) + .map(|(i, j, _)| (i, j)) } fn name(&self) -> &'static str {