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.

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

As I already mentioned in the comments, it's simply just:

float hitAccuracy = ShotsHit / ShotsFired * 100;

Basically just every time the player fires a shot, increment ShotsFired by one and if the shot hits the target, increment ShotsHit by one.

Of course, there's one problem with this. If Player A fires 100 shots of which 99 hits the target, he would lose to someone who has shot only one shot which hitted the target. This is not necessary a problem, but if it is, could you give us more details about your game?

  • \$\begingroup\$ 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). \$\endgroup\$ – Mario Dec 6 '12 at 11:07
  • \$\begingroup\$ @Mario: Why? If you hit two targets with one shot, ShotsHit would be incremented by one, or the variable ought to be renamed TargetsHit. \$\endgroup\$ – Marcks Thomas Dec 6 '12 at 12:11
  • \$\begingroup\$ Ah, you're right. Thought about counting actual hits rather than shots that hit. \$\endgroup\$ – Mario Dec 6 '12 at 12:40

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.