-1

Basically I am trying to make a points system, everything is done except from distributing points.

For example, I have an array that stores the points (Key is the players ID):

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

So from this I have to go to the following points system: 5 for 1st 4 for 2nd 3 for 3rd 2 for 4th 1 for 5th

Now if your tied second, like in the example, both players get 4 points and the last 2 players get 2 points because they are tied 4th, but I need the possibility of the following example working as well:

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

Evenly distributed points, which can also convert to the other points system, now this is dynamic, there could be 10 players, in which case the points system goes like the following:

10 for 1st 9 for 2nd 8 for 3rd 7 for 4th 6 for 5th

and so forth.

Any help would be much appreciated.

Daniel McAssey
  • 436
  • 5
  • 17

2 Answers2

1

Something like this should be what you are looking for. Keep in mind it isn't exactly optimized considering I was writing it down as my brain thought of it.

We can start out by having our array of scores (as per the example provided):

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

Next, we can put the data into a different form so that it will be easier to sort:

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);

This sorts the dictionary in descending order based on the scores of each player. After that, we can find out what place each player came in:

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 will now contain all the scores for each player based on the place that they came in. For example, player #2 would be endScores[2] which would be equal to 4.

Ichabod Clay
  • 1,981
  • 13
  • 17
0

Solved this using the following code:

        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;
            }
        }
Daniel McAssey
  • 436
  • 5
  • 17