Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class newForest {
- Tree[][] trees;
- int n;
- public static final boolean DEBUG = true;
- public newForest(){
- n = 4;
- trees = new Tree[n][n];
- Random rand = new Random();
- int range = rand.nextInt((n*n - n*2) + 1) + n*2; // decides how many tree there is going to be planted
- 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
- if(DEBUG) System.out.println("Lenght of treeType: " + treeType.length);
- // Populates treeType with 0 and 1 (random generated)
- for (int i = 0; i < range;i ++){
- treeType[i] = rand.nextInt(2);
- }
- int plantedTrees = 0; // plantedTrees
- // populate trees with Ash and Beech trees.
- while (plantedTrees < range){
- for (int i = 0; i < range; i++) {
- int lower = rand.nextInt((n) ) ;
- int upper = rand.nextInt((n) ) ;
- if (DEBUG) System.out.println( "Lowe: " + lower +"\nUpper: " + upper);
- if ( trees[lower][upper] == null ){
- if (treeType[i] == 0) {
- trees[lower][upper] = new Ash();
- plantedTrees++;
- if(DEBUG) System.out.println("Ash placed");
- } else {
- trees[lower][upper] = new Beech();
- plantedTrees++;
- if(DEBUG) System.out.println("Beech placed");
- }
- }
- }
- }
- if (DEBUG) System.out.println("Number of trees " + plantedTrees); // prints number of printed trees.
- /* Prints a model of the forest.
- * [x] represents a tree
- * [ ] represents an empty place ( null in the array )
- */
- if (DEBUG) {
- for (int i = 0; i < n ; i++) {
- for (int j = 0; j < n ; j++) {
- if (trees[i][j] == null) {
- System.out.print("[ ]");
- } else {
- System.out.print("[x]");
- }
- }
- System.out.println();
- }
- }
- }
- public Tree getTree(int i, int j){
- return trees[i][j];
- }
- public static void main(String[] args) {
- newForest forest = new newForest();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement