1
\$\begingroup\$

I am creating the combat system of a simple RPG. The only two factors in this system are damage and accuracy.

Accuracy in this game simply means either the attacker HITS or MISSES. There is no partial damage or partial hits. The target has no chance of dodging the attack if the attacker HITS. A player's accuracy is represented with a percentage, e.g. 75% of the time the player will HIT.

In combat, the damage formula determines the damage inflicted on a target, but the damage will only be inflicted if the player HITS the target.

I have already created a damage formula; however, I do not know how to create a simple and practical accuracy formula. I have thought of this, but it's quite impractical because of how it will need to be manipulated if the player's accuracy improves/increases:

  • An array is filled (Player's accuracy)% with 1's, remaining percent with 0's
  • At random, one index of the array is chosen
  • If it's a 1, the player HITS; if it's a 0, the player MISSES

I have looked around this network at accuracy/hit or miss formulas but they involve other factors such as dexterity and evasion. I've also looked elsewhere online, but again the formulas involve other factors.

Is there a simple and practical formula to accomplish this?

\$\endgroup\$
0
\$\begingroup\$

A random number generator and a simple test is all you need, for example, in C#:

Random rnd = new Random();
int value = rnd.Next(100);
if(value < accuracyValue) {
    //We have a hit
} else {
    //We have a miss
}

This works when accuracyValue is a number between 0 and 100, representing the player's hit chance. For example, a hit chance of 75% would be accuracyValue = 75;.

We choose a random value between 0 and 100. For any number between 0 and 100, 75 of those numbers will be below 75 (because 0 is included). That means, choosing a random number between 0 and 100 gives us a 75% chance of getting a number below 75. This results in a simple way to get true or false at a specific percentage.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.