Skip to content

Commit

Permalink
feat: show the user which mine ended the game
Browse files Browse the repository at this point in the history
  • Loading branch information
waynenilsen committed Sep 2, 2015
1 parent 67942f5 commit b2168f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use piston_window::*;

pub enum Content {
Number(u8),
Mine,
Mine(bool), // bool is true when this is the mine that caused you to loose the game.
None,
}

Expand Down Expand Up @@ -79,7 +79,7 @@ impl Field {
self.clear();
for _i in 0..self.mines {
let ind = rand::thread_rng().gen_range(0, self.size);
self.get_cell_mut(ind).content = Content::Mine
self.get_cell_mut(ind).content = Content::Mine(false);
}
let mut i: i32 = -1;
let w = self.width as i32;
Expand Down Expand Up @@ -141,6 +141,10 @@ impl Field {
self.get_cell(i).marked
}

pub fn set_killer(&mut self, i: u32) {
self.get_cell_mut(i).content = Content::Mine(true);
}

fn get_cell_mut(&mut self, i:u32) -> &mut Cell {
self.cells.get_mut(i as usize)
.unwrap_or_else(|| panic!("Range check error at Field::get_cell_mut ({})", i))
Expand All @@ -165,7 +169,7 @@ impl Field {

fn is_mine_safe(&self, i: i32) -> bool {
match self.get_content_safe(i) {
Some(&Content::Mine) => true,
Some(&Content::Mine(_)) => true,
_ => false
}
}
Expand Down Expand Up @@ -262,7 +266,7 @@ impl Field {
*/
if self.revealed(ind) {
match *self.get_content(i + j*self.get_width()) {
Content::Mine => {
Content::Mine(killer) => {
rectangle([1.0, 0.0, 0.0, 1.0],
[
(field_rect[0] + i*cell_w) as f64,
Expand All @@ -272,6 +276,15 @@ impl Field {
],
context.transform,
graphics);
if killer {
text::Text::colored([1.0, 1.0, 1.0, 1.0], cell_h*2/3).draw(
"*",
glyps,
&context.draw_state,
transform,
graphics
);
}
},
Content::Number(n) => {
rectangle([1.0, 1.0, 1.0, 1.0],
Expand Down
3 changes: 2 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ impl<'a> Game<'a> {
}

match *self.field.reveal(i) {
Content::Mine => {
Content::Mine(_) => {
self.field.reveal_all();
self.game_ended = true;
self.field.set_killer(i);
println!("Game over :(");
},
Content::None => {
Expand Down

0 comments on commit b2168f0

Please sign in to comment.