计算最终得分的公式

时间:2015-08-14 21:41:42

标签: c# math unity3d

我在下面的一个计算难题的最后一块上停留了。我知道如何从总的正确可能部分生成正确部分的百分比分数((correctNumPartsOnBoard / totalPossibleCorrectParts)* 100)但我想要最终的百分比分数来计算板上不正确部分的数量。 (即使所有正确的部件都在板上,如果还有不正确的部件,你仍然不会获得100%)。现在我的当前公式percentCorrectParts = ((correctNumPartsOnBoard / totalPossibleCorrectParts) / totalNumPartsOnBoard) * 100);是错误的,我在确定正确的计算方面遇到了麻烦。

因此,计算需要工作的方式是:用户需要匹配六种可能的动物中的一种,每只动物有大约15个正确的部分,但是用户也可以将不正确的部分拖到板上(部分来自另一个)动物仍然是可见的,所以他们可以在蜥蜴头上拖动一组不同的腿或角,他们也可以这样做弗兰肯斯坦式的生物)。所以可用的零件总数将是6 * 15。但是看看他们并不都是正确的,他们也会影响得分,同时将棋盘上棋子的平均得分降低。

这是什么正确的公式?

// Scoring System

using UnityEngine;
using System.Linq;
using System.Collections.Generic;

public class ScoreManager : MonoBehaviour
{
    public List<string> totalBuildBoardParts;       // Running list of all parts on board (by Tag)
    public int numCorrectPartsOnBoard;
    public int numIncorrectPartsOnBoard;
    public int totalPossibleCorrectParts;
    public float percentCorrectParts;

    void Start()
    {
        GameObject gameController = GameObject.FindGameObjectWithTag("gc");
        GameSetup gameSetup = gameController.GetComponent<GameSetup>();

        totalPossibleCorrectParts = gameSetup.totalPossibleCorrectParts;
        Debug.Log("TOTAL POSSIBLE CORRECT PARTS ARE: " + totalPossibleCorrectParts);
    }

    public void AddAnimalPartByTag(string tag)
    {
        // Add object tag to List
        totalBuildBoardParts.Add(tag);

        Debug.Log ("Added an object tagged as: " + tag);

        GameObject gameController = GameObject.FindGameObjectWithTag("gc");
        GameSetup gameSetup = gameController.GetComponent<GameSetup>();

        if (tag == gameSetup.activeTag)
        {
            numCorrectPartsOnBoard ++;

            Debug.Log ("There are " + numCorrectPartsOnBoard + " correct parts on the board");
        } else {
            numIncorrectPartsOnBoard ++;
        }

        CalculateScore();
    }

    public void RemoveAnimalPartByTag(string tag)
    {
        // Add object tag to List
        totalBuildBoardParts.Remove(tag);

        Debug.Log ("Removed an object tagged as: " + tag);

        GameObject gameController = GameObject.FindGameObjectWithTag("gc");
        GameSetup gameSetup = gameController.GetComponent<GameSetup>();

        if (tag == gameSetup.activeTag)
        {
            numCorrectPartsOnBoard --;

            Debug.Log ("There are " + numCorrectPartsOnBoard + " correct parts on the board");
        } else {
            numIncorrectPartsOnBoard --;
        }

        CalculateScore();
    }

    public void CalculateScore()
    {
        float totalNumPartsOnBoard = totalBuildBoardParts.Count();
        float correctNumPartsOnBoard = numCorrectPartsOnBoard; 
        percentCorrectParts = ((correctNumPartsOnBoard / totalPossibleCorrectParts) / totalNumPartsOnBoard) * 100);
        Debug.Log ("Your current score is: " + percentCorrectParts);
    }
}

3 个答案:

答案 0 :(得分:3)

你的公式可能是正确的。但是,您的数据类型不是。

您目前正在执行整数除法,这也会产生int。我们假设correctNumPartsOnBoard为3而totalPossibleCorrectParts为5,3/5给出0,因为int没有任何小数。

您需要将除法中的两个操作数之一转换为带小数的数据类型(例如floatdoubledecimal):

percentCorrectParts = ((correctNumPartsOnBoard / (float)totalPossibleCorrectParts) / totalNumPartsOnBoard) * 100);

通过将分母totalPossibleCorrectParts设置为浮点数,第一个除法将返回float。然后在第二个分区中使用float,并正确返回float

答案 1 :(得分:2)

I think your formula should look like this:

int correctParts;
int possibleCorrect;
int incorrectParts;
int parts;

float percentFinished =
    Mathf.Max((((float)correctParts/possibleCorrect)   // Correct percent
    - ((float)incorrectParts/parts))                   // Minus incorrect percent
    * 100f,                                            // Normalized to 100
    0f);                                               // Always a minimum of 0

Also with this formula unlike other answers, you don't have to use all of the parts to get 100%, just get the total possible correct parts which doesn't necessarily have to use up all of your parts ;)

Scenario

Lets say you have 100 parts, with 3 right and 3 wrong. Total right we are aiming for here is 20.

int correctParts = 3;
int possibleCorrect = 20;
int incorrectParts = 3;
int parts = 100;

float percentFinished =
    Mathf.Max((((float)correctParts/possibleCorrect)   // Correct percent is 0.15 or 15%
    - ((float)incorrectParts/parts))                   // Minus incorrect percent which is .03 or 3%
    * 100f,                                            // Normalized to 100 which gives us 15% - 3% = 12%
    0f);                                               // Always a minimum of 0

答案 2 :(得分:0)

我认为您的最终(%年龄)分数应为:   correctNumPartsOnBoard / totalNumPartsOnBoard * 100

如果你有80个正确的部分,20个不正确,那么总的部分是100,你有80个正确,所以你应该得到80%这样: 80 /(80 + 20)* 100