From fef4f64ecd66851176ae593284fe1fcea5303c24 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 19 Feb 2025 00:25:47 -0500 Subject: [PATCH] dedup code in Board::what_if --- src/board.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/board.rs b/src/board.rs index 58efbd1..c7a0a89 100644 --- a/src/board.rs +++ b/src/board.rs @@ -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 { + // 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