I am building a Pathfinder Character Generator, but struggling to get the stats rolls high enough. So I'd like to tell it to reroll unless the total modifier is higher than 10.
Dice are rolled 4 times, and the three highest rolls are added to make a single stat. Six stats are served into an array to work from. The total modifier is calculated by adding all six stats together, subtract 60 and divide that number by 2. So I've got all that shown here.
I assume I'm looking for an if/else statment now where if checkModifier() < 10 then run rollStat() again else move forward. But I'm not sure what that would look like. Or would it be better to simplify the whole thing into a single function and then run it as a while statement?
function makeDie(sides) {
var die = function () {
return 1 + Math.random() * sides | 0;
};
die.times = function (count) {
var rolls = [];
for(var i = 0 ; i < count ; i++) {
rolls.push(this());
}
return rolls;
};
return die;
}
var rollStat = function () {
var dieRolls = makeDie(5).times(4);
for (var a = 0; a < dieRolls.length; a++) {
a = a + 1;
}
var sortedRolls = dieRolls.sort();
var singleStat = 0;
for (var b = 1; b < sortedRolls.length; b++) {
singleStat += sortedRolls[b];
}
var sixStats = [];
for (var c = 0; c < 6; c++) {
sixStats.push(singleStat);
}
return sixStats;
};
var stats = rollStat();
var stats = stats.sort(function (a, b) {
return a - b;
});
var checkModifier = function() {
var total = 0;
for (var d in stats) {
total += stats[d];
}
return (total-60)/2;
};
Aucun commentaire:
Enregistrer un commentaire