hypesystem

GC.2 - NewForest.java

Oct 19th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. package gc;
  2.  
  3. import java.util.Random;
  4.  
  5. /**
  6.  * NewForest sets out to do the same job as Forest and more. NewForest makes the
  7.  * forest two-dimensional (instead of linear) and now takes the placement of
  8.  * trees into account before calculating growth.
  9.  * Example of a forest 4*4 (# marks a tree; n=4).
  10.  *     _ _ _ _ _ _ _ _
  11.  * j=3  #           # |  i is like the x-coordinate in a normal system, and j is
  12.  * j=2  #       #     |  like the y-coordinate. A forest with n spots in each
  13.  * j=1      #   #   # |  direciton holds n^2 trees (n*n).
  14.  * j=0  #           # |
  15.  *     i=0 i=1 i=2 i=3
  16.  * ---
  17.  * @author hypesystem
  18.  */
  19. public class NewForest {
  20.     private int n;
  21.     private Tree[][] trees;
  22.     private Random random_generator = new Random();
  23.    
  24.     /**
  25.      * Sets the dimensions (n*n) of the forest. Standard n=4.
  26.      */
  27.     public NewForest() {
  28.         n = 4;
  29.     }
  30.    
  31.     /**
  32.      * Creates between 8 and n*n trees that are either ash or beech and places
  33.      * them at random, unoccupied spots in the forest. Gives each tree an age
  34.      * from 1 - 10 years.
  35.      */
  36.     public void initialize() {
  37.         trees = new Tree[n][n];
  38.         int trees_amount = random_generator.nextInt(9) + (n * n) - 8;
  39.         for(int i = 0; i < trees_amount; i++) {
  40.             int random_i = 0;
  41.             int random_j = 0;
  42.             boolean spot_free = false;
  43.             while(!spot_free) {
  44.                 random_i = random_generator.nextInt(n);
  45.                 random_j = random_generator.nextInt(n);
  46.                 if(trees[random_i][random_j] == null) {
  47.                     spot_free = true;
  48.                 }
  49.             }
  50.             int tree_age = random_generator.nextInt(10) + 1;
  51.             int tree_type = random_generator.nextInt(2);
  52.             if(tree_type == 0) trees[random_i][random_j] = new Ash(tree_age);
  53.             else if(tree_type == 1)
  54.                 trees[random_i][random_j] = new Beech(tree_age);
  55.             else System.out.println("Unknown tree type");
  56.         }
  57.     }
  58.    
  59.     /**
  60.      * Checks if tree exists on specified location, and returns it if it does.
  61.      * @param i first coordinate of the tree, from 0 - (n-1).
  62.      * @param j second coordinate of the tree, from 0 - (n-1).
  63.      * @return the tree on the specified location or null.
  64.      */
  65.     public Tree getTree(int i, int j) {
  66.         if(i < n && j < n && i >= 0 && j >= 0) {
  67.             if(trees[i][j] != null) return trees[i][j];
  68.             else return null;
  69.         }
  70.         else {
  71.             //System.out.println("Error: Out of bounds");
  72.             return null;
  73.         }
  74.     }
  75.     /**
  76.      * Calculates the amount of shadow on a tree on a scale from 0 - 1 where
  77.      * 1 is completely covered and 0 is no shadow.
  78.      * @param i First oordinate of tree
  79.      * @param j Second oordinate of tree
  80.      * @return Shadow of tree
  81.      * ---
  82.      * Note: It makes no sense to have the shadow for any tree in a forest
  83.      * calculated in a tree. (Why would you call tree1.shadow(...) when you want
  84.      * shadow for tree2 for example?) shadow() should be found in NewForest,
  85.      * which is why it's here.
  86.      */
  87.     public double shadow(int i, int j) {
  88.         double tree_height;
  89.         double k = 0;
  90.         if(getTree(i,j) != null) tree_height = getTree(i,j).getHeight();
  91.         else {
  92.             //System.out.println("Tree does not exist");
  93.             return 0.0;
  94.         }
  95.         if(getTree(i-1,j) != null && getTree(i-1,j).getHeight() > tree_height)
  96.             k += 1;
  97.         if(getTree(i+1,j) != null && getTree(i+1,j).getHeight() > tree_height)
  98.             k += 1;
  99.         if(getTree(i,j+1) != null && getTree(i,j+1).getHeight() > tree_height)
  100.             k += 1;
  101.         if(getTree(i,j-1) != null && getTree(i,j-1).getHeight() > tree_height)
  102.             k += 1;
  103.         return k / 10;
  104.     }
  105.    
  106.     /**
  107.      * Equivalent of the growOneYear() method of Forest, this makes every tree
  108.      * in the forest grow one year, this time, however, using the shadow of the
  109.      * tree to calculate growth pct.
  110.      */
  111.     public void growOneYear() {
  112.         int this_i = 0;
  113.         for(Tree[] tree_row : trees) {
  114.             int this_j = 0;
  115.             for(Tree tree : tree_row) {
  116.                 if(tree != null) {
  117.                     tree.growOneYear(shadow(this_i,this_j));
  118.                     this_j++;
  119.                 }
  120.             }
  121.             this_i++;
  122.         }
  123.     }
  124.    
  125.     /**
  126.      * Equivalent of growManyYears() of the Forest class, this method grows the
  127.      * forest several years in a step.
  128.      * @param y Years to grow
  129.      */
  130.     public void growManyYears(int y) {
  131.         for(int i = 0; i < y; i++) {
  132.             growOneYear();
  133.         }
  134.     }
  135.    
  136.     /**
  137.      * Calls the show() method of every tree in the forest resulting in massive
  138.      * text spam of the output console.
  139.      */
  140.     public void show() {
  141.         for(Tree[] tree_row : trees) {
  142.             for(Tree tree : tree_row) {
  143.                 if(tree != null) {
  144.                     tree.show();
  145.                 }
  146.             }
  147.         }
  148.     }
  149.    
  150. }
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment