From db42964a4777b16028d21ef59cd5fe7d814e4d19 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 29 Jan 2025 10:30:49 -0500 Subject: [PATCH] cleanup --- src/board.rs | 39 +++++++++++++++++++++++---------------- src/misc.rs | 6 +++++- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/board.rs b/src/board.rs index f9c558c..8e468ad 100644 --- a/src/board.rs +++ b/src/board.rs @@ -64,10 +64,10 @@ impl Board { } 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() { + if self.get(i, j).is_some() { return None; } + let mut self_copy = *self; self_copy.place_unchecked(i, j, piece); let how_many_prop = self_copy.propegate_from(i, j); if how_many_prop == 0 { @@ -96,21 +96,29 @@ impl Board { }; // Create all chains from the piece being propegated from in `i` and `j` coordinates - let mut chains: Vec> = split_from(0, BOARD_SIZE - 1, i) + let (i_chain, j_chain) = ( + split_from(0, BOARD_SIZE - 1, i), + split_from(0, BOARD_SIZE - 1, j), + ); + + let mut chains: Vec> = i_chain + .clone() .into_iter() .map(|range| range.into_iter().map(|i| (i, j)).collect()) .collect(); chains.extend( - split_from(0, BOARD_SIZE - 1, j) + j_chain + .clone() .into_iter() .map(|range| range.into_iter().map(|j| (i, j)).collect()), ); + // handle diagonals chains.extend( - split_from(0, BOARD_SIZE - 1, i) + i_chain .into_iter() - .zip(split_from(0, BOARD_SIZE - 1, j)) + .zip(j_chain) .map(|(i_vec, j_vec)| i_vec.into_iter().zip(j_vec).collect()), ); @@ -119,21 +127,20 @@ impl Board { 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 { + // chain interupted by blank space break; }; if piece == &starting_color { - if chain_length > 0 { - // fill all opposite colors with this color - let Some(history) = chain.get(..chain_length) else { - break; - }; - captured += history.len(); + // fill all opposite colors with this color + let Some(history) = chain.get(..chain_length) else { + break; + }; + captured += history.len(); - // fill all opposite colors with this color - for &(i_o, j_o) in history { - self.place_unchecked(i_o, j_o, starting_color); - } + // fill all opposite colors with this color + for &(i_o, j_o) in history { + self.place_unchecked(i_o, j_o, starting_color); } // either the other pieces were replaced, or this was an invalid chain, // in both cases, the loop needs to be breaked diff --git a/src/misc.rs b/src/misc.rs index 5630cd8..cdd8b3b 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -6,6 +6,7 @@ where RangeInclusive: Iterator + DoubleEndedIterator, Rev>: Iterator, { + // check that x is in range if min > x || x > max { return Vec::new(); } @@ -35,6 +36,9 @@ mod test { assert_eq!(split_from(0, 6, 6), vec![vec![5, 4, 3, 2, 1, 0]]); // test out-of-bounds and also generics - assert_eq!(split_from::(-1, 4, 10), Vec::>::new()); + assert_eq!( + split_from::(-1i16, 4i16, 10i16), + Vec::>::new() + ); } }