From 283158b00f276366f061c3bb85d5075a9900fa47 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 24 Jan 2025 21:58:38 -0500 Subject: [PATCH] improve prop logic --- src/repr.rs | 58 +++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/repr.rs b/src/repr.rs index 3195f03..98d1037 100644 --- a/src/repr.rs +++ b/src/repr.rs @@ -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();