-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreProcessor.java
More file actions
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.io.*;
import java.awt.Desktop;
import java.net.*;
/**
* Deals with all methods regarding creating a storing high scores
*
* @AviRuthen @3/22/21
*/
public class ScoreProcessor {
ArrayList<HighScores> scores = new ArrayList<HighScores>();
/**
* Prints all the highscores from textfile (used for debugging)
*/
public static void read() {
try {
// Scanner to get information from scores.txt
FileReader reader = new FileReader("scores.txt");
Scanner scan = new Scanner(reader);
// Print information to console
while (scan.hasNext())
System.out.println(scan.nextLine());
scan.close();
} catch (Exception e) {
// 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
*/
public void getScores() {
// Takes scores from text file
scores.clear();
try {
FileReader reader = new FileReader("scores.txt");
Scanner scan = new Scanner(reader);
while (scan.hasNext()) {
String s = 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
int score = Integer.parseInt(splits[2]);
String date = "";
// All words involving the date occurs after splits[2]
for (int i = 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 (IOException io) {
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
*/
public void add(String name, int score, String date) {
// 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)
*/
public void saveScore() {
// Sort scores first by score
sortScores(scores);
// When saving, file should not be edited
// However we are changing this temporarily to add new file
File file = new File("scores.txt");
file.setWritable(true);
// Create new HighScores file, prints all high scores to file
try {
PrintWriter writer = new PrintWriter("scores.txt");
writer.println("High Scores:");
for (int i = 0; i < scores.size(); i++)
writer.println((i + 1) + ". " + scores.get(i).getInfo());
writer.close();
} catch (IOException io) {
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
*/
public void sortScores(ArrayList<HighScores> scores) {
// I put all the scores in an array so I can use Arrays.sort
int[] playerScores = new int[scores.size()];
for (int i = 0; i < scores.size(); i++)
playerScores[i] = scores.get(i).score;
// Sort by score
Arrays.sort(playerScores);
ArrayList<HighScores> scoreTemp = new ArrayList<HighScores>();
// Goes in reverse order
// If HighScores object has same score, adds it to ArrayList
for (int i = playerScores.length - 1; i >= 0; i--)
for (HighScores h : scores)
if (h.score == playerScores[i])
scoreTemp.add(h);
scores.clear();
for (HighScores h : scoreTemp)
scores.add(h);
}
}