Skip to content

Instantly share code, notes, and snippets.

@cbillowes
Last active September 3, 2018 02:46
Show Gist options
  • Save cbillowes/80bcf627eeed005541fad72da2a5a7c4 to your computer and use it in GitHub Desktop.
Save cbillowes/80bcf627eeed005541fad72da2a5a7c4 to your computer and use it in GitHub Desktop.
Throwing dice
(function () {
const dice = 3;
const variations = 6;
const point = 10;
function roll() {
let number = Math.round(Math.random() * variations);
return number ? number : 1;
}
function getTallyAmplifier(total) {
if (total > variations * dice) throw ("Cheating!");
if (total === variations * dice) {
return 2.5;
}
if (total >= (variations * dice) / 2) {
return 2;
}
if (total >= (variations * dice) / 3) {
return 1.5;
}
return 1;
}
function tally(total) {
let amplifier = getTallyAmplifier(total);
let score = total + (total * amplifier);
console.log({amplifier, total, score});
return Math.round(score);
}
function play(rounds) {
let score = 0;
for (let round = 1; round <= rounds; round++) {
console.log("");
let total = 0;
for (var turn = 1; turn <= dice; turn++) {
let theRoll = roll();
total += theRoll;
console.log({round, turn, theRoll, total, score});
}
score += tally(total);
}
return score;
}
var score = play(5);
console.log(`Your score is ${score}!`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment