dedup code in Board::what_if

This commit is contained in:
Simon Gardling 2025-02-19 00:25:47 -05:00
parent 876d829e5e
commit fef4f64ecd
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -234,18 +234,13 @@ impl Board {
/// Return a modified [`Board`] with the piece placed at a position
/// Returns None if the move was invalid
pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Option<Self> {
// extract check here to avoid copy
if self.get(i, j).is_some() {
return None;
}
let mut self_copy = *self;
self_copy.place_unchecked(i, j, piece);
let how_many_prop = self_copy.propegate_from(i, j);
if how_many_prop == 0 {
return None;
}
Some(self_copy)
self_copy.place(i, j, piece).ok().map(|_| self_copy)
}
/// Returns a bool which represents whether or not a move would propegate and be valid