diff options
Diffstat (limited to 'src/minesweeper.rs')
-rw-r--r-- | src/minesweeper.rs | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/src/minesweeper.rs b/src/minesweeper.rs index 8c0dc77..a56758b 100644 --- a/src/minesweeper.rs +++ b/src/minesweeper.rs @@ -148,11 +148,14 @@ impl Board { } } pub fn reveal(mut self, pos: (usize, usize)) -> MoveResult { - if pos.0 > self.width - 1 || pos.1 > self.height - 1 { panic!("OOB reveal"); } + if pos.0 > self.width - 1 || pos.1 > self.height - 1 { + println!("attempted OOB reveal @ {:?}", pos); + return MoveResult { 0: self, 1: false }; + } let off = self.pos_to_off(pos); self.flood_reveal(pos); let c = self.data[off]; - MoveResult { 0: self, 1: (c & !(FLAGGED_BIT | CORRECT_BIT)) == MINE_VAL } // Kaboom + MoveResult { 0: self, 1: (c & !(FLAGGED_BIT | CORRECT_BIT)) == MINE_VAL } } pub fn grade(mut self) -> Board { for i in &mut self.data { @@ -168,25 +171,20 @@ impl Board { MoveResult { 0: self, 1: false } } - pub fn render(&self, cursor: Option<(usize,usize)>) -> Vec<u8> { + pub fn render(&self) -> Vec<u8> { const CYAN: &[u8] = &[27,b'[',b'3',b'6',b'm']; const YELLOW: &[u8] = &[27,b'[',b'3',b'3',b'm']; const GREEN: &[u8] = &[27,b'[',b'3',b'2',b'm']; const RED: &[u8] = &[27,b'[',b'3',b'1',b'm']; const FG: &[u8] = &[27,b'[',b'3',b'9',b'm']; - const ULINE: &[u8] = &[27,b'[',b'4',b'm']; - const REGULAR: &[u8] = &[27,b'[',b'0',b'm']; let mut ret = vec![27]; let mut cur_clr = FG; for y in 0..self.height { - ret.extend_from_slice(b"<br>"); for x in 0..self.width { let c = &self.data[self.pos_to_off((x,y))]; - let is_cursor = if let Some(cursor) = cursor { cursor.0 == x && cursor.1 == y } else { false }; - if is_cursor { ret.extend_from_slice(ULINE); } match *c { - 0 => ret.push(b' '), + 0 => ret.extend_from_slice(b" "), _ if *c <= 8 => { if cur_clr != FG { ret.extend_from_slice(FG); cur_clr = FG; } ret.push(b'0' + c); }, _ if (*c & CORRECT_BIT) > 0 => { if cur_clr != GREEN { ret.extend_from_slice(GREEN); cur_clr = GREEN; } ret.push(b'F') }, _ if (*c & FLAGGED_BIT) > 0 => { if cur_clr != YELLOW { ret.extend_from_slice(YELLOW); cur_clr = YELLOW; } ret.push(b'F'); }, @@ -194,8 +192,9 @@ impl Board { _ if *c == MINE_VAL => { if cur_clr != RED { ret.extend_from_slice(RED); cur_clr = RED; } ret.push(b'O'); }, _ => ret.push(b'?'), } - if is_cursor { ret.extend_from_slice(REGULAR); ret.extend_from_slice(cur_clr); } } + ret.extend_from_slice(FG); cur_clr = FG; + ret.extend_from_slice(b"<br>"); } ret } |