From 29020b56ac8377b88f2065874bebfaced75e312e Mon Sep 17 00:00:00 2001 From: David Li Date: Thu, 10 Dec 2015 17:29:50 -0500 Subject: Initial commit --- src/world.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/world.rs (limited to 'src/world.rs') diff --git a/src/world.rs b/src/world.rs new file mode 100644 index 0000000..3542471 --- /dev/null +++ b/src/world.rs @@ -0,0 +1,55 @@ +use inhabitant; + +pub struct World { + pub name: String, + pub width: i32, + pub height: i32, + pub inhabitants: Vec, +} + +impl World { + pub fn new(name: &str, width: i32, height: i32) -> World { + World { + name: name.to_string(), + width: width, + height: height, + inhabitants: vec![inhabitant::Inhabitant::Nothing; (width * height) as usize], + } + } + + pub fn new_rand(name: &str, width: i32, height: i32) -> World { + use rand; + use rand::Rng; + + let mut world = World { + name: name.to_string(), + width: width, + height: height, + inhabitants: vec![inhabitant::Inhabitant::Nothing; (width * height) as usize], + }; + let mut rng = rand::thread_rng(); + for i in 0..(width*height) { + if rng.gen_weighted_bool(5) { + world.inhabitants[i as usize] = inhabitant::Inhabitant::Rock; + } + else if rng.gen_weighted_bool(7) { + world.inhabitants[i as usize] = inhabitant::Inhabitant::Food { amount: rng.gen_range(1, 200) }; + } + } + + world + } + + pub fn print(&self) { + for row in (0..self.height).rev() { + for col in (1..self.width).step_by(2) { + print!(" {:?}", self.inhabitants[( col * self.height + row ) as usize]); + } + print!("\n "); + for col in (0..self.width).step_by(2) { + print!("{:?} ", self.inhabitants[( col * self.height + row ) as usize]); + } + print!("\n"); + } + } +} -- cgit v1.2.3