diff options
author | stale <redkugelblitzin@gmail.com> | 2022-05-29 21:38:34 -0300 |
---|---|---|
committer | stale <redkugelblitzin@gmail.com> | 2022-05-29 21:38:34 -0300 |
commit | c896f3a5f9b2e3f7f03ad1871b6566f098c0e295 (patch) | |
tree | e9e22fde8c3493de558fee5bdc8acf0aa76b09b5 | |
parent | bcd5764aa03e128e06c355272b7b81eac8d4501d (diff) |
limit flood depth, quiet logs, fix board tx
-rw-r--r-- | assets/client.js | 10 | ||||
-rw-r--r-- | src/conn.rs | 6 | ||||
-rw-r--r-- | src/minesweeper.rs | 29 |
3 files changed, 28 insertions, 17 deletions
diff --git a/assets/client.js b/assets/client.js index a6a2a73..e50ea9d 100644 --- a/assets/client.js +++ b/assets/client.js @@ -89,7 +89,15 @@ function connect() { function acceptBoard(data) { let vals = new Uint8Array(data); - board = Array.from(vals).reduce((s,c) => s + String.fromCodePoint(c), ""); + board = Array.from(vals).reduce((s,c) => { + let v = String.fromCodePoint(c); + if (v == ' ') { + s = s + " "; + } else { + s = s + v; + } + return s; + }, ""); let last = board[0]; let last_idx = 0; let split_board = []; 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'), |