-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathScoreboard.java
61 lines (55 loc) · 1.24 KB
/
Scoreboard.java
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
import java.util.ArrayList;
public class Scoreboard {
private ArrayList<Integer> scores;
private int maxCount;
public Scoreboard(int maxScoreCount) {
scores = new ArrayList<Integer>(maxScoreCount);
maxCount = maxScoreCount;
}
public void addScore(int score) {
if(isHighScore(score))
{
for(int i=0; i<scores.size();i++)
{
if(score > scores.get(i))
{
scores.add(i,score);
if(scores.size() > maxCount)
{
for(int j=maxCount; j<scores.size();j++)
scores.remove(j);
}
return;
}
}
if(scores.size() < maxCount)
scores.add(score);
if(scores.size() > maxCount)
{
for(int k=maxCount; k<scores.size();k++)
scores.remove(k);
}
}
}
public boolean isHighScore(int score) {
for(int i=0; i<=maxCount-1; i++)
{
if(scores.size() < maxCount)
return true;
if(score >= scores.get(i))
return true;
}
return false;
}
public int getScore(int position) {
if (position < scores.size()) {
return scores.get(position);
}
else {
return 0;
}
}
public int getNumScores() {
return scores.size();
}
}