Skip to content

Instantly share code, notes, and snippets.

@mlms13
Last active August 29, 2015 14:26
Show Gist options
  • Save mlms13/9fabb150003e1cbdfa86 to your computer and use it in GitHub Desktop.
Save mlms13/9fabb150003e1cbdfa86 to your computer and use it in GitHub Desktop.
Attacker vs Defender with big armies in risk
function roll() {
return Math.ceil(Math.random() * 6);
}
function compare() {
var one = [roll(), roll(), roll()].sort(function (a, b) { return b-a }).slice(0,2);
var two = [roll(), roll()].sort(function (a, b) { return b-a });
var score = 0;
one.forEach(function (item, index) {
if (item > two[index]) {
score++;
} else {
score--;
}
});
return score;
}
var wins = 0;
var losses = 0
var ties = 0;
for (var i = 0; i < 1000000; i++) {
var result = compare();
if (result > 0) {
wins++;
} else if (result < 0) {
losses++;
} else {
ties++
}
}
console.log(wins, losses, ties);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment