/
ScoreBoard.cs
86 lines (76 loc) · 2.57 KB
/
ScoreBoard.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CowsAndBulls
{
public class Scoreboard
{
private SortedSet<PlayerScore> scores;
private const int MaxPlayersToShowInScoreboard = 10;
private string fileName = string.Empty;
/// <summary>
/// Reads score results from the score file.
/// </summary>
/// <param name="filename">The score file name or path.</param>
public Scoreboard(string filename)
{
this.fileName = filename;
this.scores = new SortedSet<PlayerScore>();
StreamReader inputStream = new StreamReader(this.fileName);
using (inputStream)
{
while (!inputStream.EndOfStream)
{
string scoreString = inputStream.ReadLine();
this.scores.Add(PlayerScore.Deserialize(scoreString));
}
}
}
/// <summary>
/// Create new object to be storred in the score file.
/// </summary>
/// <param name="name">Name of the player</param>
/// <param name="guesses">Number of guesses</param>
public void AddScore(string name, int guesses)
{
PlayerScore newScore = new PlayerScore(name, guesses);
this.scores.Add(newScore);
}
/// <summary>
/// Save the generated scores from scores to the file.
/// </summary>
/// <param name="filename">The score file name or path.</param>
public void SaveToFile()
{
using (StreamWriter outputStream = new StreamWriter(this.fileName))
{
foreach (PlayerScore gameScore in scores)
{
outputStream.WriteLine(gameScore.Serialize());
}
}
}
public override string ToString()
{
if (scores.Count == 0)
{
return "Top scoreboard is empty." + Environment.NewLine;
}
StringBuilder scoreBoard = new StringBuilder();
scoreBoard.AppendLine("Scoreboard:");
int count = 0;
foreach (PlayerScore gameScore in scores)
{
count++;
scoreBoard.AppendLine(string.Format("{0}. {1}", count, gameScore));
if (count > MaxPlayersToShowInScoreboard)
{
break;
}
}
return scoreBoard.ToString();
}
}
}