-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighScore.pde
More file actions
60 lines (54 loc) · 1.86 KB
/
HighScore.pde
File metadata and controls
60 lines (54 loc) · 1.86 KB
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
class HighScore {
String name;
int score;
HighScore(String info) {
String[] scoreInfo = split(info, ",");
this.name = scoreInfo[0];
this.score = parseInt(scoreInfo[1]);
}
String toString() {
return name + "," + score;
}
void saveScores() {
if(this.score > highScoresList[9].score){
highScoresList = (HighScore[])append(shorten(highScoresList), this);
}
highScoresList = sortHighScores(highScoresList);
PrintWriter output = createWriter("HighScores.txt");
for (int i = 0; i < highScoresList.length; i ++) {
output.println(highScoresList[i]);
}
output.flush(); // Writes the remaining data to the file.
output.close(); // Finishes the file.
}
boolean before(HighScore otherHighScore){
if(otherHighScore.score > this.score){
return true;
}else if(this.score > otherHighScore.score){
return false;
}
//CompareTo function returns negative number if second one is alphabetically before first one.
if(otherHighScore.name.compareTo(this.name) < 0){
return true;
}else if(this.name.compareTo(otherHighScore.name) < 0){
return false;
}
return false;
}
HighScore[] sortHighScores(HighScore[] array){
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array.length; j++){
if(array[j].before(array[i])){
array = swap(array, i, j);
}
}
}
return array;
}
HighScore[] swap(HighScore[] array, int index1, int index2){
HighScore c = array[index1];
array[index1] = array[index2];
array[index2] = c;
return array;
}
}