diff --git a/src/marble_engine.rs b/src/marble_engine.rs index c1e36be..7ae33e7 100644 --- a/src/marble_engine.rs +++ b/src/marble_engine.rs @@ -22,7 +22,6 @@ pub struct Machine { steps: usize, } - impl Machine { pub fn new_empty(input: Vec, width: usize) -> Self { Self { @@ -242,17 +241,20 @@ impl Machine { let Some(front_tile) = self.board.get_mut(front_pos) else { continue; }; + + let mut move_to = |tile, target_pos, dir, board: &mut Board| { + let value = match tile { + OpenTile::Blank => value, + OpenTile::Digit(n) => value.wrapping_mul(10).wrapping_add(n as MarbleValue), + }; + board.set(*pos, Tile::default()); + board.set(target_pos, Tile::Marble { value, dir }); + *pos = target_pos; + }; + if let Tile::Open(space_type, claim_state) = front_tile { if *claim_state == MarbleTarget::Claimed { - let value = match space_type { - OpenTile::Blank => value, - OpenTile::Digit(n) => { - value.wrapping_mul(10).wrapping_add(*n as MarbleValue) - } - }; - self.board.set(*pos, Tile::default()); - self.board.set(front_pos, Tile::Marble { value, dir }); - *pos = front_pos; + move_to(*space_type, front_pos, dir, &mut self.board); } else if *claim_state != MarbleTarget::Free { // (Free means a marble was just here but moved earlier this tick) // bounce on failed direct movement @@ -283,12 +285,10 @@ impl Machine { } Tile::Powerable(PTile::Bag, _) => { removed_marbles.push(i); - self.board.set(*pos, Tile::default()); continue; } Tile::Powerable(PTile::IO, _) => { removed_marbles.push(i); - self.board.set(*pos, Tile::default()); self.output.push(value as u8); continue; } @@ -298,21 +298,7 @@ impl Machine { continue; }; if let Tile::Open(space_type, MarbleTarget::ClaimedIndirect) = target_tile { - let value = match space_type { - OpenTile::Blank => value, - OpenTile::Digit(n) => { - value.wrapping_mul(10).wrapping_add(*n as MarbleValue) - } - }; - self.board.set(*pos, Tile::default()); - self.board.set( - target_pos, - Tile::Marble { - value, - dir: new_dir, - }, - ); - *pos = target_pos; + move_to(*space_type, target_pos, new_dir, &mut self.board); if is_trigger { triggers_activated.push(front_pos); } @@ -328,6 +314,7 @@ impl Machine { // remove marbles for &i in removed_marbles.iter().rev() { + self.board.set(self.marbles[i], Tile::default()); self.marbles.swap_remove(i); }