Moved dice.js and renamed it

This commit is contained in:
Stefan Zermatten
2015-05-18 13:18:27 +02:00
parent a9648c10cc
commit 66d8a3bfbf
2 changed files with 21 additions and 44 deletions

21
rpg-docs/lib/dice/roll.js Normal file
View File

@@ -0,0 +1,21 @@
roll = function(n, d){
var result = [];
if (!isNaN(n)){
//if only provided 1 argument, it is the dice to roll
if (d === undefined){
d = n;
n = 1;
}
//hard limit the number of dice rolled
if (n > 500){
console.warn(n + " > 500, cannot lift that many dice to roll them");
return;
}
for (var i = 0; i < n; i++){
var roll = Math.floor(Random.fraction() * d + 1);
result.push(roll);
}
return result;
}
return result;
};

View File

@@ -1,44 +0,0 @@
roll = function(n, d){
if (!isNaN(n)){
//first digit is a number
if (d === undefined){
d = n;
n = 1;
}
if (n > 500){
console.log(n + " > 500, cannot lift that many dice to roll them");
return;
}
var result = {sum: 0, rolls: []};
for (var i = 0; i < n; i++){
var roll = Math.floor(Random.fraction() * d + 1);
result.sum += roll;
result.rolls.push(roll);
}
return result;
}
console.log("rolling dice failed for inputs: ", n, d);
return {sum: 0, rolls: []};
};
rollDropLow = function(n, d, drop){
var r = roll(n, d);
r.rolls.sort(function(a, b){return a - b;}); //sort ascending
r.rolls.splice(0, drop); //remove the lowest elements
r.sum = 0;
for (var i = 0, l = r.rolls.length; i , l ; i++){
sum += r.rolls[i];
}
return r;
};
rollDropHigh = function(n, d, drop){
var r = roll(n, d);
r.rolls.sort(function(a, b){return b - a;}); //sort descending
r.rolls.splice(0, drop); //remove the highest elements
r.sum = 0;
for (var i = 0, l = r.rolls.length; i , l ; i++){
sum += r.rolls[i];
}
return r;
};