-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreEntry.java
More file actions
64 lines (55 loc) · 2.01 KB
/
Copy pathScoreEntry.java
File metadata and controls
64 lines (55 loc) · 2.01 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
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ScoreEntry {
private String playerName;
private static int attemptsCount;
// Lista estática para armazenar as entradas de pontuação
private static List<ScoreEntry> scoreList = new ArrayList<>();
public ScoreEntry(String playerName, int attemptsCount) {
this.playerName = playerName;
this.attemptsCount = attemptsCount;
}
// Método estático para registrar a pontuação
public static void scoreGame(String playerName, int attemptsCount) {
ScoreEntry score = new ScoreEntry(playerName, attemptsCount);
scoreList.add(score);
// Ordena a lista de pontuações
Collections.sort(scoreList, (s1, s2) -> 10 - attemptsCount);
// Mantém apenas os top 5 scores
if (scoreList.size() > 5) {
scoreList = scoreList.subList(0, 5);
}
// Escreve os dados no arquivo de texto
writeScoresToFile();
}
// Método para escrever os scores no arquivo de texto
private static void writeScoresToFile() {
String fileName = "scores.txt";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true))) { // true para append
for (ScoreEntry score : scoreList) {
writer.write(score.getPlayerName() + "," + (10 - attemptsCount));
writer.newLine();
}
} catch (IOException e) {
System.err.println("Erro ao escrever no arquivo: " + e.getMessage());
}
}
// Método para obter a lista de top 5 melhores tentativas
public static List<ScoreEntry> getTopScores() {
return scoreList;
}
public String getPlayerName() {
return playerName;
}
public int getAttempts() {
return attemptsCount;
}
@Override
public String toString() {
return "Jogador: " + playerName + ", Tentativas: " + attemptsCount;
}
}