-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHighScore.cs
43 lines (37 loc) · 1.19 KB
/
HighScore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace NewTetris
{
public class HighScore
{
public string Name { get; set; } = String.Empty;
public int Score { get; set; }
private static string filePath = "highscores.txt";
public static List<HighScore> LoadHighScores()
{
var scores = new List<HighScore>();
if (File.Exists(filePath))
{
var lines = File.ReadAllLines(filePath);
foreach (var line in lines)
{
var parts = line.Split(',');
if (parts.Length == 2 && int.TryParse(parts[1], out int score))
{
scores.Add(new HighScore { Name = parts[0], Score = score });
}
}
}
return scores.OrderByDescending(s => s.Score).ToList();
}
public static void SaveHighScores(List<HighScore> scores)
{
var lines = scores.Select(s => $"{s.Name},{s.Score}");
File.WriteAllLines(filePath, lines);
}
}
}