Advertisement
Guest User

Untitled

a guest
Dec 30th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 5.20 KB | None | 0 0
  1. extern crate rand;
  2.  
  3. use rand::Rng;
  4. use std::clone;
  5. use std::fmt;
  6. use std::io;
  7. use std::io::Write;
  8.  
  9. #[derive(Clone)]
  10. enum TreeType {
  11.     Mature,
  12.     Sapling,
  13.     Elder,
  14. }
  15. #[derive(Clone)]
  16. struct Tree {
  17.     plant: TreeType,
  18.     age: u32,
  19. }
  20.  
  21. #[derive(Clone)]
  22. enum ForestPopulation {
  23.     Lumberjack,
  24.     Bear,
  25.     FTree(Tree),
  26.     Empty,
  27.     BearTree(Tree), // A tree with a bear in it!!
  28.     LumberSap(u32),
  29. }
  30.  
  31. type ForestGrid = Vec<Vec<ForestPopulation>>;
  32.  
  33. impl fmt::Debug for TreeType {
  34.     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  35.         match self {
  36.             TreeType::Mature => write!(f, "T"),
  37.             TreeType::Sapling => write!(f, "S"),
  38.             TreeType::Elder => write!(f, "E"),
  39.         }
  40.     }
  41. }
  42. //ignoring displaying of age
  43. impl fmt::Debug for Tree {
  44.     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  45.         self.plant.fmt(f)
  46.     }
  47. }
  48. impl fmt::Debug for ForestPopulation {
  49.     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  50.         match self {
  51.             ForestPopulation::Lumberjack => write!(f, "L"),
  52.             ForestPopulation::Bear => write!(f, "B"),
  53.             ForestPopulation::Empty => write!(f, " "),
  54.             ForestPopulation::FTree(tree) => tree.fmt(f),
  55.             ForestPopulation::BearTree(tree) => tree.fmt(f),
  56.             ForestPopulation::LumberSap(_) => TreeType::Sapling.fmt(f), //keep it consistent with sapling
  57.         }
  58.     }
  59. }
  60.  
  61. //TODO remove duplcation and put into private methods
  62. impl Tree {
  63.     fn new(age:u32) -> Tree {
  64.         match age{
  65.             0 => Tree {
  66.                 plant: TreeType::Sapling,
  67.                 age: 0,
  68.             },
  69.              0...11 => Tree {
  70.                 plant: TreeType::Sapling,
  71.                 age: age
  72.             },
  73.             11...119 => Tree {
  74.                 plant: TreeType::Mature,
  75.                 age: age,
  76.             },
  77.             _ => Tree {
  78.                 plant: TreeType::Elder,
  79.                 age: age,
  80.             },
  81.         }
  82.     }
  83.     fn grow(&self) -> Tree {
  84.         match self.age {
  85.             0...11 => Tree {
  86.                 plant: TreeType::Sapling,
  87.                 age: self.age + 1,
  88.             },
  89.             11...119 => Tree {
  90.                 plant: TreeType::Mature,
  91.                 age: self.age + 1,
  92.             },
  93.             _ => Tree {
  94.                 plant: TreeType::Elder,
  95.                 age: self.age + 1,
  96.             },
  97.         }
  98.     }
  99.     fn mut_grow(&mut self) {
  100.         self.age += 1;
  101.         match self.age {
  102.             0...12 => {}
  103.             12...120 => self.plant = TreeType::Mature,
  104.             _ => self.plant = TreeType::Elder,
  105.         }
  106.     }
  107. }
  108.  
  109. fn main() {
  110.     let size = get_size();
  111.     let mut forest: ForestGrid = (0..size) //TODO make a 1 dimensional variant as performance on this is not great
  112.         .into_iter()
  113.         .map(|_| {
  114.             (0..size)
  115.                 .into_iter()
  116.                 .map(|_| match rand::thread_rng().gen_range(0, 100) {
  117.                     0...10 => ForestPopulation::Lumberjack,
  118.                     10...50 => ForestPopulation::FTree(Tree {
  119.                         plant: TreeType::Mature,
  120.                         age: 12,
  121.                     }),
  122.                     50..=52 => ForestPopulation::Bear,
  123.                     _ => ForestPopulation::Empty,
  124.                 })
  125.                 .collect()
  126.         })
  127.         .collect();
  128.  
  129.     println!("The Forest");
  130.     for i in &forest {
  131.         println!("{:?}", i);
  132.     }
  133.     //TODO movable
  134.     //let
  135.     for month in 1..4800 {
  136.         println!("The Forest month {}", month);
  137.         for row in forest.iter_mut() {
  138.             for feature in row.iter_mut() {
  139.                 match feature {
  140.                     ForestPopulation::Lumberjack => *feature = ForestPopulation::Lumberjack,
  141.                     ForestPopulation::Bear => *feature = ForestPopulation::Bear,
  142.                     ForestPopulation::Empty => *feature = ForestPopulation::Empty,
  143.                     ForestPopulation::FTree(ref mut tree) => tree.mut_grow(),
  144.                     //TODO handle these cases properly
  145.                     ForestPopulation::BearTree(ref mut tree) => tree.mut_grow(),
  146.                     ForestPopulation::LumberSap(sap_age) =>{ let mut newTree = Tree::new(*sap_age);
  147.                         let mut newFTree = ForestPopulation::FTree(newTree);
  148.                         *feature =   newFTree;
  149.                         },
  150.                    
  151.                 }
  152.             }
  153.         }
  154.  
  155.         for i in &forest {
  156.             println!("{:?}", i);
  157.         }
  158.     }
  159. }
  160.  
  161. fn get_size() -> u32 {
  162.     loop {
  163.         let mut input_text = String::new();
  164.         print!("Enter the size of the forest:");
  165.         io::stdout().flush().expect("unable to flush output"); //print is line buffered
  166.         match io::stdin().read_line(&mut input_text) {
  167.             Ok(..) => (),
  168.             Err(..) => {
  169.                 println!("failed to read std IO trying again");
  170.                 continue;
  171.             }
  172.         };
  173.         println!("you entered {}", input_text);
  174.         match input_text.trim().parse::<u32>() {
  175.             Ok(i) => return i,
  176.             Err(e) => println!("{}", e),
  177.         };
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement