Last active
December 13, 2016 17:42
-
-
Save chrisforrence/5880258 to your computer and use it in GitHub Desktop.
Rolls an N-sided die M times, keeping the top P results.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var FORTITUDE = 1, WILL = 2, REFLEX = 3; | |
var Player = { | |
"player": "", | |
"name": "", | |
"race": "", | |
"classes": [], | |
"fortitude": 0, | |
"reflex": 0, | |
"will": 0, | |
"strength": 0, | |
"dexterity": 0, | |
"constitution": 0, | |
"wisdom": 0, | |
"intelligence": 0, | |
"charisma": 0, | |
"items": [], | |
"skills": [], | |
"spells": { | |
"known": [] | |
} | |
}; | |
Player.save = function(type, dc, modifiers) { | |
var def = roll(20); | |
console.log("Rolled, " + def); | |
if(type === FORTITUDE) def += this.fortitude; | |
if(type === REFLEX) def += this.reflex; | |
if(type === WILL) def += this.will; | |
console.log("After base saves, " + def); | |
if(modifiers !== undefined) { | |
for(var i = 0; i < modifiers.length; i++) { | |
def += modifiers[i]; | |
} | |
} | |
console.log("After modifiers, " + def); | |
console.log(def + (def >= dc ? " succeeds " : " fails ") + "against DC " + dc); | |
return def >= dc; | |
}; | |
function roll(spots, total, count) | |
{ | |
if(spots === undefined || parseInt(spots) < 1) spots = 6; | |
if(total === undefined || parseInt(total) < 1) total = 1; | |
if(count === undefined || parseInt(count) < 1 || parseInt(count) > total) count = total + 0; | |
var res = new Array(); | |
for(var i = 0; i < total; i++) res.push(Math.floor(Math.random() * (spots)) + 1); | |
res.sort(function(a,b){return b-a}); | |
res = res.slice(0, count); | |
var total=0; | |
for(var i in res) total += res[i]; | |
return total; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment