- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreProcessor.java
More file actions
Latest commit
153 lines (131 loc) · 4.89 KB
/
Copy pathScoreProcessor.java
File metadata and controls
153 lines (131 loc) · 4.89 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
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
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Scanner;
importjava.io.*;
importjava.awt.Desktop;
importjava.net.*;
/**
* Deals with all methods regarding creating a storing high scores
*
* @AviRuthen @3/22/21
*/
publicclassScoreProcessor {
ArrayList<HighScores> scores = newArrayList<HighScores>();
/**
* Prints all the highscores from textfile (used for debugging)
*/
publicstaticvoidread() {
try {
// Scanner to get information from scores.txt
FileReaderreader = newFileReader("scores.txt");
Scannerscan = newScanner(reader);
// Print information to console
while (scan.hasNext())
System.out.println(scan.nextLine());
scan.close();
} catch (Exceptione) {
// Catches any exception if file isn't found, etc.
System.out.println("File missing or not located. IO exception thrown!");
}
}
/**
* Gets all the scores from the scores.txt file and Stores the scores in an
* ArrayList of HighScores objects
*
* @return an ArrayList of current high score objects
*/
publicvoidgetScores() {
// Takes scores from text file
scores.clear();
try {
FileReaderreader = newFileReader("scores.txt");
Scannerscan = newScanner(reader);
while (scan.hasNext()) {
Strings = scan.nextLine();
// Skips over first line
if (s.equals("High Scores:"))
continue;
// Separate all words in each line
String[] splits = s.split(" ");
// splits[2] is the score for each line
intscore = Integer.parseInt(splits[2]);
Stringdate = "";
// All words involving the date occurs after splits[2]
for (inti = 3; i < splits.length - 1; i++)
date += splits[i] + " ";
// Don't add space on last date
date += splits[splits.length - 1];
// Assuming order in txt file is name, score, date
// splits[1] is the name of person (splits[0] is ranking)
//scores.add(new HighScores(splits[1], score, date));
}
scan.close();
} catch (IOExceptionio) {
System.out.println("File missing or not located. IO exception thrown!");
}
}
/**
* Creates a new ArrayList of high score objects with new score added
*
* @return an ArrayList of all current high score objects with new one
*/
publicvoidadd(Stringname, intscore, Stringdate) {
// Get all the current scores
getScores();
// Add the new score
//scores.add(new HighScores(name, score, date));
}
/**
* Sorts all high score objects by score and saves them To a new text file
* (scores.txt)
*
* @param the current list of high score objects (easily obtained with the
* getScores() method in this class)
*/
publicvoidsaveScore() {
// Sort scores first by score
sortScores(scores);
// When saving, file should not be edited
// However we are changing this temporarily to add new file
Filefile = newFile("scores.txt");
file.setWritable(true);
// Create new HighScores file, prints all high scores to file
try {
PrintWriterwriter = newPrintWriter("scores.txt");
writer.println("High Scores:");
for (inti = 0; i < scores.size(); i++)
writer.println((i + 1) + ". " + scores.get(i).getInfo());
writer.close();
} catch (IOExceptionio) {
System.out.println("Error in finding file");
}
// Make file uneditable
file.setWritable(false);
}
/**
* Sorts all objects by score
*
* @param current ArrayList of all high scores
* @return sorted version of the ArrayList of high scores
*
* NOTE: Keeping this in to show work
*/
publicvoidsortScores(ArrayList<HighScores> scores) {
// I put all the scores in an array so I can use Arrays.sort
int[] playerScores = newint[scores.size()];
for (inti = 0; i < scores.size(); i++)
playerScores[i] = scores.get(i).score;
// Sort by score
Arrays.sort(playerScores);
ArrayList<HighScores> scoreTemp = newArrayList<HighScores>();
// Goes in reverse order
// If HighScores object has same score, adds it to ArrayList
for (inti = playerScores.length - 1; i >= 0; i--)
for (HighScoresh : scores)
if (h.score == playerScores[i])
scoreTemp.add(h);
scores.clear();
for (HighScoresh : scoreTemp)
scores.add(h);
}
}