Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
ShotsHit / ShotsFired * 100? Or did I mis-understand the question..? –  Jaakko Lipsanen 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
add comment

1 Answer

up vote 0 down vote accepted

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?

share|improve this answer
    
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
    
Ah, you're right. Thought about counting actual hits rather than shots that hit. –  Mario Dec 6 '12 at 12:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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