test2
by davidelrizzo
JavaScript
Math.randInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
var Dice = {
// standardDiceRoll Return total of rolling n standard (six sided) dice + y given as a string "(n)d(+y)"
standard: function(roll) {
if (!isNaN(Number(roll))) {
return parseInt(roll);
} else {
var total = 0;
for (var i = 0; i < parseInt(roll.split("d")[0]); i++) {
total += Math.randInt(1, 6);
}
var plus = parseInt(roll.split("d")[1]);
if (isNaN(Number(plus))) plus = 0;
return total += plus;
}
},
// Roll n combat dice (3x skulls, 2x white shields, 1x black shields)
// Reurn array of results + total of "s" skulls, "w" white shields and "b" black shields
combat: function(n) {
n = Math.abs(Math.round(n));
if (isNaN(Number(n))) {
return false;
} else {
var result = {
"rolls": [],
"s": 0,
"w": 0,
"b": 0
}
for (var i = 0; i < n; i++) {
var rand = Math.randInt(1, 6);
if (rand < 4) {
result.rolls.push("s");
result.s++;
} else if (rand < 6) {
result.rolls.push("w");
result.w++;
} else {
result.rolls.push("b");
result.b++;
}
}
return result;
}
},
// Return array of "miss","hit","block"s with given number of attackDice and defenceDice
// heroDefender = true if hero is defending otherwise monster is defending
resolveCombat: function(attackDice, defenceDice, heroDefender) {
var attack = Dice.combat(attackDice);
var defence = Dice.combat(defenceDice);
var result = []
for (var i = 0; i <...