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!
- var Apple = function (color, age, size) {
- this.age = 0;
- this.color = color;
- this.size = 0;
- this.isDeteriorated = 0;
- this.isFallen = 0;
- };
- Apple.prototype.toFall = function() {
- this.isFallen = 1;
- };
- Apple.prototype.toDeteriorate = function() {
- this.isDeteriorated = 1;
- };
- var Tree = function (fruits) {
- this.fruits = fruits;
- this.age = 0;
- this.diedApples = [];
- };
- Tree.prototype.addApple = function(apple) {
- this.fruits.push(apple);
- };
- Tree.prototype.howManyFruits = function() {
- return this.fruits.length;
- };
- Tree.prototype.fillRandomApples = function() {
- var colors = ['red', 'orange', 'green', 'yellow', 'white'];
- var i1 = Math.random() * 4;
- var i2 = Math.random() * 30;
- var i3 = Math.random() * 10;
- var count = Math.random() * 20;
- for (var i = 0; i < count; ++i) {
- my_apple = new Apple(colors[i1], i2, i3);
- this.addApple(my_apple);
- }
- }
- var Garden = function (trees) {
- this.trees = trees;
- this.age = 0;
- };
- Garden.prototype.addTree = function(tree) {
- this.trees.push(tree);
- };
- Garden.prototype.fillRandomTrees = function(count) {
- for (var i = 0; i < count; ++i) {
- my_tree = new Tree([]);
- my_tree.fillRandomApples();
- this.addTree(my_tree);
- }
- };
- Garden.prototype.howManyTrees = function() {
- return this.trees.length;
- };
- Garden.prototype.howManyApples =function() {
- var count = 0;
- this.trees.forEach(function(item) {
- count += item.howManyFruits();
- });
- return count;
- };
- Garden.prototype.passDay = function() {
- this.age += 1;
- this.trees.forEach(function (tree) {
- if (tree.age == 30) {
- tree.addApple();
- }
- });
- for (var i = 0; i < this.trees.length; ++i) {
- for (var index = 0; index < this.trees[i].fruits.length; ++index) {
- this.trees[i].fruits[index].age += 1;
- this.trees[i].fruits[index].size += 0.3;
- if (this.trees[i].fruits[index].age >= 30) {
- this.trees[i].fruits[index].age.isFallen = 1;
- }
- if (this.trees[i].fruits[index].age >= 31) {
- this.trees[i].fruits[index].age.isDeteriorated = 1;
- this.trees[i].diedApples.push(this.trees[i].fruits[index]);
- this.trees[i].fruits.splice(index, 1);
- }
- }
- }
- var tree_c = 0;
- this.trees.forEach(function(tree, index) {
- for (var j = 0; j < tree.diedApples.length; j++ ) {
- tree.diedApples[j].age += 1;
- if (tree.diedApples[j].age >= 45) {
- tree.diedApples.splice(j, 1);
- tree_c++;
- }
- }
- });
- for (var i = 0; i < tree_c; i++) {
- var tree = new Tree([]);
- tree.fillRandomApples();
- this.addTree(tree);
- }
- };
- Garden.prototype.passManyDays = function(count) {
- for (var i = 0; i < count; ++i)
- {
- this.passDay();
- }
- };
RAW Paste Data

