PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 6, 2022

[FIXED] How to Calculate Accuracy %

 October 06, 2022     c#, math, statistics, unity3d     No comments   

Issue

I have the following scenario. I have a game in Unity where a player is provided with varying amount of targets (we'll say 125 as an example). The accuracy is multi-class in that there is Perfect(bullseye), Great, Good, Miss (where miss is the target is missed entirely, no points awarded). I'm trying to find the right way to calculate a correct accuracy percentage in this scenario. If the player hits every target (125) as Perfect, the accuracy would be 100%. If they hit 124 Perfect and 1 Great, while every target was hit the accuracy percentage would still drop (99.8%). What would be the correct way to calculate this? Balanced Accuracy? Weighted Accuracy? Precision?

I'd like to understand the underlying calculation, not just how to implement this in code.

I appreciate any help I can get with this.


Solution

This can be calculated by assigning each accuracy a score between 0 and 100 (percentage) and then calculating the average score or arithmetic mean for all the shots.

You could use an enum to define the scores for the different accuracies.

public enum Accuracy
{
    Perfect = 100,
    Great = 80,
    Good = 50,
    Miss = 0
}

Then to calculate the average you just need to sum all the accuracy scores together and divide the result by the total number of shots.

int sum = 0;
foreach(Accuracy shot in shotsTaken)
{
    sum += (int)shot;
}

double average = (double)sum / shotsTaken.Count;

Calculating the average can be simplified using System.Linq.

public class Tally
{
    private readonly List<Accuracy> shotsTaken = new List<Accuracy>();

    public void RecordShot(Accuracy shot) => shotsTaken.Add(shot);

    public string CalculateAverageAccuracy() => shotsTaken.Average(shot => (int)shot).ToString("0.#") + "%";
}

You can test the results using this code:

[MenuItem("Test/Tally")]
public static void Test()
{
    var tally = new Tally();
    for(int i = 0; i < 124; i++)
    {
        tally.RecordShot(Accuracy.Perfect);
    }
    tally.RecordShot(Accuracy.Great);
    Debug.Log(tally.CalculateAverageAccuracy());
}

Result:

enter image description here



Answered By - Sisus
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing