a
This commit is contained in:
30
src/board.rs
30
src/board.rs
@@ -39,6 +39,14 @@ impl Board {
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn starting_pos(mut self) -> Self {
|
||||
self.place_unchecked(3, 3, Piece::White);
|
||||
self.place_unchecked(4, 3, Piece::Black);
|
||||
self.place_unchecked(3, 4, Piece::Black);
|
||||
self.place_unchecked(4, 4, Piece::White);
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to a place on the [`Board`]
|
||||
/// at (i, j)
|
||||
pub const fn get_mut(&mut self, i: usize, j: usize) -> &mut Option<Piece> {
|
||||
@@ -55,13 +63,21 @@ impl Board {
|
||||
*self.get_mut(i, j) = Some(piece);
|
||||
}
|
||||
|
||||
pub fn test_prop(&self, i: usize, j: usize, piece: Piece) -> bool {
|
||||
let mut self_copy = self.clone();
|
||||
if self_copy.get(i, j).is_some() {
|
||||
return false;
|
||||
}
|
||||
self_copy.place_and_prop_unchecked(i, j, piece)
|
||||
}
|
||||
|
||||
/// Propegate piece captures originating from (i, j)
|
||||
/// DO NOT USE THIS ALONE, this should be called as a part of
|
||||
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
|
||||
fn propegate_from(&mut self, i: usize, j: usize) {
|
||||
fn propegate_from(&mut self, i: usize, j: usize) -> bool {
|
||||
// returns if that place is empty
|
||||
let Some(starting_color) = *self.get(i, j) else {
|
||||
return;
|
||||
return false;
|
||||
};
|
||||
|
||||
// Create all chains from the piece being propegated from in `i` and `j` coordinates
|
||||
@@ -69,6 +85,7 @@ impl Board {
|
||||
.into_iter()
|
||||
.map(|range| range.into_iter().map(|i| (i, j)).collect())
|
||||
.collect();
|
||||
|
||||
chains.extend(
|
||||
split_from(0, BOARD_SIZE - 1, j)
|
||||
.into_iter()
|
||||
@@ -80,6 +97,7 @@ impl Board {
|
||||
let Some(piece) = self.get(new_i, new_j) else {
|
||||
break;
|
||||
};
|
||||
|
||||
if piece == &starting_color {
|
||||
if chain_length > 0 {
|
||||
// fill all opposite colors with this color
|
||||
@@ -91,18 +109,20 @@ impl Board {
|
||||
for &(i_o, j_o) in history {
|
||||
self.place_and_prop_unchecked(i_o, j_o, starting_color);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// either the other pieces were replaced, or this was an invalid chain,
|
||||
// in both cases, the loop needs to be breaked
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn place_and_prop_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
|
||||
fn place_and_prop_unchecked(&mut self, i: usize, j: usize, piece: Piece) -> bool {
|
||||
self.place_unchecked(i, j, piece);
|
||||
self.propegate_from(i, j);
|
||||
self.propegate_from(i, j)
|
||||
}
|
||||
|
||||
/// Place a piece on the [`Board`]
|
||||
|
||||
Reference in New Issue
Block a user