Skip to content

Commit

Permalink
wip: field generation, draw and more
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinatorul committed Aug 23, 2015
1 parent ee8363a commit 25259e3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 30 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -8,3 +8,4 @@ license-file = "LICENSE"
piston_window = "0.15.0"
clap = "1.2"
rand = "*"
find_folder = "*"
Binary file added assets/FiraSans-Regular.ttf
Binary file not shown.
56 changes: 53 additions & 3 deletions src/field.rs
Expand Up @@ -50,12 +50,49 @@ impl Field {

fn fill(&mut self) {
self.clear();
for _i in 1..self.mines {
for _i in 0..self.mines {
let ind = rand::thread_rng().gen_range(0, self.width*self.height);
self.get_cell_mut(ind).content = Content::Bomb
}
for i in 1..self.width*self.height {
let cell = self.get_cell_mut(i)
for i in 0..self.width {
for j in 0..self.height {
match self.get_cell(i*self.width + j).content {
Content::None => {
let mut ct : u8 = 0;
let i1 = i as i32;
let j1 = j as i32;
if let Some(b) = self.is_bomb_safe(i1-1, j1-1) {
ct += b;
}
if let Some(b) = self.is_bomb_safe(i1-1, j1) {
ct += b;
}
if let Some(b) = self.is_bomb_safe(i1-1, j1+1) {
ct += b;
}
if let Some(b) = self.is_bomb_safe(i1, j1-1) {
ct += b;
}
if let Some(b) = self.is_bomb_safe(i1, j1+1) {
ct += b;
}
if let Some(b) = self.is_bomb_safe(i1+1, j1-1) {
ct += b;
}
if let Some(b) = self.is_bomb_safe(i1+1, j1) {
ct += b;
}
if let Some(b) = self.is_bomb_safe(i1+1, j1+1) {
ct += b;
}
if ct > 0 {
let w = self.width; // get w before mutually borrowing
self.get_cell_mut(i*w + j).content = Content::Number(ct);
}
},
_ => {}
}
}
}
}

Expand Down Expand Up @@ -84,4 +121,17 @@ impl Field {
pub fn get_content(& self, i: u32) -> &Content {
&self.get_cell(i).content
}

fn is_bomb_safe(&self, i: i32, j: i32) -> Option<u8> {
if (i < 0) || ((i as u32) >= self.width) {
None
} else if (j < 0) || ((j as u32) >= self.height) {
None
} else {
match self.get_cell((i as u32)*self.width + (j as u32)).content {
Content::Bomb => Some(1),
_ => Some(0)
}
}
}
}
51 changes: 27 additions & 24 deletions src/game.rs
Expand Up @@ -2,53 +2,56 @@ use field::{Field, Content};
use piston_window::*;

pub struct Game {
field: Field
field: Field,
glyphs: Glyphs
}

impl Game {
pub fn new() -> Game {
pub fn new(glyphs: Glyphs) -> Game {
Game {
field: Field::new(20, 20, 30)
field: Field::new(20, 20, 50),
glyphs: glyphs
}
}

pub fn render(&self, window: &PistonWindow) {
window.draw_2d(|c, g| {
clear([0.0, 0.0, 0.0, 1.0], g);
});
self.draw_field(window);
pub fn render(&mut self, window: &PistonWindow) {
self.draw_field(window);
}

fn draw_field(&self, window: &PistonWindow) {
fn draw_field(&mut self, window: &PistonWindow) {
let w = window.size().width as f64;
let h = window.size().height as f64;
let cell_h = h / (self.field.height as f64);
let cell_w = w / (self.field.width as f64);
for i in 0..self.field.width {
for j in 0..self.field.height {
match *self.field.get_content(i*self.field.width + j) {
Content::Bomb => {
window.draw_2d(|c, g| {
window.draw_2d(|c, g| {
clear([0.0, 0.0, 0.0, 1.0], g);
for i in 0..self.field.width {
for j in 0..self.field.height {
match *self.field.get_content(i*self.field.width + j) {
Content::Bomb => {
rectangle([1.0, 0.0, 0.0, 1.0],
[(i as f64)*cell_w, (j as f64)*cell_h, cell_w, cell_h],
c.transform, g);

});
},
Content::Number(n) => {

},
Content::None => {
window.draw_2d(|c, g| {
},
Content::Number(n) => {
let transform = c.transform.trans((i as f64)*cell_w + 10.0, (j as f64)*cell_h + cell_h - 5.0);
text::Text::colored([1.0, 1.0, 1.0, 1.0], 32).draw(
&*n.to_string(),
&mut self.glyphs,
&c.draw_state,
transform, g
);
},
Content::None => {
rectangle([1.0, 1.0, 1.0, 1.0],
[(i as f64)*cell_w, (j as f64)*cell_h, cell_w, cell_h],
c.transform, g);

});
}
}
}
}
}
});
}

pub fn proc_key(&self, button: Button) {
Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
@@ -1,6 +1,7 @@
extern crate piston_window;
extern crate clap;
extern crate rand;
extern crate find_folder;

mod game;
mod field;
Expand All @@ -17,8 +18,8 @@ fn main() {
.arg(Arg::from_usage("--height [height] 'window height'"))
.get_matches();

let mut width = 640;
let mut height = 480;
let mut width = 600;
let mut height = 600;
if let Some(w) = matches.value_of("width") {
width = w.parse().unwrap_or(width);
}
Expand All @@ -32,7 +33,13 @@ fn main() {
.build()
.unwrap();

let game = game::Game::new();
let assets = find_folder::Search::ParentsThenKids(3, 3)
.for_folder("assets").unwrap();
let ref font = assets.join("FiraSans-Regular.ttf");
let factory = window.factory.borrow().clone();
let glyphs = Glyphs::new(font, factory).unwrap();

let mut game = game::Game::new(glyphs);

for e in window {
game.render(&e);
Expand Down

0 comments on commit 25259e3

Please sign in to comment.