C# 中的分布点(整数)

本文关键字:整数 分布 | 更新日期: 2023-09-27 17:56:11

基本上我正在尝试建立一个积分系统,除了分配积分之外,一切都完成了。

例如,我有一个存储点的数组(键是玩家 ID):

array[0] = 0
array[1] = 0
array[2] = 3
array[3] = 3
array[4] = 5

因此,我必须从以下积分系统:5 代表第 1 名4 代表第 2 名3 代表第 3 名2 代表第 4 名1 代表第 5 名

现在,如果你的并列第二,就像在示例中一样,两个玩家都得到 4 分,最后 2 名玩家得到 2 分,因为他们并列第四,但我也需要以下示例工作的可能性:

array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5

均匀分布的积分,也可以转换为其他积分系统,现在这是动态的,可能有 10 名玩家,在这种情况下,积分系统如下所示:

10 代表第 1 名9 代表第 2 名8 代表第 3 名7 代表第 4 名6 代表第 5 名

等等。

任何帮助将不胜感激。

C# 中的分布点(整数)

这样的东西应该是你要找的。请记住,考虑到我在我的大脑想到它时把它写下来,它并没有完全优化。

我们可以从我们的分数数组开始(根据提供的示例):

int[] scores = { 0, 0, 3, 3, 5 };

接下来,我们可以将数据放入不同的形式,以便更容易排序:

Dictionary<int, int> playerScorePair = new Dictionary<int, int>();
//Loop through scores[] and add each player # and score to the Dictionary.
for (int i = 0; i < scores.Length; i++)
    playerScorePair.Add(i, scores[i]);
playerScorePair = playerScorePair.OrderByDescending(psp => psp.Value)
                                 .ToDictionary(psp => psp.Key, psp => psp.Value);

这将根据每个玩家的分数按降序对字典进行排序。之后,我们可以找出每个玩家进入的位置:

int previousScore = -1, counter = 1, place = 1; //A few starting variables.
int totalPlayers = playerScorePair.Count;
int[] endScores = new int[scores.Length]; //This endScores[] array will hold the final scores for each player based on the place they finished in.
foreach (KeyValuePair<int, int> kvp in playerScorePair)
{
    //If the previous score is not equal to the current score, then we can
    // increment the current place. For example, if players 1-2-3 had scores of
    // 10-10-5 respectively, players 1 and 2 would be in first, but player 3 would be
    // equal to "counter", which would be the 3rd iteration, thus 3rd place.
    if (previousScore != kvp.Value && previousScore != -1)
        place = counter;
    endScores[kvp.Key] = (totalPlayers - place) + 1;
    previousScore = kvp.Value;
    counter++;
}

endScores现在将包含每个玩家的所有分数,具体取决于他们进入的位置。例如,玩家 #2 将是 endScores[2] 等于 4。

使用以下

代码解决了这个问题:

        int maxPointGiven = scoreManager.numPlayers;
        int previousScore = 0;
        int previousPosition = 0;
        int previousPoints = 0;
        int countGiven = 0;
        bool newCount = true;
        for (int i = (scoreList.Count - 1); i >= 0; i--)
        {
            if (newCount == true)
            {
                previousPosition = i + 1;
                previousScore = scoreList[i].Value;
                previousPoints = maxPointGiven;
                newCount = false;
            }
            if (scoreList[i].Value == previousScore)
            {
                int oldPoints = int.Parse(dgvPlayers.Rows[scoreList[i].Key].Cells[2].Value.ToString());
                dgvPlayers.Rows[scoreList[i].Key].Cells[2].Value = previousPoints + oldPoints;
                countGiven += 1;
            }
            else
            {
                int oldPoints = int.Parse(dgvPlayers.Rows[scoreList[i].Key].Cells[2].Value.ToString());
                previousPosition = i + 1;
                previousScore = scoreList[i].Value;
                previousPoints = maxPointGiven - countGiven;
                dgvPlayers.Rows[scoreList[i].Key].Cells[2].Value = previousPoints + oldPoints;
                countGiven += 1;
            }
        }