-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScore.java
More file actions
79 lines (70 loc) · 2.52 KB
/
Score.java
File metadata and controls
79 lines (70 loc) · 2.52 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Score {
private List<Score> scoreList = new ArrayList<Score>();
private String name;
private String mazeName;
private int moves;
private int fails;
private int points;
public Score(String name, String maze, int move, int fail, int point) {
this.name = name;
this.mazeName = maze;
this.moves = move;
this.fails = fail;
this.points = point;
}
public void loadScore(File file) {
int lineNum = 0;
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
in.readLine();
String line = in.readLine();
final String FIELD_SEP = ",";
while (line != null) {
lineNum++;
line = line.trim();
String[] column = line.split(FIELD_SEP);
String name = column[0];
String maze = column[1];
int move = Integer.parseInt(column[2]);
int fail = Integer.parseInt(column[3]);
int point = Integer.parseInt(column[4]);
Score score = new Score(name, maze, move, fail, point);
scoreList.add(score);
line = in.readLine();
}
in.close();
} catch (IOException e) {
System.out
.println(e + " Failed to load high_scores.txt. Your score could not be written to high_scores.txt");
System.exit(1);
} catch (NumberFormatException e) {
System.out.println(e + ". Error detected at line " + lineNum
+ " of high_scores.txt. Your score could not be written.");
System.exit(1);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e + ". Error detected at line " + lineNum
+ " of high_scores.txt. Your score could not be written.");
System.exit(1);
}
}
public List<Score> getScoreList() {
return scoreList;
}
public int getPoints() {
return points;
}
/* sorts scores by descending points */
public void sortScoreList() {
Collections.sort(scoreList, (s1, s2) -> s2.getPoints() - s1.getPoints());
}
@Override
public String toString() {
return name + "," + mazeName + "," + moves + "," + fails + "," + points;
}
}