-
Notifications
You must be signed in to change notification settings - Fork 0
/
uHightScore.pas
181 lines (147 loc) · 3.09 KB
/
uHightScore.pas
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
unit uHightScore;
interface
uses
Classes, SysUtils;
type
THightScore = class
private
FNames: TStringList;
FScores: TStringList;
FMax: Integer;
procedure FillTable;
procedure LimitTable(Max: Integer);
procedure Sort;
public
constructor Create;
//
procedure SetMax(Max: Integer);
procedure AddToTable(Player: AnsiString; Score: Integer);
procedure ClearTable;
function GetScores: AnsiString;
function GetNames: AnsiString;
procedure SaveToFile(FileName: AnsiString);
procedure LoadFromFile(FileName: AnsiString);
//
destructor Destroy;
end;
implementation
//* THightScore *//
procedure THightScore.FillTable;
var
i: Integer;
begin
for i:= 0 to FMax - 1 do
AddToTable('Nobody',0);
end;
procedure THightScore.LimitTable(Max: Integer);
var
i: Integer;
begin
if FNames.Count > Max then
for i:= Max to FNames.Count-1 do
begin
FNames.Delete(i);
FScores.Delete(i);
end;
end;
procedure THightScore.Sort;
var
i,j: Integer;
buf: AnsiString;
max: Integer;
begin
for i:= 0 to FScores.Count - 1 do
begin
max:= i;
for j:= i+1 to FScores.Count - 1 do
if StrToInt(FScores.Strings[j]) > StrToInt(FScores.Strings[max]) then max:= j;
buf:= FScores.Strings[i];
FScores.Strings[i]:= FScores.Strings[max];
FScores.Strings[max]:= buf;
buf:= FNames.Strings[i];
FNames.Strings[i]:= FNames.Strings[max];
FNames.Strings[max]:= buf;
end;
end;
// Public
constructor THightScore.Create;
begin
inherited Create;
FMax:= 10;
FNames := TStringList.Create;
FScores:= TStringList.Create;
FillTable;
end;
procedure THightScore.SetMax(Max: Integer);
begin
FMax:= Max;
end;
procedure THightScore.AddToTable(Player: AnsiString; Score: Integer);
var
a: AnsiString;
begin
if Score < 10 then a:= '0000';
if (Score < 100) and (Score > 9) then a:= '000';
if (Score < 1000) and (Score > 99) then a:= '00';
if (Score < 10000) and (Score > 999) then a:= '0';
if (Score < 100000) and (Score > 9999) then a:= '';
if Score > 99999 then Score:= 99999;
FNames.Add(Player);
FScores.Add(a + IntToStr(Score));
Sort;
LimitTable(FMax);
end;
function THightScore.GetNames: AnsiString;
var
i: Integer;
begin
Result:= FNames.Text;
end;
function THightScore.GetScores: AnsiString;
begin
Result:= FScores.Text;
end;
procedure THightScore.LoadFromFile(FileName: AnsiString);
var
F: TextFile;
i: Integer;
S1: AnsiString;
S2: Integer;
begin
ClearTable;
AssignFile(F,FileName);
Reset(F);
for i:= 0 to FMax - 1 do
begin
ReadLn(F,S1);
ReadLn(F,S2);
AddToTable(S1,S2);
end;
CloseFile(F);
end;
procedure THightScore.SaveToFile(FileName: AnsiString);
var
F: TextFile;
i: Integer;
begin
AssignFile(F,FileName);
Rewrite(F);
for i:= 0 to FMax - 1 do
begin
WriteLn(F,FNames.Strings[i]);
WriteLn(F,FScores.Strings[i]);
end;
CloseFile(F);
end;
destructor THightScore.Destroy;
begin
inherited Destroy;
FreeAndNil(FNames);
FreeAndNil(FScores);
end;
procedure THightScore.ClearTable;
begin
FNames.Clear;
FScores.Clear;
end;
end.