Skip to content

Instantly share code, notes, and snippets.

@RPDeshaies
Last active April 17, 2020 02:16
Show Gist options
  • Save RPDeshaies/501cdc2f3d6b9e8b2afd546f0a30e232 to your computer and use it in GitHub Desktop.
Save RPDeshaies/501cdc2f3d6b9e8b2afd546f0a30e232 to your computer and use it in GitHub Desktop.
Google Slides Fudge Dice
function onOpen() {
SlidesApp.getUi()
.createMenu("Dice")
.addItem("Roll 4df", "runFudgeDice")
.addToUi();
update();
}
function runFudgeDice() {
const fudgeDice = [-1, -1, 0, 0, 1, 1];
const result = rollDice(fudgeDice, 4);
const ui = SlidesApp.getUi();
const title = "You rolled a " + result.toString();
ui.alert(title, "", ui.ButtonSet.OK);
}
function rollDice(dice, times) {
let total = 0;
for (let i = 0; i < times; i++) {
const side = getRandomDiceSide(dice.length);
const roll = dice[side];
total += roll;
}
return total;
}
function getRandomDiceSide(numberOfSides) {
const randomNumber = Math.round(Math.random() * 100);
const side = randomNumber % numberOfSides;
return side;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment