Jypeli 10
The simple game programming library
ScoreList.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2009 University of Jyväskylä, Department of Mathematical
4 * Information Technology.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#endregion
25
26/*
27 * Authors: Tomi Karppinen
28 */
29
30
31using System;
32using System.Collections.Generic;
33using System.ComponentModel;
34
35namespace Jypeli
36{
40 [Save]
41 public class ScoreList : INotifyList<ScoreItem>
42 {
43 [Save]
44 [EditorBrowsable(EditorBrowsableState.Never)]
46
50 [Save]
51 public string LastEnteredName = "";
52
56 public int Count
57 {
58 get { return _scores.Length; }
59 }
60
61 public IEnumerator<ScoreItem> GetEnumerator()
62 {
63 int i = 0;
64 while ( i < _scores.Length )
65 {
66 yield return _scores[i++];
67 }
68 }
69
70 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
71 {
72 return GetEnumerator();
73 }
74
84 public ScoreItem this[int position]
85 {
86 get { return _scores[position - 1]; }
87 set
88 {
89 for ( int i = _scores.Length - 1; i > position - 1; i-- )
90 _scores[i] = _scores[i - 1];
91 _scores[position - 1] = value;
92
93 for ( int i = 0; i < _scores.Length; i++ )
94 _scores[i].Position = i + 1;
95
96 OnChanged();
97 }
98 }
99
103 public bool Reverse { get; private set; }
104
108 public event Action Changed;
109
110 private void OnChanged()
111 {
112 if ( Changed != null )
113 Changed();
114 }
115
119 public ScoreList()
120 : this( 10, false, 0 )
121 {
122 }
123
131 public ScoreList( int length, bool reverse, double baseScore, string defaultName )
132 {
133 if ( length < 0 ) throw new ArgumentException( "List length must be more than zero!" );
134 _scores = new ScoreItem[length];
135 Reverse = reverse;
136
137 ScoreItem zeroItem = new ScoreItem( defaultName, baseScore );
138 for ( int i = 0; i < length; i++ )
139 {
140 _scores[i] = zeroItem;
141 _scores[i].Position = i + 1;
142 }
143 }
144
151 public ScoreList(int length, bool reverse, double baseScore)
152 : this(length, reverse, baseScore, "-")
153 {
154 }
155
156 public override int GetHashCode()
157 {
158 return _scores.Length;
159 }
160
166 public override bool Equals( object obj )
167 {
168 var other = obj as ScoreList;
169 if ( other == null || this.Count != other.Count ) return false;
170
171 for ( int i = 0; i < _scores.Length; i++ )
172 {
173 if ( !_scores[i].Equals( other._scores[i] ) )
174 return false;
175 }
176
177 return true;
178 }
179
185 public bool Qualifies( double score )
186 {
187 return Reverse && score < _scores[_scores.Length - 1].Score ||
188 !Reverse && score > _scores[_scores.Length - 1].Score;
189 }
190
197 public int Add( string name, double score )
198 {
199 if ( !Qualifies( score ) )
200 return -1;
201
202 LastEnteredName = name;
203
204 for ( int i = 1; i <= Count; i++ )
205 {
206 if ( !Reverse && score > this[i].Score || Reverse && score < this[i].Score )
207 {
208 this[i] = new ScoreItem( name, score );
209 return i + 1;
210 }
211 }
212
213 throw new InvalidOperationException( "Internal error in HighScoreList!" );
214 }
215 }
216
220 [Save]
221 public struct ScoreItem
222 {
223 public int Position;
224
228 [Save]
229 public string Name;
230
234 [Save]
235 public double Score;
236
242 public ScoreItem( string name, double score )
243 {
244 Position = -1;
245 Name = name;
246 Score = score;
247 }
248
254 public override bool Equals( object obj )
255 {
256 if ( !( obj is ScoreItem ) )
257 return false;
258
259 var other = (ScoreItem)obj;
260 return this.Name == other.Name && Math.Abs( this.Score - other.Score ) <= float.Epsilon;
261 }
262
267 public override int GetHashCode()
268 {
269 return Name.GetHashCode() + Score.GetHashCode();
270 }
271 }
272}
Parhaiden pisteiden lista.
Definition: ScoreList.cs:42
ScoreList(int length, bool reverse, double baseScore)
Luo uuden, tyhjän top-listan.
Definition: ScoreList.cs:151
Action Changed
Tapahtuu kun listan sisältö muuttuu.
Definition: ScoreList.cs:108
string LastEnteredName
Viimeksi kirjoitettu nimi
Definition: ScoreList.cs:51
IEnumerator< ScoreItem > GetEnumerator()
Definition: ScoreList.cs:61
bool Reverse
Käänteinen järjestys, ts. pienempi tulos on parempi.
Definition: ScoreList.cs:103
ScoreList()
Luo tyhjän, 10 sijan top-listan.
Definition: ScoreList.cs:119
int Add(string name, double score)
Lisää nimen ja pisteet listalle, jos tulos on tarpeeksi hyvä.
Definition: ScoreList.cs:197
ScoreItem[] _scores
Definition: ScoreList.cs:45
bool Qualifies(double score)
Tarkistaa, kelpaako tulos listalle.
Definition: ScoreList.cs:185
int Count
Kuinka monta nimeä listalle mahtuu.
Definition: ScoreList.cs:57
void OnChanged()
Definition: ScoreList.cs:110
override bool Equals(object obj)
Tarkistaa, onko kaksi listaa yhtäsuuret.
Definition: ScoreList.cs:166
ScoreList(int length, bool reverse, double baseScore, string defaultName)
Luo uuden, tyhjän top-listan.
Definition: ScoreList.cs:131
override int GetHashCode()
Definition: ScoreList.cs:156
Lista, joka ilmoittaa muutoksistaan.
Definition: INotifyList.cs:11
Nimi ja pisteet.
Definition: ScoreList.cs:222
override int GetHashCode()
Hajautuskoodi
Definition: ScoreList.cs:267
override bool Equals(object obj)
Onko tämä piste-olio sama kuin toinen.
Definition: ScoreList.cs:254
ScoreItem(string name, double score)
Luo uuden sijoituksen listalle.
Definition: ScoreList.cs:242
string Name
Nimi
Definition: ScoreList.cs:229
double Score
Pistemäärä
Definition: ScoreList.cs:235