diff --git a/src/repr.rs b/src/repr.rs index 4db7a30..476290e 100644 --- a/src/repr.rs +++ b/src/repr.rs @@ -79,6 +79,7 @@ impl Board { return; }; + // 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) .into_iter() .map(|range| range.into_iter().map(|i| (i, j)).collect()) @@ -87,7 +88,7 @@ impl Board { &mut split_from(0, BOARD_SIZE - 1, j) .into_iter() .map(|range| range.into_iter().map(|j| (i, j)).collect()) - .collect::>>(), + .collect(), ); for chain in chains { @@ -100,15 +101,17 @@ impl Board { if piece == &starting_color { if chain_length > 0 { // fill all opposite colors with this color - let Some(i_o_s) = chain.get(..chain_length) else { + let Some(history) = chain.get(..chain_length) else { break; }; // fill all opposite colors with this color - for &(i_o, j_o) in i_o_s { + for &(i_o, j_o) in history { self.place_and_prop_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 break; } chain_length += 1; @@ -138,8 +141,6 @@ impl Board { mod test { use super::*; - // TODO! add tests for double prop - #[test] fn place_and_get() { let mut board = Board::new(); @@ -235,4 +236,46 @@ mod test { ); } } + + #[test] + fn double_prop() { + let mut board = Board::new(); + assert_eq!(board.place(0, 0, Piece::Black), Ok(())); + + assert_eq!(board.place(0, 1, Piece::White), Ok(())); + + // ----------------- + // |□|■| | | | | | | + // ----------------- + // | | | | | | | | | + + assert_eq!(board.place(1, 1, Piece::White), Ok(())); + + // ----------------- + // |□|■| | | | | | | + // ----------------- + // | |■| | | | | | | + + assert_eq!(board.place(2, 1, Piece::Black), Ok(())); + + // ----------------- + // |□|■| | | | | | | + // ----------------- + // | |■| | | | | | | + // ----------------- + // | |□| | | | | | | + + assert_eq!(board.place(0, 2, Piece::Black), Ok(())); + + // ----------------- + // |□|□|□| | | | | | + // ----------------- + // | |□| | | | | | | + // ----------------- + // | |□| | | | | | | + + assert_eq!(board.get(0, 1), &Some(Piece::Black)); + + assert_eq!(board.get(1, 1), &Some(Piece::Black)); + } }