From c896f3a5f9b2e3f7f03ad1871b6566f098c0e295 Mon Sep 17 00:00:00 2001 From: stale Date: Sun, 29 May 2022 21:38:34 -0300 Subject: limit flood depth, quiet logs, fix board tx --- src/minesweeper.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'src/minesweeper.rs') diff --git a/src/minesweeper.rs b/src/minesweeper.rs index a494b94..013e441 100644 --- a/src/minesweeper.rs +++ b/src/minesweeper.rs @@ -5,6 +5,7 @@ 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 @@ -186,23 +187,25 @@ impl Board { (x < self.width.get() && y < self.height.get()).then(|| (x,y)) } else { None } } - pub fn flood_reveal(&mut self, pos: (usize,usize)) { - 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 } - drop(c); - self.neighs(pos).map(|n| n.iter().for_each(|pos| { - self.flood_reveal(*pos); - })); + pub fn flood_reveal(&mut self, pos: (usize,usize), depth: usize) { + if depth > 0 { + 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 } + drop(c); + self.neighs(pos).map(|n| n.iter().for_each(|pos| { + self.flood_reveal(*pos, depth - 1); + })); + } } } } pub fn reveal(mut self, pos: (usize,usize)) -> MoveResult { if let Some(off) = self.pos_to_off(pos) { - self.flood_reveal(pos); + self.flood_reveal(pos, FLOOD_MAX_DEPTH); let c = self.data[off]; MoveResult { 0: self, 1: (c & !(FLAGGED_BIT | CORRECT_BIT)) == TILE_NUMBITS } } else { @@ -230,7 +233,7 @@ impl Board { for x in 0..self.width.get() { let c = &self.data[self.pos_to_off_unchecked((x,y))]; match *c { - 0 => ret.extend_from_slice(b" "), + 0 => ret.push(b' '), _ if *c <= 8 => ret.push(b'0' + c), _ if (*c & CORRECT_BIT) > 0 => ret.push(b'C'), _ if (*c & FLAGGED_BIT) > 0 => ret.push(b'F'), -- cgit v1.2.3