/** @link http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array */
function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var forestApp = angular.module('ForestApp', ['ngAnimate']);
forestApp.value('Parameters', {
/** @var int[] Maximal number of moves by species */
speed: {
bear: 5,
lumberjack: 3,
},
/** @var int[] Initial percentage of each species in the forest */
initialPercentage: {
bear: 2,
lumberjack: 10,
tree: 50,
},
/** @var int[] Spawing rate, in percentage, of new saplings around an existing tree */
spawningPercentage: {
0: 0,
1: 10,
2: 20,
},
/** @var int[] Age of growth for an existing tree */
ageOfGrowth: {
sapling: 12,
tree: 120,
},
/** @var int[] Lumber collected on an existing tree */
numberOfLumbers: {
tree: 1,
elderTree: 2,
},
/** @var int Size of each side of the forest */
forestSize: 20,
/** @var int Number of milliseconds between each tick (month in the forest) */
simulationInterval: 50,
});
forestApp.constant('TREE_STAGE', {
SAPLING: 0,
TREE: 1,
ELDER_TREE: 2,
});
forestApp.factory('Tree', ['Forest', 'Parameters', 'TREE_STAGE', function (Forest, Parameters, TREE_STAGE) {
// Classes which represents a tree
var Tree = function (stage, x, y) {
/** @var TREE_STAGE Current stage of the tree */
this.stage = stage;
/** @var...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.