Last active
July 9, 2018 02:17
-
-
Save sankage/96cb5c8605dc6b6a6406dd175c696b48 to your computer and use it in GitHub Desktop.
D&D dice rollers
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
window.Dice || (window.Dice = {}); | |
Dice.roll = function(dice) { | |
let match = /(\d+)?d(\d+)(?:\+(\d+))?/.exec(dice); | |
if (match == null) { | |
return {}; | |
} | |
let number_of_dice = match[1] == undefined ? 1 : parseInt(match[1], 10); | |
let dice_size = parseInt(match[2], 10); | |
let modifier = match[3] == undefined ? 0 : parseInt(match[3], 10); | |
let dice_rolls = []; | |
for (var i = 1; i <= number_of_dice; i++) { | |
dice_rolls.push(Math.floor(Math.random() * (dice_size - 1 + 1)) + 1); | |
} | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
return { | |
quantity: number_of_dice, | |
type: "d" + dice_size, | |
modifier: modifier, | |
dice_rolls: dice_rolls, | |
result: dice_rolls.reduce(reducer, modifier) | |
}; | |
}; | |
Dice.roll("d20") | |
// {quantity: 1, type: "d20", modifier: 0, dice_rolls: [18], result: 18} | |
Dice.roll("10d10") | |
// {quantity: 10, type: "d10", modifier: 0, dice_rolls: [3, 10, 7, 1, 2, 8, 1, 3, 4, 6], result: 45} | |
Dice.roll("4d4+4") | |
// {quantity: 4, type: "d4", modifier: 4, dice_rolls: [2, 3, 3, 4], result: 16} |
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
class Dice | |
def self.roll(dice) | |
match = /(\d+)?d(\d+)(?:\+(\d+))?/.match(dice) | |
return 0 if match.nil? | |
number_of_dice = match[1].nil? ? 1 : match[1].to_i | |
dice_size = match[2].to_i | |
modifier = match[3].to_i | |
(1..number_of_dice).map { rand(1..dice_size) }.reduce(0, &:+) + modifier | |
end | |
end | |
Dice.roll("d20") #=> 18 | |
Dice.roll("10d10") #=> 56 | |
Dice.roll("4d4+4") #=> 18 |
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
javascript:(function () { | |
$(".ct-character-header-desktop__group--gap").after('<div class="ct-character-header-desktop__group ct-character-header-desktop__group--roller"><div class="ct-character-header-desktop__button"><span class="result ct-character-header-desktop__button-label" style="margin-right:0.5rem;color:#b59e54;font-size:2rem;"></span><input style="border:0;text-align:center;width:5rem;"><span class="ct-character-header-desktop__button-label">Roll</span></div></div>'); | |
window.Dice || (window.Dice = {}); | |
Dice.roll = function(dice) { | |
let match = /(\d+)?d(\d+)(?:\+(\d+))?/.exec(dice); | |
if (match == null) { | |
return {}; | |
} | |
let number_of_dice = match[1] == undefined ? 1 : parseInt(match[1], 10); | |
let dice_size = parseInt(match[2], 10); | |
let modifier = match[3] == undefined ? 0 : parseInt(match[3], 10); | |
let dice_rolls = []; | |
for (var i = 1; i <= number_of_dice; i++) { | |
dice_rolls.push(Math.floor(Math.random() * (dice_size - 1 + 1)) + 1); | |
} | |
const reducer = (accumulator, currentValue) => accumulator + currentValue; | |
return { | |
quantity: number_of_dice, | |
type: "d" + dice_size, | |
modifier: modifier, | |
dice_rolls: dice_rolls, | |
result: dice_rolls.reduce(reducer, modifier) | |
}; | |
}; | |
$(".ct-character-header-desktop__group--roller .ct-character-header-desktop__button").on("click", function() { | |
var roll = Dice.roll($("input", this).val()); | |
console.log(roll); | |
$(".result", this).text(roll.result); | |
}); | |
$(".ct-character-header-desktop__group--roller input").on("click", function(event){ | |
event.stopPropagation(); | |
}); | |
}()); |
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
javascript:(function(){$(".ct-character-header-desktop__group--gap").after('<div class="ct-character-header-desktop__group ct-character-header-desktop__group--roller"><div class="ct-character-header-desktop__button"><span class="result ct-character-header-desktop__button-label" style="margin-right:0.5rem;color:#b59e54;font-size:2rem;"></span><input style="border:0;text-align:center;width:5rem;"><span class="ct-character-header-desktop__button-label">Roll</span></div></div>'),window.Dice||(window.Dice={}),Dice.roll=function(e){var r,t,a,c,o,l;if(null==(r=/(\d+)?d(\d+)(?:\+(\d+))?/.exec(e)))return{};t=null==r[1]?1:parseInt(r[1],10),a=parseInt(r[2],10),c=null==r[3]?0:parseInt(r[3],10),o=[];for(var n=1;n<=t;n++)o.push(Math.floor(Math.random()*(a-1+1))+1);return l=function(e,r){return e+r},{quantity:t,type:"d"+a,modifier:c,dice_rolls:o,result:o.reduce(l,c)}},$(".ct-character-header-desktop__group--roller .ct-character-header-desktop__button").on("click",function(){var e=Dice.roll($("input",this).val());console.log(e),$(".result",this).text(e.result)}),$(".ct-character-header-desktop__group--roller input").on("click",function(e){e.stopPropagation()});}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment