Add toggling empty tiles

This commit is contained in:
ElementG9 2020-04-14 02:31:05 -06:00
parent 33d97cd311
commit 51dece2bb9

View File

@ -49,7 +49,7 @@ pub fn run() {
let mut game = Game::new(width, height, difficulty); let mut game = Game::new(width, height, difficulty);
while game.running { while game.running {
println!("{}", game.get_board()); println!("{}", game.get_board());
match prompt("\nWhat would you like to do?\n1. Uncover a tile\n2. Flag a tile\n3. Give up\n> ", 1, 3) { match prompt("\nWhat would you like to do?\n1. Uncover a tile\n2. Flag a tile\n3. Toggle empty tiles\n4. Give up\n> ", 1, 4) {
1 => { 1 => {
let pos = prompt_coords(0, game.board.width - 1, 0, game.board.height - 1); let pos = prompt_coords(0, game.board.width - 1, 0, game.board.height - 1);
println!("Uncovering ({}, {})", pos.x, pos.y); println!("Uncovering ({}, {})", pos.x, pos.y);
@ -61,6 +61,9 @@ pub fn run() {
game.flag(pos.x as usize, pos.y as usize); game.flag(pos.x as usize, pos.y as usize);
}, },
3 => { 3 => {
game.show_empty = !game.show_empty;
},
4 => {
game.end(); game.end();
}, },
_ => {} _ => {}
@ -139,6 +142,7 @@ pub struct Game {
pub uncovered: Vec<Vec<bool>>, pub uncovered: Vec<Vec<bool>>,
pub flags: Vec<Coord>, pub flags: Vec<Coord>,
pub num_bombs: usize, pub num_bombs: usize,
pub show_empty: bool,
} }
impl Game { impl Game {
pub fn new(width: usize, height: usize, difficulty: Difficulty) -> Game { pub fn new(width: usize, height: usize, difficulty: Difficulty) -> Game {
@ -169,6 +173,7 @@ impl Game {
uncovered, uncovered,
flags: Vec::new(), flags: Vec::new(),
num_bombs, num_bombs,
show_empty: true
} }
} }
pub fn get_valid_neighbors(&self, x: usize, y: usize) -> Vec<Coord> { pub fn get_valid_neighbors(&self, x: usize, y: usize) -> Vec<Coord> {
@ -304,7 +309,7 @@ impl Game {
6 => "6".color(Color::LightBlue), 6 => "6".color(Color::LightBlue),
7 => "7".color(Color::Black), 7 => "7".color(Color::Black),
8 => "8".color(Color::White), 8 => "8".color(Color::White),
_ => ".".color(Color::White), _ => if self.show_empty { ".".color(Color::DarkGray) } else { " ".color(Color::DarkGray) },
}); });
} }
Cell::Bomb => { Cell::Bomb => {