-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveScore.java
More file actions
51 lines (40 loc) · 1.61 KB
/
SaveScore.java
File metadata and controls
51 lines (40 loc) · 1.61 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
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SaveScore {
private int attempt_no = 1;
private final File file = new File("d2048.txt");
private final File attempt = new File("Attempt.txt");
public void saveScore(int size, int score) {
try {
if (!attempt.exists()) attempt.createNewFile();
if (!file.exists()) file.createNewFile();
Path att = Paths.get("Attempt.txt");
if (attempt.exists()) {
String attemptNumber = Files.readString(att).trim();
attempt_no = attemptNumber.isEmpty() ? 1 : Integer.parseInt(attemptNumber) + 1;
Files.write(att, String.valueOf(attempt_no).getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
Path path = file.toPath();
String entry = "Attempt " + attempt_no + " | Size: " + size + " | Score: " + score + "\n";
Files.write(path, entry.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException ignored) {
}
}
public List<String> loadScores() {
List<String> scores = new ArrayList<>();
try {
scores = Files.readAllLines(Paths.get("d2048.txt"));
Collections.reverse(scores); // Show latest scores first
} catch (IOException ignored) {
}
return scores;
}
}