I want to add a random modifier to this code, something that will either add to or subtract from the damage based off of a range of +-20% of the base value. The damage would randomize between a range of 80% of result to 120% of result. For a numeric example:
attacker.Strength(20) – defender.Defense(10) = result
20 – 10 = range(8 to 12)
var Fight = function (attacker, defender) {
var result;
result = (attacker.Strength - defender.Defense);
defender.HP = defender.HP - result;
if(defender.HP >= 1) {
return defender.Name + " has taken " + result + " damage!";
} else {
return defender.Name + " has been slain";
}
};
Math.randomreturns a number between zero and one. Map this to your range from0.8to1.2with this:Not sure whether you want to round somewhere so that your player’s health points are always natural numbers. If not, you should change your death condition to
defender.HP > 0.