Jypeli  5
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 
31 using System;
32 using System.Collections.Generic;
33 using System.Text;
34 
35 namespace Jypeli
36 {
40  [Save]
41  public class ScoreList : INotifyList<ScoreItem>
42  {
43  [Save]
44  internal ScoreItem[] _scores;
45 
46  [Save]
47  internal string LastEnteredName = "";
48 
52  public int Count
53  {
54  get { return _scores.Length; }
55  }
56 
57  public IEnumerator<ScoreItem> GetEnumerator()
58  {
59  int i = 0;
60  while ( i < _scores.Length )
61  {
62  yield return _scores[i++];
63  }
64  }
65 
66  System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
67  {
68  return GetEnumerator();
69  }
70 
80  public ScoreItem this[int position]
81  {
82  get { return _scores[position - 1]; }
83  set
84  {
85  for ( int i = _scores.Length - 1; i > position - 1; i-- )
86  _scores[i] = _scores[i - 1];
87  _scores[position - 1] = value;
88 
89  for ( int i = 0; i < _scores.Length; i++ )
90  _scores[i].Position = i + 1;
91 
92  OnChanged();
93  }
94  }
95 
99  public bool Reverse { get; private set; }
100 
104  public event Action Changed;
105 
106  private void OnChanged()
107  {
108  if ( Changed != null )
109  Changed();
110  }
111 
115  public ScoreList()
116  : this( 10, false, 0 )
117  {
118  }
119 
127  public ScoreList( int length, bool reverse, double baseScore, string defaultName )
128  {
129  if ( length < 0 ) throw new ArgumentException( "List length must be more than zero!" );
130  _scores = new ScoreItem[length];
131  Reverse = reverse;
132 
133  ScoreItem zeroItem = new ScoreItem( defaultName, baseScore );
134  for ( int i = 0; i < length; i++ )
135  {
136  _scores[i] = zeroItem;
137  _scores[i].Position = i + 1;
138  }
139  }
140 
147  public ScoreList(int length, bool reverse, double baseScore)
148  : this(length, reverse, baseScore, "-")
149  {
150  }
151 
157  public bool Qualifies( double score )
158  {
159  return Reverse && score < _scores[_scores.Length - 1].Score ||
160  !Reverse && score > _scores[_scores.Length - 1].Score;
161  }
162 
169  public int Add( string name, double score )
170  {
171  if ( !Qualifies( score ) )
172  return -1;
173 
174  LastEnteredName = name;
175 
176  for ( int i = 1; i <= Count; i++ )
177  {
178  if ( !Reverse && score > this[i].Score || Reverse && score < this[i].Score )
179  {
180  this[i] = new ScoreItem( name, score );
181  return i + 1;
182  }
183  }
184 
185  throw new InvalidOperationException( "Internal error in HighScoreList!" );
186  }
187  }
188 
192  [Save]
193  public struct ScoreItem
194  {
195  public int Position;
196 
200  [Save]
201  public string Name;
202 
206  [Save]
207  public double Score;
208 
214  public ScoreItem( string name, double score )
215  {
216  Position = -1;
217  Name = name;
218  Score = score;
219  }
220  }
221 }
bool Reverse
Käänteinen järjestys, ts. pienempi tulos on parempi.
Definition: ScoreList.cs:99
double Score
Pistemäärä
Definition: ScoreList.cs:207
Action Changed
Tapahtuu kun listan sisältö muuttuu.
Definition: ScoreList.cs:104
ScoreItem(string name, double score)
Luo uuden sijoituksen listalle.
Definition: ScoreList.cs:214
Nimi ja pisteet.
Definition: ScoreList.cs:193
bool Qualifies(double score)
Tarkistaa, kelpaako tulos listalle.
Definition: ScoreList.cs:157
string Name
Nimi
Definition: ScoreList.cs:201
ScoreList()
Luo tyhjän, 10 sijan top-listan.
Definition: ScoreList.cs:115
Parhaiden pisteiden lista.
Definition: ScoreList.cs:41
int Add(string name, double score)
Lisää nimen ja pisteet listalle, jos tulos on tarpeeksi hyvä.
Definition: ScoreList.cs:169
ScoreList(int length, bool reverse, double baseScore)
Luo uuden, tyhjän top-listan.
Definition: ScoreList.cs:147
ScoreList(int length, bool reverse, double baseScore, string defaultName)
Luo uuden, tyhjän top-listan.
Definition: ScoreList.cs:127
IEnumerator< ScoreItem > GetEnumerator()
Definition: ScoreList.cs:57
Lista, joka ilmoittaa muutoksistaan.
Definition: INotifyList.cs:12
int Count
Kuinka monta nimeä listalle mahtuu.
Definition: ScoreList.cs:53