diff options
author | stale <redkugelblitzin@gmail.com> | 2022-06-05 20:43:32 -0300 |
---|---|---|
committer | stale <redkugelblitzin@gmail.com> | 2022-06-05 20:43:32 -0300 |
commit | e00fa116c51e728e341e48e4c5f9f09cf4d416fc (patch) | |
tree | 9dc60cd8c915ee0637ce73b765937c36cbf0fbb0 /src | |
parent | aeb7beb3f9b154f4aefbaa1c08d822ed572461fb (diff) |
code cleanup, floodfill algo changed to iterative
Diffstat (limited to 'src')
-rw-r--r-- | src/minesweeper.rs | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/minesweeper.rs b/src/minesweeper.rs index 013e441..288847e 100644 --- a/src/minesweeper.rs +++ b/src/minesweeper.rs @@ -5,7 +5,6 @@ use std::{ use rand::{ thread_rng, Rng, distributions::Uniform }; use serde::Serialize; -const FLOOD_MAX_DEPTH: usize = 64; const HIDDEN_BIT: u8 = 1 << 7; pub const FLAGGED_BIT: u8 = 1 << 6; const CORRECT_BIT: u8 = 1 << 5; // grading for a rightly flagged mine @@ -187,25 +186,26 @@ impl Board { (x < self.width.get() && y < self.height.get()).then(|| (x,y)) } else { None } } - pub fn flood_reveal(&mut self, pos: (usize,usize), depth: usize) { - if depth > 0 { + pub fn flood_reveal(&mut self, pos: (usize,usize)) { + let mut queue = vec![pos]; + while let Some(pos) = queue.pop() { if let Some(off) = self.pos_to_off(pos) { let c = &mut self.data[off]; if *c & HIDDEN_BIT > 0 { *c &= !(HIDDEN_BIT | FLAGGED_BIT); self.hidden_tiles -= 1; - if *c > 0 { return } + if *c > 0 { continue; } drop(c); - self.neighs(pos).map(|n| n.iter().for_each(|pos| { - self.flood_reveal(*pos, depth - 1); - })); + if let Some(mut adj) = self.neighs(pos) { + queue.append(&mut adj); + } } } } } pub fn reveal(mut self, pos: (usize,usize)) -> MoveResult { if let Some(off) = self.pos_to_off(pos) { - self.flood_reveal(pos, FLOOD_MAX_DEPTH); + self.flood_reveal(pos); let c = self.data[off]; MoveResult { 0: self, 1: (c & !(FLAGGED_BIT | CORRECT_BIT)) == TILE_NUMBITS } } else { |