Search code examples
c#arrayssortingreverse

C# Array Scoring System


Im having problems with sorting out the scoring system for my game. The problem arrises when i sort out the "HighScoresPointsLevel1" array because despite the array being displayed in the correct order it has no link with the "HighScoresNameLevel1". In other words a high score for the game would be assigned to a random players name.

Possible solution? I was thinking that if i was able to pass two parameters (HighScoresPointsLevel1 , HighScoresNameLevel1) inside of the array.sort/array.reverse but the problem is that array.reverse dosent accept two parameters. The reason why i thought this would work is because by calling array.sort the first time i was able to establish a relationship between the arrays "HighScoresLevel1", "HighScoresNameLevel1".

Is there any possible solutions to this issue. Any Help would be appreciated. Thanks.

     string PlayersName = "Player's Name";
     float[] HighScoresLevel1 = new float[5];
     float[] HighScoresPointsLevel1 = new float[5];
     string[] HighScoresNameLevel1 = new string[5];

     public static void addLastScoreLevel1(
       float newScore,
       float newPoints, 
       float[] HighScoresLevel1,
       float[] HighScoresPointsLevel1, 
       string[] HighScoresNameLevel1, 
       string PlayersName)
     {
        if (newScore < HighScoresLevel1[4])
        {
            HighScoresLevel1[4] = newScore;
            HighScoresPointsLevel1[4] = newPoints;
            HighScoresNameLevel1[4] = PlayersName;
            Array.Sort(HighScoresLevel1, HighScoresNameLevel1);
            Array.Sort(HighScoresPointsLevel1);
            Array.Reverse(HighScoresPointsLevel1);
        }
     }

Edit: After looking at the feedback from "Kieran Devlin", i have implemented the changes and so far so good , but im having problems printing the list. So within my other form when trying to display the content of the players list within a the list box, the list box only displays Game.Player.

public partial class MainMenu : Form
{

    public static List<Player> GetPlayers(float newScore, float newPoints, 
    string PlayersName)
    {
        var players = new List<Player>();
        var newPlayer = new Player
        {
            Name = PlayersName,
            Points = newPoints,
            Timer = newScore
        };
        players.Add(newPlayer);

        var TopTenLevel1 = players.OrderByDescending(x => x.Timer).Take(10);
        return players;
    }
 }

public partial class HighScoresMenu : Form
{
        foreach (var Player in MainMenu.GetPlayers(newScore, newPoints, 
        PlayersName))
        {
            ListBoxLevel1.Items.Add(Player);
        }
}

Solution

  • Use objects to group data that will give you more context

    public class Player
    {
        public string Name { get; set; }
        public int Points { get; set; }
        public int Level { get; set; }
    }
    

    Then you can use it like so:

    var players = new List<Player>();
    var newPlayer = new Player
    {
        Name = "Some name",
        Points = 10,
        Level = 3
    };
    
    highscore.Add(newPlayer);
    

    And if you want to get the top ten players by a field:

    var topTenByLevel = players
        .OrderByDecending(x => x.Level)
        .Take(10);