Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class newForest {
  4.     Tree[][] trees;
  5.     int n;
  6.     public static final boolean DEBUG = true;
  7.  
  8.     public newForest(){
  9.         n = 4;
  10.         trees = new Tree[n][n];
  11.  
  12.         Random rand = new Random();
  13.         int range = rand.nextInt((n*n - n*2) + 1) + n*2;  // decides how many tree there is going to be planted
  14.         int[] treeType = new int[range]; // treeType is an array there is going to hold 0,1's where 0 represents Ash and 1 represents Beech
  15.         if(DEBUG) System.out.println("Lenght of treeType: " + treeType.length);
  16.  
  17.         // Populates treeType with 0 and 1 (random generated)
  18.         for (int i = 0; i < range;i ++){
  19.             treeType[i] = rand.nextInt(2);
  20.         }
  21.  
  22.         int plantedTrees = 0; // plantedTrees
  23.  
  24.         // populate trees with Ash and Beech trees.
  25.         while (plantedTrees < range){
  26.             for (int i = 0; i < range; i++) {
  27.                 int lower = rand.nextInt((n) ) ;
  28.                 int upper = rand.nextInt((n) ) ;
  29.  
  30.                 if (DEBUG) System.out.println( "Lowe: " + lower +"\nUpper: " + upper);
  31.  
  32.                 if ( trees[lower][upper] == null ){
  33.                     if (treeType[i] == 0) {
  34.                         trees[lower][upper] = new Ash();
  35.                         plantedTrees++;
  36.                         if(DEBUG) System.out.println("Ash placed");
  37.                     } else {
  38.                         trees[lower][upper] = new Beech();
  39.                         plantedTrees++;
  40.                         if(DEBUG) System.out.println("Beech placed");
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         if (DEBUG) System.out.println("Number of trees " + plantedTrees); // prints number of printed trees.
  46.  
  47.         /* Prints a model of the forest.
  48.         *  [x] represents a tree
  49.         *  [ ] represents an empty place ( null in the array )
  50.         */
  51.         if (DEBUG) {
  52.             for (int i = 0; i < n ; i++) {
  53.                 for (int j = 0; j < n ; j++) {
  54.                     if (trees[i][j] == null) {
  55.                         System.out.print("[ ]");
  56.                     } else {
  57.                         System.out.print("[x]");
  58.                     }
  59.                 }
  60.                 System.out.println();
  61.             }
  62.         }
  63.  
  64.  
  65.  
  66.     }
  67.  
  68.     public Tree getTree(int i, int j){
  69.         return trees[i][j];
  70.     }
  71.  
  72.     public static void main(String[] args) {
  73.         newForest forest = new newForest();
  74.  
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement