public void saveHighScore(String name, int points) { //First save to a list of Highscores for sorting List<Highscore> highscoreList = new List<Highscore>(); Highscore newHighscore = new Highscore(name, points);//points); highscoreList.Add(newHighscore); //write to txt file //Delimeter const string DELIM = ","; string totalString = ""; const string personDelim = "_"; if (File.Exists("highscores.txt")) { //read and store highscores in string; FileStream inFile = new FileStream("highscores.txt", FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(inFile); string recordIn = reader.ReadLine(); //totalString = totalString + recordIn; while (recordIn != null) { //totalString = reader.ReadLine(); totalString = totalString + recordIn; recordIn = reader.ReadLine(); } reader.Close(); inFile.Close(); String totalStringForSorting = totalString.Remove(totalString.Length - 1); String[] highscoreSplitIntoHighscores = totalStringForSorting.Split('_'); String[] highscoreSplitIntoRecords = null; for (int i = 0; i < highscoreSplitIntoHighscores.Length; i++) { highscoreSplitIntoRecords = highscoreSplitIntoHighscores[i].Split(','); //highscoreSplitIntoRecordsPU String highscoreName = highscoreSplitIntoRecords[0]; int highscoreScore = Int32.Parse(highscoreSplitIntoRecords[1]); Highscore highscoreNew = new Highscore(highscoreName, highscoreScore); highscoreList.Add(highscoreNew); } highscoreList.Sort(); highscoreList.Reverse(); FileStream outFile = new FileStream("highscores.txt", FileMode.Open, FileAccess.Write); StreamWriter writer = new StreamWriter(outFile); foreach (Highscore h in highscoreList) { writer.WriteLine(h.GetScoreName() + DELIM + h.GetScoreTotal() + personDelim); } writer.Close(); outFile.Close(); ////write old highscores + new highscore //FileStream outFile = new FileStream("highscores.txt", FileMode.Open, FileAccess.Write); //StreamWriter writer = new StreamWriter(outFile); //writer.WriteLine(totalString); //writer.WriteLine(name + DELIM + points + personDelim); //writer.Close(); //outFile.Close(); } else { FileStream outFile = new FileStream("highscores.txt", FileMode.Create, FileAccess.Write); StreamWriter writer = new StreamWriter(outFile); writer.WriteLine(name + DELIM + points + personDelim); writer.Close(); outFile.Close(); } }
public void saveHighScore(String name, int points) { //First save to a list of Highscores for sorting List <Highscore> highscoreList = new List <Highscore>(); Highscore newHighscore = new Highscore(name, points);//points); highscoreList.Add(newHighscore); //write to txt file //Delimeter const string DELIM = ","; string totalString = ""; const string personDelim = "_"; if (File.Exists("highscores.txt")) { //read and store highscores in string; FileStream inFile = new FileStream("highscores.txt", FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(inFile); string recordIn = reader.ReadLine(); //totalString = totalString + recordIn; while (recordIn != null) { //totalString = reader.ReadLine(); totalString = totalString + recordIn; recordIn = reader.ReadLine(); } reader.Close(); inFile.Close(); String totalStringForSorting = totalString.Remove(totalString.Length - 1); String[] highscoreSplitIntoHighscores = totalStringForSorting.Split('_'); String[] highscoreSplitIntoRecords = null; for (int i = 0; i < highscoreSplitIntoHighscores.Length; i++) { highscoreSplitIntoRecords = highscoreSplitIntoHighscores[i].Split(','); //highscoreSplitIntoRecordsPU String highscoreName = highscoreSplitIntoRecords[0]; int highscoreScore = Int32.Parse(highscoreSplitIntoRecords[1]); Highscore highscoreNew = new Highscore(highscoreName, highscoreScore); highscoreList.Add(highscoreNew); } highscoreList.Sort(); highscoreList.Reverse(); FileStream outFile = new FileStream("highscores.txt", FileMode.Open, FileAccess.Write); StreamWriter writer = new StreamWriter(outFile); foreach (Highscore h in highscoreList) { writer.WriteLine(h.GetScoreName() + DELIM + h.GetScoreTotal() + personDelim); } writer.Close(); outFile.Close(); ////write old highscores + new highscore //FileStream outFile = new FileStream("highscores.txt", FileMode.Open, FileAccess.Write); //StreamWriter writer = new StreamWriter(outFile); //writer.WriteLine(totalString); //writer.WriteLine(name + DELIM + points + personDelim); //writer.Close(); //outFile.Close(); } else { FileStream outFile = new FileStream("highscores.txt", FileMode.Create, FileAccess.Write); StreamWriter writer = new StreamWriter(outFile); writer.WriteLine(name + DELIM + points + personDelim); writer.Close(); outFile.Close(); } }