コード例 #1
0
        /// <summary>
        /// Reads and sorts highscore from the txt file path
        /// </summary>
        /// <returns> a list of sorted highscore data</returns>
        public List <HighScoreData> ReadHighScores()
        {
            if (File.Exists(mFilePath))
            {
                StreamReader sr           = new StreamReader(mFilePath);
                string       fileContents = sr.ReadToEnd();
                sr.Close();

                string[] linesInFile;
                linesInFile = fileContents.Split("\n"[0]);

                mHighScores.Clear();
                foreach (string line in linesInFile)
                {
                    string[] splitArray = line.Split(',');
                    if (!string.IsNullOrEmpty(splitArray[0]) && !string.IsNullOrEmpty(splitArray[1]))
                    {
                        HighScoreData highScore = new HighScoreData(splitArray[0], float.Parse(splitArray[1]));
                        mHighScores.Add(highScore);
                    }
                }

                //Sort Scores
                mHighScores = SortHighScores(mHighScores);
            }
            return(mHighScores);
        }
コード例 #2
0
 private List <HighScoreData> SortHighScores(List <HighScoreData> highscores)
 {
     if (highscores.Count > 1)
     {
         for (int i = 0; i < highscores.Count; i++)
         {
             for (int j = 0; j < highscores.Count - 1; j++)
             {
                 if (highscores[j]._Score < highscores[j + 1]._Score)
                 {
                     HighScoreData temp = highscores[j + 1];
                     highscores[j + 1] = highscores[j];
                     highscores[j]     = temp;
                 }
             }
         }
     }
     return(highscores);
 }