From 29020b56ac8377b88f2065874bebfaced75e312e Mon Sep 17 00:00:00 2001
From: David Li
Date: Thu, 10 Dec 2015 17:29:50 -0500
Subject: Initial commit
---
.gitignore | 1 +
Cargo.lock | 41 +++++++++++++++++++++++++++++++++++++++++
Cargo.toml | 7 +++++++
src/coordinate.rs | 17 +++++++++++++++++
src/inhabitant.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++
src/main.rs | 13 +++++++++++++
src/world.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 180 insertions(+)
create mode 100644 .gitignore
create mode 100644 Cargo.lock
create mode 100644 Cargo.toml
create mode 100644 src/coordinate.rs
create mode 100644 src/inhabitant.rs
create mode 100644 src/main.rs
create mode 100644 src/world.rs
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..eb5a316
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..69aa928
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,41 @@
+[root]
+name = "rust_critterworld"
+version = "0.1.0"
+dependencies = [
+ "rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "advapi32-sys"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "rand"
+version = "0.3.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "winapi"
+version = "0.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "winapi-build"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..8083460
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "rust_critterworld"
+version = "0.1.0"
+authors = ["David Li "]
+
+[dependencies]
+rand = "0.3"
diff --git a/src/coordinate.rs b/src/coordinate.rs
new file mode 100644
index 0000000..d99cd16
--- /dev/null
+++ b/src/coordinate.rs
@@ -0,0 +1,17 @@
+// "even-q" vertical layout
+// not exactly the same as 2112
+#[derive(Clone)]
+pub struct Coordinate {
+ x: i32,
+ y: i32,
+}
+
+#[derive(Clone)]
+pub enum Direction {
+ N,
+ S,
+ NE,
+ SE,
+ NW,
+ SW
+}
diff --git a/src/inhabitant.rs b/src/inhabitant.rs
new file mode 100644
index 0000000..2ed7de7
--- /dev/null
+++ b/src/inhabitant.rs
@@ -0,0 +1,46 @@
+use std::fmt;
+
+use coordinate;
+
+#[derive(Clone)]
+pub enum Inhabitant {
+ Critter {
+ species: String,
+ offense: i32,
+ defense: i32,
+ size: i32,
+ energy: u32,
+ posture: i32,
+ position: coordinate::Coordinate,
+ direction: coordinate::Direction,
+ },
+ Nothing,
+ Rock,
+ Food {
+ amount: i32,
+ },
+}
+
+impl fmt::Debug for Inhabitant {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match *self {
+ Inhabitant::Nothing => write!(f, "__"),
+ Inhabitant::Rock => write!(f, "RR"),
+ Inhabitant::Food { amount: amount } => {
+ if amount < 50 {
+ write!(f, "ff")
+ }
+ else if amount < 100 {
+ write!(f, "fF")
+ }
+ else if amount < 200 {
+ write!(f, "Ff")
+ }
+ else {
+ write!(f, "FF")
+ }
+ },
+ Inhabitant::Critter {..} => write!(f, "CC")
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..6629d34
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,13 @@
+#![feature(step_by)]
+
+extern crate rand;
+
+mod inhabitant;
+mod coordinate;
+mod world;
+
+fn main() {
+ let world = world::World::new_rand("World 0", 10, 10);
+
+ world.print();
+}
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