Skip to content

Commit

Permalink
imp(Game): added ability to win
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinatorul committed Aug 29, 2015
1 parent 77e7b5c commit 568b3a9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
35 changes: 27 additions & 8 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ impl Cell {
self.revealed = false;
}

fn reveal(&mut self) {
fn reveal(&mut self) -> &Content {
self.revealed = true;
&self.content
}
}

Expand All @@ -39,7 +40,9 @@ pub struct Field {
mines: u32,
size: u32,
selected_x: u32,
selected_y: u32
selected_y: u32,
nubmers_total: u32,
nubmers_opened: u32
}

impl Field {
Expand All @@ -51,7 +54,9 @@ impl Field {
mines: mines,
size: width*height,
selected_x: width/2,
selected_y: height/2
selected_y: height/2,
nubmers_total: 0,
nubmers_opened: 0
};
for _i in 0..field.size {
field.cells.push(Cell{content: Content::None,
Expand Down Expand Up @@ -94,6 +99,7 @@ impl Field {
}
if ct > 0 {
self.get_cell_mut(i as u32).content = Content::Number(ct);
self.nubmers_total += 1;
}
},
_ => {}
Expand All @@ -105,12 +111,17 @@ impl Field {
for i in 0..self.size {
self.get_cell_mut(i).clear();
}
self.nubmers_opened = 0;
self.nubmers_total = 0;
}

pub fn reveal(&mut self, i: u32) -> &Content {
let cell = self.get_cell_mut(i);
cell.reveal();
&cell.content
if !self.revealed(i) {
if let &Content::Number(_i) = self.get_cell_mut(i).reveal() {
self.nubmers_opened += 1;
}
}
&self.get_content(i)
}

pub fn revealed(&self, i: u32) -> bool {
Expand Down Expand Up @@ -159,7 +170,7 @@ impl Field {
}

pub fn reveal_all(&mut self) {
for i in 0..self.size {
for i in 0..self.size {
self.get_cell_mut(i).revealed = true;
}
}
Expand Down Expand Up @@ -254,7 +265,7 @@ impl Field {
}
}
}
rectangle([1.0, 1.0, 1.0, 0.5],
rectangle([0.5, 0.5, 0.5, 0.75],
[
(field_rect[0] + self.selected_x*cell_w) as f64,
(field_rect[1] + self.selected_y*cell_h) as f64,
Expand Down Expand Up @@ -311,4 +322,12 @@ impl Field {
}
}
}

pub fn get_selected_ind(&self) -> u32 {
self.selected_x + self.selected_y*self.width
}

pub fn is_victory(&self) -> bool {
self.nubmers_total == self.nubmers_opened
}
}
37 changes: 30 additions & 7 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub struct Game {
field: Field,
glyphs: Glyphs,
mouse_x: f64,
mouse_y: f64
mouse_y: f64,
game_ended: bool
}

impl Game {
Expand All @@ -14,7 +15,8 @@ impl Game {
field: Field::new(20, 15, 50),
glyphs: glyphs,
mouse_x: 0.0,
mouse_y: 0.0
mouse_y: 0.0,
game_ended: false
}
}

Expand All @@ -34,16 +36,19 @@ impl Game {
[0, 0, w, h]
}


pub fn proc_key(&mut self, button: Button, window: &PistonWindow) {
match button {
Button::Keyboard(key) => {
match key {
Key::R => self.field.restart(),
Key::R => self.restart(),
Key::Up => self.field.move_selection(MoveDestination::Up),
Key::Down => self.field.move_selection(MoveDestination::Down),
Key::Left => self.field.move_selection(MoveDestination::Left),
Key::Right => self.field.move_selection(MoveDestination::Right),
Key::Space => {
let ind = self.field.get_selected_ind();
self.open_cell(ind);
},
_ => println!("{:?}", key)
}
},
Expand Down Expand Up @@ -71,20 +76,38 @@ impl Game {
}

fn open_cell(&mut self, i: u32) {
if self.game_ended {
return;
}
match *self.field.reveal(i) {
Content::Bomb => {
self.field.reveal_all();
println!("Game over =(, {}", i);
self.game_ended = true;
println!("Game over :(");
},
Content::None => {
self.field.chain_reveal(i);
self.field.chain_reveal(i);
if self.field.is_victory() {
println!("You win :)");
self.game_ended = true;
}
}
Content::Number(_i) => {
if self.field.is_victory() {
println!("You win :)");
self.game_ended = true;
}
}
_ => println!("ok {}", i)
}
}

pub fn mouse_move(&mut self, mouse_rel: [f64; 2]) {
self.mouse_x = mouse_rel[0];
self.mouse_y = mouse_rel[1];
}

fn restart(&mut self) {
self.game_ended = false;
self.field.restart();
}
}

0 comments on commit 568b3a9

Please sign in to comment.