# Calculating Hit Accuracy score in a game

I'm currently in the process of making a scoreboard for my game. One of things I would like to display is the players accuracy in the amount of hits they had in game. However, I have never done this before and I've no idea how to go about doing this.

Is there a commonly used algorithm out there that can help me calculate this, or has someone found a way to calculate this fairly easily?

Any help with this would be appreciated.

-
ShotsHit / ShotsFired * 100? Or did I mis-understand the question..? – flai Dec 6 '12 at 9:33
@JaakkoLipsanen Nah I think that's just it, you should post this as answer. – Laurent Couvidou Dec 6 '12 at 9:43
The 'algorithm' is just the addition of one to the accuracy variable, executed on every hit, i.e. `accuracy++;`. – Marcks Thomas Dec 6 '12 at 12:16
-1 If you have to ask how to calculate a percentage, you've definitely not done enough research. – Anko Dec 6 '12 at 21:35

``````float hitAccuracy = ShotsHit / ShotsFired * 100;
Depending on the type of `ShotsFired` and `ShotsHit` you might want to cast them first to avoid integer division. Also I don't consider the number of shots fired being an issue. Such games usually show both values and only consider values for rankings, if a given minimum number of shots have been fired. One more thing to note, which might be more serious issue: If you're able to hit multiple targets, it's possible to get more than 100% (or back to 100%) rather easy (prominent example: Battlefield 3's shotguns). – Mario Dec 6 '12 at 11:07
@Mario: Why? If you hit two targets with one shot, `ShotsHit` would be incremented by one, or the variable ought to be renamed `TargetsHit`. – Marcks Thomas Dec 6 '12 at 12:11