add double prop test

This commit is contained in:
Simon Gardling 2025-01-28 11:45:53 -05:00
parent d807b1bb34
commit c48a4595c3
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -79,6 +79,7 @@ impl Board {
return; return;
}; };
// 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 mut chains: Vec<Vec<(usize, usize)>> = split_from(0, BOARD_SIZE - 1, i)
.into_iter() .into_iter()
.map(|range| range.into_iter().map(|i| (i, j)).collect()) .map(|range| range.into_iter().map(|i| (i, j)).collect())
@ -87,7 +88,7 @@ impl Board {
&mut split_from(0, BOARD_SIZE - 1, j) &mut split_from(0, BOARD_SIZE - 1, j)
.into_iter() .into_iter()
.map(|range| range.into_iter().map(|j| (i, j)).collect()) .map(|range| range.into_iter().map(|j| (i, j)).collect())
.collect::<Vec<Vec<(usize, usize)>>>(), .collect(),
); );
for chain in chains { for chain in chains {
@ -100,15 +101,17 @@ impl Board {
if piece == &starting_color { if piece == &starting_color {
if chain_length > 0 { if chain_length > 0 {
// fill all opposite colors with this color // 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; break;
}; };
// fill all opposite colors with this color // 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); 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; break;
} }
chain_length += 1; chain_length += 1;
@ -138,8 +141,6 @@ impl Board {
mod test { mod test {
use super::*; use super::*;
// TODO! add tests for double prop
#[test] #[test]
fn place_and_get() { fn place_and_get() {
let mut board = Board::new(); 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));
}
} }