improve prop logic

This commit is contained in:
Simon Gardling 2025-01-24 21:58:38 -05:00
parent 7cfd5b5733
commit 283158b00f
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -30,12 +30,11 @@ impl Board {
&self.board[i][j]
}
fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
const fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
*self.get_mut(i, j) = Some(piece);
}
// TODO! make this better and actually readable
// TODO! propegate from propegated pieces
fn propegate_from(&mut self, i: usize, j: usize) {
// returns if that place is empty
let Some(starting_color) = *self.get(i, j) else {
@ -50,44 +49,46 @@ impl Board {
for i_range in i_ranges {
let mut chain_length: usize = 0;
for new_i in i_range.clone() {
match self.get(new_i, j) {
Some(piece) => {
if piece == &starting_color {
if chain_length > 0 {
// fill all opposite colors with this color
i_range.get(..chain_length).unwrap().iter().for_each(|i_o| {
self.place_and_prop_unchecked(*i_o, j, starting_color)
});
}
for &new_i in &i_range {
let Some(piece) = self.get(new_i, j) else {
break;
};
if piece == &starting_color {
if chain_length > 0 {
// fill all opposite colors with this color
let Some(i_o_s) = i_range.get(..chain_length) else {
break;
} else {
chain_length += 1;
};
for i_o in i_o_s {
self.place_and_prop_unchecked(*i_o, j, starting_color);
}
}
None => break,
break;
} else {
chain_length += 1;
}
}
}
for j_range in j_ranges {
let mut chain_length: usize = 0;
for new_j in j_range.clone() {
match self.get(i, new_j) {
Some(piece) => {
if piece == &starting_color {
if chain_length > 0 {
// fill all opposite colors with this color
j_range.get(..chain_length).unwrap().iter().for_each(|j_o| {
self.place_and_prop_unchecked(i, *j_o, starting_color)
});
}
for &new_j in &j_range {
let Some(piece) = self.get(i, new_j) else {
break;
};
if piece == &starting_color {
if chain_length > 0 {
let Some(j_o_s) = j_range.get(..chain_length) else {
break;
} else {
chain_length += 1;
};
// fill all opposite colors with this color
for j_o in j_o_s {
self.place_and_prop_unchecked(i, *j_o, starting_color);
}
}
None => break,
break;
} else {
chain_length += 1;
}
}
}
@ -114,6 +115,7 @@ impl Board {
#[cfg(test)]
mod test {
use super::*;
#[test]
fn place_and_get() {
let mut board = Board::new();