some changes
This commit is contained in:
52
src/board.rs
52
src/board.rs
@@ -190,7 +190,7 @@ impl Board {
|
||||
|
||||
/// Returns a bool which represents whether or not a move would propegate and be valid
|
||||
pub fn would_prop(&self, i: usize, j: usize, piece: Piece) -> bool {
|
||||
self.get(i, j).is_none() && !self.propegate_from_dry(i, j, piece).is_empty()
|
||||
self.get(i, j).is_none() && self.propegate_from_dry(i, j, piece).next().is_some()
|
||||
}
|
||||
|
||||
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), &'static str> {
|
||||
@@ -207,7 +207,7 @@ impl Board {
|
||||
return 0;
|
||||
};
|
||||
|
||||
let pos_s = self.propegate_from_dry(i, j, starting_color);
|
||||
let pos_s: Vec<(usize, usize)> = self.propegate_from_dry(i, j, starting_color).collect();
|
||||
let pos_len = pos_s.len();
|
||||
for (i, j) in pos_s {
|
||||
self.place_unchecked(i, j, starting_color);
|
||||
@@ -219,40 +219,36 @@ impl Board {
|
||||
/// 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_dry(&self, i: usize, j: usize, starting_color: Piece) -> Vec<(usize, usize)> {
|
||||
// Longest chain is (BOARD_SIZE - 2) as there needs to be the two pieces containing it
|
||||
let mut fill: Vec<(usize, usize)> = Vec::with_capacity((BOARD_SIZE - 2) * 8);
|
||||
|
||||
for chain in &ADJ_LOOKUP[i][j] {
|
||||
for (chain_length, &(new_i, new_j)) in chain.iter().enumerate() {
|
||||
let Some(piece) = self.get(new_i, new_j) else {
|
||||
// chain interupted by blank space
|
||||
break;
|
||||
};
|
||||
|
||||
if piece == starting_color {
|
||||
// Chain is only ever added to `fill` if it is completed
|
||||
|
||||
if let Some(history) = chain.get(..chain_length) {
|
||||
// fill all opposite colors with this color
|
||||
fill.extend(history);
|
||||
fn propegate_from_dry(
|
||||
&self,
|
||||
i: usize,
|
||||
j: usize,
|
||||
starting_color: Piece,
|
||||
) -> impl Iterator<Item = (usize, usize)> + use<'_> {
|
||||
ADJ_LOOKUP[i][j]
|
||||
.iter()
|
||||
.filter_map(move |chain| {
|
||||
let mut end_idx = None;
|
||||
for (idx, &(new_i, new_j)) in chain.iter().enumerate() {
|
||||
let piece = self.get(new_i, new_j)?;
|
||||
if piece == starting_color {
|
||||
end_idx = Some(idx);
|
||||
break;
|
||||
}
|
||||
|
||||
// either the other pieces were replaced, or this was an invalid chain,
|
||||
// in both cases, the loop needs to be broken
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
fill
|
||||
end_idx.map(|idx| &chain[..idx])
|
||||
})
|
||||
.flatten()
|
||||
.cloned()
|
||||
}
|
||||
|
||||
/// Count the number of a type of [`Piece`] on the board
|
||||
pub fn count(&self, piece: Piece) -> usize {
|
||||
match piece {
|
||||
Piece::Black => self.black_board.count(),
|
||||
Piece::White => self.white_board.count(),
|
||||
Piece::Black => self.black_board,
|
||||
Piece::White => self.white_board,
|
||||
}
|
||||
.count()
|
||||
}
|
||||
|
||||
/// Returns (White score, Black score)
|
||||
|
||||
Reference in New Issue
Block a user