Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate rand;
- use rand::Rng;
- use std::clone;
- use std::fmt;
- use std::io;
- use std::io::Write;
- #[derive(Clone)]
- enum TreeType {
- Mature,
- Sapling,
- Elder,
- }
- #[derive(Clone)]
- struct Tree {
- plant: TreeType,
- age: u32,
- }
- #[derive(Clone)]
- enum ForestPopulation {
- Lumberjack,
- Bear,
- FTree(Tree),
- Empty,
- BearTree(Tree), // A tree with a bear in it!!
- LumberSap(u32),
- }
- type ForestGrid = Vec<Vec<ForestPopulation>>;
- impl fmt::Debug for TreeType {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- TreeType::Mature => write!(f, "T"),
- TreeType::Sapling => write!(f, "S"),
- TreeType::Elder => write!(f, "E"),
- }
- }
- }
- //ignoring displaying of age
- impl fmt::Debug for Tree {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- self.plant.fmt(f)
- }
- }
- impl fmt::Debug for ForestPopulation {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- ForestPopulation::Lumberjack => write!(f, "L"),
- ForestPopulation::Bear => write!(f, "B"),
- ForestPopulation::Empty => write!(f, " "),
- ForestPopulation::FTree(tree) => tree.fmt(f),
- ForestPopulation::BearTree(tree) => tree.fmt(f),
- ForestPopulation::LumberSap(_) => TreeType::Sapling.fmt(f), //keep it consistent with sapling
- }
- }
- }
- //TODO remove duplcation and put into private methods
- impl Tree {
- fn new(age:u32) -> Tree {
- match age{
- 0 => Tree {
- plant: TreeType::Sapling,
- age: 0,
- },
- 0...11 => Tree {
- plant: TreeType::Sapling,
- age: age
- },
- 11...119 => Tree {
- plant: TreeType::Mature,
- age: age,
- },
- _ => Tree {
- plant: TreeType::Elder,
- age: age,
- },
- }
- }
- fn grow(&self) -> Tree {
- match self.age {
- 0...11 => Tree {
- plant: TreeType::Sapling,
- age: self.age + 1,
- },
- 11...119 => Tree {
- plant: TreeType::Mature,
- age: self.age + 1,
- },
- _ => Tree {
- plant: TreeType::Elder,
- age: self.age + 1,
- },
- }
- }
- fn mut_grow(&mut self) {
- self.age += 1;
- match self.age {
- 0...12 => {}
- 12...120 => self.plant = TreeType::Mature,
- _ => self.plant = TreeType::Elder,
- }
- }
- }
- fn main() {
- let size = get_size();
- let mut forest: ForestGrid = (0..size) //TODO make a 1 dimensional variant as performance on this is not great
- .into_iter()
- .map(|_| {
- (0..size)
- .into_iter()
- .map(|_| match rand::thread_rng().gen_range(0, 100) {
- 0...10 => ForestPopulation::Lumberjack,
- 10...50 => ForestPopulation::FTree(Tree {
- plant: TreeType::Mature,
- age: 12,
- }),
- 50..=52 => ForestPopulation::Bear,
- _ => ForestPopulation::Empty,
- })
- .collect()
- })
- .collect();
- println!("The Forest");
- for i in &forest {
- println!("{:?}", i);
- }
- //TODO movable
- //let
- for month in 1..4800 {
- println!("The Forest month {}", month);
- for row in forest.iter_mut() {
- for feature in row.iter_mut() {
- match feature {
- ForestPopulation::Lumberjack => *feature = ForestPopulation::Lumberjack,
- ForestPopulation::Bear => *feature = ForestPopulation::Bear,
- ForestPopulation::Empty => *feature = ForestPopulation::Empty,
- ForestPopulation::FTree(ref mut tree) => tree.mut_grow(),
- //TODO handle these cases properly
- ForestPopulation::BearTree(ref mut tree) => tree.mut_grow(),
- ForestPopulation::LumberSap(sap_age) =>{ let mut newTree = Tree::new(*sap_age);
- let mut newFTree = ForestPopulation::FTree(newTree);
- *feature = newFTree;
- },
- }
- }
- }
- for i in &forest {
- println!("{:?}", i);
- }
- }
- }
- fn get_size() -> u32 {
- loop {
- let mut input_text = String::new();
- print!("Enter the size of the forest:");
- io::stdout().flush().expect("unable to flush output"); //print is line buffered
- match io::stdin().read_line(&mut input_text) {
- Ok(..) => (),
- Err(..) => {
- println!("failed to read std IO trying again");
- continue;
- }
- };
- println!("you entered {}", input_text);
- match input_text.trim().parse::<u32>() {
- Ok(i) => return i,
- Err(e) => println!("{}", e),
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement