-1

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

例如,我有一个存储点的数组(键是玩家 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 分,因为他们并列第四,但我需要以下示例的可能性:

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

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

第 10 名 第 2 名 9 名 第 3 名 8 名 第 4 名 7 名 第 5 名 6

等等。

任何帮助将非常感激。

4

2 回答 2

1

像这样的东西应该是你正在寻找的东西。请记住,考虑到我是按照大脑的想法写下来的,它并没有完全优化。

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

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。

于 2013-01-28T04:53:27.883 回答
0

使用以下代码解决了这个问题:

        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;
            }
        }
于 2013-01-28T04:46:58.323 回答