Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlinaWithAFace/c01df5c9311f76251f18a4e7c05fa89b to your computer and use it in GitHub Desktop.
Save AlinaWithAFace/c01df5c9311f76251f18a4e7c05fa89b to your computer and use it in GitHub Desktop.
[2016-06-13] Challenge #271 [Easy] Critical Hit
/*
https://www.reddit.com/r/dailyprogrammer/comments/4nvrnx/20160613_challenge_271_easy_critical_hit/
Description
Critical hits work a bit differently in this RPG. If you roll the maximum value on a die, you get to roll the die again and add both dice rolls to get your final score. Critical hits can stack indefinitely -- a second max value means you get a third roll, and so on. With enough luck, any number of points is possible.
Input
d -- The number of sides on your die.
h -- The amount of health left on the enemy.
Output
The probability of you getting h or more points with your die.
Challenge Inputs and Outputs
Input: d Input: h Output
4 1 1
4 4 0.25
4 5 0.25
4 6 0.1875
1 10 1
100 200 0.0001
8 20 0.009765625
Secret, off-topic math bonus round
What's the expected (mean) value of a D4? (if you are hoping for as high a total as possible).
*/
var d = 6;// prompt("How many sides are on your die?", "6");
var h = 10; // prompt("What's the enemy's starting hp?");
var crit = function(d,h) {
var dieroll = Math.floor(Math.random() * d) + 1;
console.log("You rolled a " + dieroll);
h -= dieroll;
console.log("The enemy's hp is now " + h + ".");
if (dieroll === d) {
console.log("Critical hit! Roll again.");
crit(d,h);
}
};
crit(d,h);
@AlinaWithAFace
Copy link
Author

This only does the critical hit portion of the challenge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment