-
Notifications
You must be signed in to change notification settings - Fork 0
/
Results.cs
75 lines (68 loc) · 2.58 KB
/
Results.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
using System;
using System.Collections.Generic;
using System.IO;
namespace Snake
{
public class Results
{
public const string RESULTS_FILE = "results.dat";
public static IEnumerable<KeyValuePair<int, KeyValuePair<int, string>>> ReadResults()
{
FileStream file = new FileStream(RESULTS_FILE, FileMode.OpenOrCreate);
StreamReader readFile = new StreamReader(file);
try
{
//opening the file which is containing the results
string level;//going to contain game levels
string score;//going to contain game score
string name;//going to contain player's name
List<KeyValuePair<int, KeyValuePair<int, string>>> results =
new List<KeyValuePair<int, KeyValuePair<int, string>>>();
//reading the file
while ((level = readFile.ReadLine()) != null)
{
name = readFile.ReadLine();
score = readFile.ReadLine();
if (score != null)
{
//reading the user's name and scores
KeyValuePair<int, string> temp =
new KeyValuePair<int, string>(Convert.ToInt32(score), name);
//reading game's level
KeyValuePair<int, KeyValuePair<int, string>> element =
new KeyValuePair<int, KeyValuePair<int, string>>(Convert.ToInt16(level), temp);
results.Add(element);
}
}
readFile.Close();
file.Close();
return results;
}
catch (Exception)
{
readFile.Close();
file.Close();
FixResults();
}
return null;
}
private static void FixResults()
{
try
{
FileStream stream = new FileStream(RESULTS_FILE, FileMode.Truncate);
stream.Close();
}
catch (Exception) { }
}
public static void AddResult(int level, string username, int score)
{
FileStream resultsFile = new FileStream(RESULTS_FILE, FileMode.Append);
StreamWriter writeToFile = new StreamWriter(resultsFile);
writeToFile.WriteLine(level + "\n" + username + "\n" + score);
writeToFile.Flush();
writeToFile.Close();
resultsFile.Close();
}
}
}