This commit is contained in:
Simon Gardling 2025-01-29 10:30:49 -05:00
parent 7aebdc3c78
commit db42964a47
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 28 additions and 17 deletions

View File

@ -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<Vec<(usize, usize)>> = 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<Vec<(usize, usize)>> = 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

View File

@ -6,6 +6,7 @@ where
RangeInclusive<T>: Iterator<Item = T> + DoubleEndedIterator,
Rev<RangeInclusive<T>>: Iterator<Item = T>,
{
// 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::<i16>(-1, 4, 10), Vec::<Vec<i16>>::new());
assert_eq!(
split_from::<i16>(-1i16, 4i16, 10i16),
Vec::<Vec<i16>>::new()
);
}
}