Board::what_if use Result

This commit is contained in:
Simon Gardling 2025-02-19 13:43:39 -05:00
parent 6f51c31752
commit d7a94c2d57
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -234,14 +234,14 @@ 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> {
pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Result<Self, &'static str> {
// extract check here to avoid copy
if self.get(i, j).is_some() {
return None;
return Err("position is occupied");
}
let mut self_copy = *self;
self_copy.place(i, j, piece).ok().map(|_| self_copy)
self_copy.place(i, j, piece).map(|_| self_copy)
}
/// Returns a bool which represents whether or not a move would propegate and be valid