-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreManager.cs
58 lines (51 loc) · 1.67 KB
/
ScoreManager.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace MyPersonalTetris
{
public class ScoreManager
{
private readonly string highScoreFile;
private readonly int[] ScorePerLines = { 1, 40, 100, 300, 1200 };
public ScoreManager(string highScoreFile)
{
this.highScoreFile = highScoreFile;
this.HighScore = this.GetHighScore();
}
public int Score { get; private set; }
public int HighScore { get; private set; }
public void AddToScore(int level, int lines)
{
this.Score += ScorePerLines[lines] * level;
if (this.Score > this.HighScore)
{
this.HighScore = this.Score;
}
}
public void AddToHighScore()
{
File.AppendAllLines(this.highScoreFile, new List<string>
{
$"[{DateTime.Now.ToString()}] {Environment.UserName} => {this.Score}"
});
}
private int GetHighScore()
{
var highScore = 0;
if (File.Exists(this.highScoreFile))
{
var allScores = File.ReadAllLines(this.highScoreFile);
foreach (var score in allScores)
{
var match = Regex.Match(score, @" => (?<score>[0-9]+)");
highScore = Math.Max(highScore, int.Parse(match.Groups["score"].Value));
}
}
return highScore;
}
}
}