Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gc;
- import java.util.Random;
- /**
- * NewForest sets out to do the same job as Forest and more. NewForest makes the
- * forest two-dimensional (instead of linear) and now takes the placement of
- * trees into account before calculating growth.
- * Example of a forest 4*4 (# marks a tree; n=4).
- * _ _ _ _ _ _ _ _
- * j=3 # # | i is like the x-coordinate in a normal system, and j is
- * j=2 # # | like the y-coordinate. A forest with n spots in each
- * j=1 # # # | direciton holds n^2 trees (n*n).
- * j=0 # # |
- * i=0 i=1 i=2 i=3
- * ---
- * @author hypesystem
- */
- public class NewForest {
- private int n;
- private Tree[][] trees;
- private Random random_generator = new Random();
- /**
- * Sets the dimensions (n*n) of the forest. Standard n=4.
- */
- public NewForest() {
- n = 4;
- }
- /**
- * Creates between 8 and n*n trees that are either ash or beech and places
- * them at random, unoccupied spots in the forest. Gives each tree an age
- * from 1 - 10 years.
- */
- public void initialize() {
- trees = new Tree[n][n];
- int trees_amount = random_generator.nextInt(9) + (n * n) - 8;
- for(int i = 0; i < trees_amount; i++) {
- int random_i = 0;
- int random_j = 0;
- boolean spot_free = false;
- while(!spot_free) {
- random_i = random_generator.nextInt(n);
- random_j = random_generator.nextInt(n);
- if(trees[random_i][random_j] == null) {
- spot_free = true;
- }
- }
- int tree_age = random_generator.nextInt(10) + 1;
- int tree_type = random_generator.nextInt(2);
- if(tree_type == 0) trees[random_i][random_j] = new Ash(tree_age);
- else if(tree_type == 1)
- trees[random_i][random_j] = new Beech(tree_age);
- else System.out.println("Unknown tree type");
- }
- }
- /**
- * Checks if tree exists on specified location, and returns it if it does.
- * @param i first coordinate of the tree, from 0 - (n-1).
- * @param j second coordinate of the tree, from 0 - (n-1).
- * @return the tree on the specified location or null.
- */
- public Tree getTree(int i, int j) {
- if(i < n && j < n && i >= 0 && j >= 0) {
- if(trees[i][j] != null) return trees[i][j];
- else return null;
- }
- else {
- //System.out.println("Error: Out of bounds");
- return null;
- }
- }
- /**
- * Calculates the amount of shadow on a tree on a scale from 0 - 1 where
- * 1 is completely covered and 0 is no shadow.
- * @param i First oordinate of tree
- * @param j Second oordinate of tree
- * @return Shadow of tree
- * ---
- * Note: It makes no sense to have the shadow for any tree in a forest
- * calculated in a tree. (Why would you call tree1.shadow(...) when you want
- * shadow for tree2 for example?) shadow() should be found in NewForest,
- * which is why it's here.
- */
- public double shadow(int i, int j) {
- double tree_height;
- double k = 0;
- if(getTree(i,j) != null) tree_height = getTree(i,j).getHeight();
- else {
- //System.out.println("Tree does not exist");
- return 0.0;
- }
- if(getTree(i-1,j) != null && getTree(i-1,j).getHeight() > tree_height)
- k += 1;
- if(getTree(i+1,j) != null && getTree(i+1,j).getHeight() > tree_height)
- k += 1;
- if(getTree(i,j+1) != null && getTree(i,j+1).getHeight() > tree_height)
- k += 1;
- if(getTree(i,j-1) != null && getTree(i,j-1).getHeight() > tree_height)
- k += 1;
- return k / 10;
- }
- /**
- * Equivalent of the growOneYear() method of Forest, this makes every tree
- * in the forest grow one year, this time, however, using the shadow of the
- * tree to calculate growth pct.
- */
- public void growOneYear() {
- int this_i = 0;
- for(Tree[] tree_row : trees) {
- int this_j = 0;
- for(Tree tree : tree_row) {
- if(tree != null) {
- tree.growOneYear(shadow(this_i,this_j));
- this_j++;
- }
- }
- this_i++;
- }
- }
- /**
- * Equivalent of growManyYears() of the Forest class, this method grows the
- * forest several years in a step.
- * @param y Years to grow
- */
- public void growManyYears(int y) {
- for(int i = 0; i < y; i++) {
- growOneYear();
- }
- }
- /**
- * Calls the show() method of every tree in the forest resulting in massive
- * text spam of the output console.
- */
- public void show() {
- for(Tree[] tree_row : trees) {
- for(Tree tree : tree_row) {
- if(tree != null) {
- tree.show();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment