-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScores.java
164 lines (139 loc) · 4.86 KB
/
Scores.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
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
import javafx.util.Pair;
import java.io.*;
import java.util.ArrayList;
public class Scores implements Serializable{
protected ScoreList[] lists;
protected File file;
protected int dif=0;
protected Pair<String,Integer> current;
private class ScoreList extends ArrayList<Pair<String,Integer>>{
//Fonction dajout dans l'ordre croissant avec un maximum de 10 éléments
public boolean add(String name,int score) {
int i = 0;
boolean added=false;
if (this.size() == 0) return super.add(new Pair<>(name, score));
Pair<String, Integer> pair;
int val;
while (i < this.size()) {
pair = this.get(i);
val = pair.getValue();
if (score < val) {
super.add(i, new Pair<>(name, score));
added=true;
break;
}
i++;
}
if(this.size()<10 && !added){
super.add(new Pair<>(name,score));
return true;
}
if (this.size() > 10) this.remove(this.size() - 1);
return true;
}
public boolean add(Scores score){
ScoreList l=score.lists[dif];
for(Pair<String,Integer> p:l){
this.add(p.getKey(),p.getValue());
};
return true;
}
public String toString(){
String scores="";
for(Pair<String,Integer> p: this) scores+=p.getKey()+"-"+MazeInterface.getT(p.getValue())+"\r\n";
return scores;
}
public String toStringR(){
String scores="";
for(Pair<String,Integer> p: this) if(p.getKey()!=current.getKey() && p.getValue()!=current.getValue()) scores+=p.getKey()+"-"+MazeInterface.getT(p.getValue())+"\r\n";
return scores;
}
}
public Scores(){
lists=new ScoreList[1];
for(int i=0;i<lists.length;i++) lists[i]=new ScoreList();
}
public Scores(String path,int dif) throws IOException{
lists=new ScoreList[4];
for(int i=0;i<lists.length;i++) lists[i]=new ScoreList();
this.dif=dif;
file=new File(path);
if(!file.exists()) file.createNewFile();
fillList();
}
public File getFile(){
return file;
}
public ScoreList getList() {
return lists[dif];
}
public Pair<String,Integer> get(int i){
return lists[dif].get(i);
}
public int length(){
return lists[dif].size();
}
public void setCurrent(String str){
String[] tab = str.split("-");
current=new Pair<String,Integer>(tab[0], MazeInterface.getSeconds(tab[1]));
}
/*Fonction pour remplir la liste avec le contenu du ficher
-Les différents niveaux sont séparés par des étoiles dans le fichier et sont répartis chacun dans une des 4 listes
*/
public void fillList(){
try{
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String line=null;
int i=-1;
while((line=br.readLine())!=null){
if(line.equals("**")) i++;
else {
String[] tab = line.split("-");
lists[i].add(tab[0], MazeInterface.getSeconds(tab[tab.length-1]));
}
}
fr.close();
br.close();
}
catch(IOException ex){
ex.printStackTrace();
}
}
//Fonction pour ajouter un score dans le fichier des meilleurs scores
public void addToScoresFile(String name, int score) {
current=new Pair<>(name,score);
lists[dif].add(name, score);
try {
FileWriter fw = new FileWriter(file);
fw.write(listsString());
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public boolean add(Scores score){
return lists[dif].add(score);
}
//Fonction pour ajouter un score uniquement dans le liste pour les classements le temps d'une partie(multijoueur)
public void addToScoresList(String name, int score){
lists[dif].add(name,score);
}
public String getScores(){
return lists[dif].toString();
}
public String listsString(){
String s="";
for(int i=0;i<lists.length;i++){
s+="**\r\n"+lists[i].toString();
}
return s;
}
public String toString(){
return lists[dif].toString();
}
public String getCurrentScore(){
if(current!=null) return current.getKey()+"-"+MazeInterface.getT(current.getValue())+"\r\n";
return "";
}
}