daily pastebin goal
41%
SHARE
TWEET

Avdeeva_U_K

a guest Jan 29th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Apple = function (color, age, size) {
  2.     this.age = 0;
  3.     this.color = color;
  4.     this.size = 0;
  5.     this.isDeteriorated = 0;
  6.     this.isFallen = 0;
  7. };
  8. Apple.prototype.toFall = function() {
  9.     this.isFallen = 1;
  10. };
  11. Apple.prototype.toDeteriorate = function() {
  12.     this.isDeteriorated = 1;
  13. };
  14.  
  15. var Tree = function (fruits) {
  16.     this.fruits = fruits;
  17.     this.age = 0;
  18.     this.diedApples = [];
  19. };
  20. Tree.prototype.addApple = function(apple) {
  21.     this.fruits.push(apple);
  22. };
  23. Tree.prototype.howManyFruits = function() {
  24.     return this.fruits.length;
  25. };
  26. Tree.prototype.fillRandomApples = function() {
  27.     var colors = ['red', 'orange', 'green', 'yellow', 'white'];
  28.     var i1 = Math.random() * 4;
  29.     var i2 = Math.random() * 30;
  30.     var i3 = Math.random() * 10;
  31.     var count = Math.random() * 20;
  32.     for (var i = 0; i < count; ++i) {
  33.         my_apple = new Apple(colors[i1], i2, i3);
  34.         this.addApple(my_apple);
  35.     }
  36. }
  37.  
  38. var Garden = function (trees) {
  39.     this.trees = trees;
  40.     this.age = 0;
  41. };
  42. Garden.prototype.addTree = function(tree) {
  43.     this.trees.push(tree);
  44. };
  45. Garden.prototype.fillRandomTrees = function(count) {
  46.     for (var i = 0; i < count; ++i) {
  47.         my_tree = new Tree([]);
  48.         my_tree.fillRandomApples();
  49.         this.addTree(my_tree);
  50.     }
  51. };
  52. Garden.prototype.howManyTrees = function() {
  53.     return this.trees.length;
  54. };
  55. Garden.prototype.howManyApples =function() {
  56.     var count = 0;
  57.     this.trees.forEach(function(item) {
  58.         count += item.howManyFruits();
  59.     });
  60.     return count;
  61. };
  62. Garden.prototype.passDay = function() {
  63.     this.age += 1;
  64.     this.trees.forEach(function (tree) {
  65.         if (tree.age == 30) {
  66.             tree.addApple();
  67.         }
  68.     });
  69.     for (var i = 0; i < this.trees.length; ++i) {
  70.         for (var index = 0; index < this.trees[i].fruits.length; ++index) {
  71.             this.trees[i].fruits[index].age += 1;
  72.             this.trees[i].fruits[index].size += 0.3;
  73.             if (this.trees[i].fruits[index].age >= 30) {
  74.                 this.trees[i].fruits[index].age.isFallen = 1;
  75.             }
  76.             if (this.trees[i].fruits[index].age >= 31) {
  77.                 this.trees[i].fruits[index].age.isDeteriorated = 1;
  78.                 this.trees[i].diedApples.push(this.trees[i].fruits[index]);
  79.                 this.trees[i].fruits.splice(index, 1);
  80.  
  81.             }
  82.         }
  83.     }
  84.     var tree_c = 0;
  85.     this.trees.forEach(function(tree, index) {
  86.         for (var j = 0; j < tree.diedApples.length; j++ ) {
  87.             tree.diedApples[j].age += 1;
  88.             if (tree.diedApples[j].age >= 45) {
  89.                 tree.diedApples.splice(j, 1);
  90.                 tree_c++;
  91.             }
  92.         }
  93.     });
  94.     for (var i = 0; i < tree_c; i++) {
  95.         var tree = new Tree([]);
  96.         tree.fillRandomApples();
  97.         this.addTree(tree);
  98.     }
  99. };
  100. Garden.prototype.passManyDays = function(count) {
  101.     for (var i = 0; i < count; ++i)
  102.     {
  103.         this.passDay();
  104.     }
  105. };
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top