diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/conn.rs | 6 | ||||
-rw-r--r-- | src/minesweeper.rs | 29 |
2 files changed, 19 insertions, 16 deletions
diff --git a/src/conn.rs b/src/conn.rs index d38b6ef..af7903f 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -137,7 +137,7 @@ pub async fn handle_room(mut incoming: SplitStream<WebSocket>, addr: SocketAddr, "reveal" => { match parse_pos(fields) { Some(pos) => { - println!("{cmd} from {me}"); + //println!("{cmd} from {me}"); if let Err(e) = cmd_tx.send(MetaMove::Move(Move { t: MoveType::Reveal, pos }, addr)) { println!("couldn't process {me}'s reveal command: {e}"); }; @@ -150,7 +150,7 @@ pub async fn handle_room(mut incoming: SplitStream<WebSocket>, addr: SocketAddr, "flag" => { match parse_pos(fields) { Some(pos) => { - println!("{cmd} from {me}"); + //println!("{cmd} from {me}"); if let Err(e) = cmd_tx.send(MetaMove::Move(Move { t: MoveType::ToggleFlag, pos }, addr)) { println!("couldn't process {me}'s flag command: {e}"); }; @@ -161,7 +161,7 @@ pub async fn handle_room(mut incoming: SplitStream<WebSocket>, addr: SocketAddr, } }, "reset" => { - println!("{cmd} from {me}"); + //println!("{cmd} from {me}"); if let Err(e) = cmd_tx.send(MetaMove::Reset) { println!("couldn't request game dump in behalf of {me}: {e}"); } 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'), |