Last active
August 29, 2015 14:05
-
-
Save sma/d0151b1b56144c240846 to your computer and use it in GitHub Desktop.
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
function d(n) { | |
return Math.floor(Math.random() * n) + 1; | |
} | |
function attack(attacker, enemy) { | |
var a = d(20), p = d(20); | |
if (a <= attacker.at) { | |
if (a === 1) a = attack.at; | |
if (p <= enemy.pa) { | |
if (p === 1) p = enemy.pa; | |
a -= p; | |
} | |
if (a > 0) { | |
enemy.lp -= a; | |
return enemy.lp <= 0; | |
} | |
} | |
return false; | |
} | |
function turnsToKillAlric() { | |
var alrik = { | |
lp: 21, | |
at: 13, | |
pa: 13 | |
}; | |
for (var turn = 1; ; turn++) { | |
if (attack(alrik, alrik)) { | |
return turn; | |
} | |
} | |
} | |
var turns = 0, count = 10000; | |
for (var i = 0; i < count; i++) { | |
turns += turnsToKillAlric(); | |
} | |
console.log(turns / count); |
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
function d(n) { | |
return Math.floor(Math.random() * n) + 1; | |
} | |
function attack(attacker, enemy) { | |
if (d(20) <= attacker.at) { | |
if (!(d(20) <= enemy.pa)) { | |
enemy.lp -= Math.max(attacker.weapon() - enemy.rs, 0); | |
return enemy.lp <= 0; | |
} | |
} | |
return false; | |
} | |
function sword() { | |
return d(6) + 4; | |
} | |
function turnsToKillAlric() { | |
var alrik = { | |
lp: 30, | |
at: 10, | |
pa: 4, | |
rs: 3, | |
weapon: sword | |
}; | |
for (var turn = 1; ; turn++) { | |
if (attack(alrik, alrik)) { | |
return turn; | |
} | |
} | |
} | |
var turns = 0, count = 10000; | |
for (var i = 0; i < count; i++) { | |
turns += turnsToKillAlric(); | |
} | |
console.log(turns / count); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment