在C#中分发点(整数)

时间:2013-01-28 02:23:09

标签: c# arrays points

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

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

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

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

第一名是10分 第二名9 第3名8 7比4 6为第5次

等等。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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;
            }
        }