-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSaver.cs
235 lines (204 loc) · 4.71 KB
/
Saver.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Created by SharpDevelop.
* User: user
* Date: 08.10.2013
* Time: 2:46
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Collections.Generic;
namespace Tetris
{
public static class Saver
{
public static string FileName="tetris.sav";
public static HighTimesList HighTimes=new HighTimesList();
public static HighScoresList HighScores=new HighScoresList();
public static void Save(TetrisSave s)
{
bool added=false;
if(HighScores.CanAdd(s))
{ HighScores.Add(s); added=true; }
if(HighTimes.CanAdd(s))
{ HighTimes.Add(s); added=true; }
if(!added) return;
using(FileStream fs=new FileStream(FileName, FileMode.Create, FileAccess.Write))
using(BinaryWriter bw=new BinaryWriter(fs))
{
for(int i=0; i<10; ++i)
{
bw.Write(HighScores[i].UserName);
bw.Write(HighScores[i].Score);
bw.Write(HighScores[i].GameDuration.Ticks);
bw.Write(HighScores[i].GameEnded.Ticks);
}
for(int i=0; i<10; ++i)
{
bw.Write(HighTimes[i].UserName);
bw.Write(HighTimes[i].Score);
bw.Write(HighTimes[i].GameDuration.Ticks);
bw.Write(HighTimes[i].GameEnded.Ticks);
}
}
}
public static void Load()
{
using(FileStream fs=new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using(BinaryReader br=new BinaryReader(fs))
{
List<TetrisSave> loaded=new List<TetrisSave>();
while(br.PeekChar()!=-1)
{
TetrisSave sv=new TetrisSave();
sv.UserName=br.ReadString();
sv.Score=br.ReadInt32();
sv.GameDuration=new TimeSpan(br.ReadInt64());
sv.GameEnded=new DateTime(br.ReadInt64());
loaded.Add(sv);
}
while(loaded.Count<20)
{
loaded.Add(new TetrisSave("---", 0, new TimeSpan(0)));
}
TetrisSave[] sc=new TetrisSave[10], tm=new TetrisSave[10];
for(int i=0; i<10; ++i)
sc[i]=loaded[i];
for(int i=0; i<10; ++i)
tm[i]=loaded[10+i];
for(int i=0; i<10; ++i)
System.Diagnostics.Trace.WriteLine(tm[i].ToString());
HighScores.Write(sc);
HighTimes.Write(tm);
}
}
}
public struct TetrisSave
{
public string UserName;
public int Score;
public DateTime GameEnded;
public TimeSpan GameDuration;
public TetrisSave(string uname, int sc, TimeSpan dur)
{
UserName=uname; Score=sc; GameDuration=dur; GameEnded=DateTime.Now;
}
public override string ToString()
{
return string.Format("[TetrisSave UserName={0}, Score={1}, GameEnded={2}, GameDuration={3}]", UserName, Score, GameEnded, GameDuration);
}
}
public abstract class SavesList
{
protected TetrisSave[] Saves;
public TetrisSave this[int index]
{
get { return Saves[index]; }
set { Saves[index]=value; }
}
public SavesList()
{
Saves=new TetrisSave[10];
for(int i=0; i<10; i++)
{
Saves[i]=new TetrisSave("---", 0, new TimeSpan(0));
Saves[i].UserName="---";
Saves[i].GameEnded=new DateTime(0);
}
}
public void Write(TetrisSave[] svs)
{
for(int i=0; i<10; i++)
Saves[i]=svs[i];
Sort();
}
public abstract void Add(TetrisSave s);
public abstract bool CanAdd(TetrisSave s);
protected abstract void Sort();
}
public sealed class HighScoresList: SavesList
{
public HighScoresList(): base() {}
protected override void Sort()
{
for(int i=0; i<10; i++)
{
int maxInd=i;
for(int j=i; j<10; j++)
{
if(Saves[j].Score>Saves[maxInd].Score)
{
maxInd=j;
}
}
TetrisSave tmp=Saves[maxInd];
Saves[maxInd]=Saves[i];
Saves[i]=tmp;
}
}
public override bool CanAdd(TetrisSave s)
{
Sort();
return Saves[9].Score<s.Score;
}
public override void Add(TetrisSave s)
{
int i;
for(i=9; i>=1; --i)
{
if(Saves[i-1].Score>=s.Score)
{
break;
}
Saves[i]=Saves[i-1];
}
if(Saves[0].Score>s.Score)
Saves[i]=s;
else
Saves[0]=s;
}
}
public sealed class HighTimesList: SavesList
{
public HighTimesList(): base() {}
protected override void Sort()
{
for(int i=0; i<10; i++)
{
int maxInd=i;
for(int j=i; j<10; j++)
{
if(Saves[j].GameDuration>Saves[maxInd].GameDuration)
{
maxInd=j;
}
}
TetrisSave tmp=Saves[maxInd];
Saves[maxInd]=Saves[i];
Saves[i]=tmp;
}
}
public override bool CanAdd(TetrisSave s)
{
Sort();
return Saves[9].GameDuration<s.GameDuration;
}
public override void Add(TetrisSave s)
{
int i;
for(i=9; i>=1; --i)
{
if(Saves[i-1].GameDuration>=s.GameDuration)
{
break;
}
Saves[i]=Saves[i-1];
}
if(Saves[0].GameDuration>s.GameDuration)
Saves[i]=s;
else
Saves[0]=s;
}
}
}