Skip to content

Commit

Permalink
feat: when you click on a revealed number, it clicks on unrevealed ne…
Browse files Browse the repository at this point in the history
…ighbors as a shortcut.
  • Loading branch information
waynenilsen committed Sep 2, 2015
1 parent f399b9a commit 67942f5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use piston_window::*;
pub enum Content {
Number(u8),
Mine,
None
None,
}

struct Cell {
content: Content,
revealed: bool,
marked: bool
marked: bool,
}

impl Cell {
Expand Down Expand Up @@ -178,6 +178,10 @@ impl Field {
self.height
}

pub fn get_size(&self) -> u32 {
self.size
}

pub fn restart(&mut self) {
self.fill();
}
Expand Down Expand Up @@ -245,6 +249,17 @@ impl Field {
for i in 0..self.get_width() {
for j in 0..self.get_height() {
let ind = i + j*self.get_width();
let transform = context.transform.trans((field_rect[0] + i*cell_w) as f64 + 5.0,
(field_rect[1] + (j+1)*cell_h) as f64 - 5.0);
/*// show cell numbers, todo: make this a command line option
text::Text::colored([1.0, 1.0, 0.0, 1.0], cell_h/2 ).draw(
&*ind.to_string(),
glyps,
&context.draw_state,
transform,
graphics
);
*/
if self.revealed(ind) {
match *self.get_content(i + j*self.get_width()) {
Content::Mine => {
Expand All @@ -259,8 +274,6 @@ impl Field {
graphics);
},
Content::Number(n) => {
let transform = context.transform.trans((field_rect[0] + i*cell_w) as f64 + 5.0,
(field_rect[1] + (j+1)*cell_h) as f64 - 5.0);
rectangle([1.0, 1.0, 1.0, 1.0],
[
(field_rect[0] + i*cell_w) as f64,
Expand Down Expand Up @@ -404,4 +417,4 @@ impl Field {
pub fn toggle_mark(&mut self, i: u32) {
self.get_cell_mut(i).toggle_mark();
}
}
}
31 changes: 28 additions & 3 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,34 @@ impl<'a> Game<'a> {
}

fn open_cell(&mut self, i: u32) {
if self.game_ended || (self.field.marked(i)) {
if self.game_ended {
return;
}

if !self.field.revealed(i) {
self.check_reveal(i);
} else {
let on_left_edge = i % self.field.get_width() == 0;
let on_right_edge = (i+1) % self.field.get_width() == 0;

for rdelta in -1..2i32 {
for cdelta in -1..2i32 {
if on_left_edge && cdelta == -1 { continue; }
if on_right_edge && cdelta == 1 { continue; }
let tgt = (i as i32) + rdelta*(self.field.get_width() as i32) + cdelta;
if tgt < 0 || tgt > self.field.get_size() as i32 { continue; }
self.check_reveal(tgt as u32);

}
}
}
}

pub fn check_reveal(&mut self, i : u32) {
if self.field.marked(i) {
return;
}

match *self.field.reveal(i) {
Content::Mine => {
self.field.reveal_all();
Expand All @@ -183,7 +208,7 @@ impl<'a> Game<'a> {
if self.field.is_victory() {
println!("You win :)");
self.game_ended = true;
}
}
}
}
}
Expand All @@ -197,4 +222,4 @@ impl<'a> Game<'a> {
self.game_ended = false;
self.field.restart();
}
}
}

0 comments on commit 67942f5

Please sign in to comment.