none
High Scores linked to text file RRS feed

  • Question

  • Hi there! I'm really, really new to programming and am trying to complete an assessment with C#. I have to create a game that is linked to a text file that stores high scores. I can pull the data from the text file into an array, but am not sure how to order the file from high to low and then replace the lowest variable with the user's score if they've gotten higher than it. This is my code for that so far:

            private void HighScores()
            {
                string[] highscoreNameArray = new string[10];
                int[] highscoreScoreArray = new int[10];
                txtHighScoresList.Multiline = true;
                txtHighScoresList.Text = "";
                int index = 0;
                string Filename = @"D:\Documents\highscores.txt"; //Highscore file path set to string variable
                StreamReader Reader; //Declaring reader object
                Reader = File.OpenText(Filename);//Creating reader link to the highscores file
                while (!Reader.EndOfStream)
                {
                    highscoreNameArray[index] = Reader.ReadLine();
                    highscoreScoreArray[index] = int.Parse(Reader.ReadLine());
                    index += 1;
                }
                Reader.Close();
                for (int i = 0; i <= 9; i++)
                {
                    txtHighScoresList.Text += highscoreNameArray[i] + ", ".PadRight(3) + highscoreScoreArray[i];
                    txtHighScoresList.Text += System.Environment.NewLine;
                }
            }

    Any help would be most appreciated!

    Monday, October 16, 2017 8:00 AM

Answers

  • Hi,

    The problem you are going to have here, with your current way of storing the data, is that you have the player names and scores in two separate arrays.

    So if you sort one of the arrays (which you can do using Array.Sort), you will end up with the names and scores mismatched!

    I would recommend creating a new class to hold each name and score together. E.g:

     public class Player
    {
         public Player(string name, int score)
         {
            Name = name;
            Score = score;
          }
    
          public string Name { get; set; }
          public int Score { get; set; }
    }

    The Player class just has two properties: Name and Score, and a constructor that allows you to pass these values in to initialise it.

    So you would create an single array of these Player classes instead of two arrays of names and scores.

    Player[] players = new Player[10];


    In your loop, while reading your file, you add new Players with:

    string name = Reader.ReadLine();
    int score = Parse(Reader.ReadLine());
    players[index] = new Player(name, score);


    Finally, after you have read in your data you can sort the array with

    Array.Sort(players, (p1, p2)=> p1.Score.CompareTo(p2.Score));

    NB: To replace a given player's score you would have to find their Player record in the array and just update the Score property (and then re-sort). Or you could just replace the last Player element in the array with a new Player instance.


    • Edited by RJP1973 Monday, October 16, 2017 8:23 AM
    • Marked as answer by gs8ski Monday, October 16, 2017 9:35 AM
    Monday, October 16, 2017 8:22 AM

All replies

  • Hi,

    The problem you are going to have here, with your current way of storing the data, is that you have the player names and scores in two separate arrays.

    So if you sort one of the arrays (which you can do using Array.Sort), you will end up with the names and scores mismatched!

    I would recommend creating a new class to hold each name and score together. E.g:

     public class Player
    {
         public Player(string name, int score)
         {
            Name = name;
            Score = score;
          }
    
          public string Name { get; set; }
          public int Score { get; set; }
    }

    The Player class just has two properties: Name and Score, and a constructor that allows you to pass these values in to initialise it.

    So you would create an single array of these Player classes instead of two arrays of names and scores.

    Player[] players = new Player[10];


    In your loop, while reading your file, you add new Players with:

    string name = Reader.ReadLine();
    int score = Parse(Reader.ReadLine());
    players[index] = new Player(name, score);


    Finally, after you have read in your data you can sort the array with

    Array.Sort(players, (p1, p2)=> p1.Score.CompareTo(p2.Score));

    NB: To replace a given player's score you would have to find their Player record in the array and just update the Score property (and then re-sort). Or you could just replace the last Player element in the array with a new Player instance.


    • Edited by RJP1973 Monday, October 16, 2017 8:23 AM
    • Marked as answer by gs8ski Monday, October 16, 2017 9:35 AM
    Monday, October 16, 2017 8:22 AM
  • Okay thank you so much! 
    Monday, October 16, 2017 9:35 AM