Repository navigation
Expand file tree
/
Copy pathHighScoreTableManager.java
More file actions
191 lines (162 loc) · 7.15 KB
/
Copy pathHighScoreTableManager.java
File metadata and controls
191 lines (162 loc) · 7.15 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Manages the high scores for the game. Allows new entries to be added
* and saved to a level's high score table.
* @author Alex Vesely
* @author Tahi Rahman
*/
public class HighScoreTableManager {
private static final int MAX_HIGH_SCORES = 10;
private static final int FINAL_LEVEL = 3;
private static final int HIGH_SCORE_TABLE_WIDTH = 400;
private static final int HIGH_SCORE_TABLE_HEIGHT = 500;
private static final int SPACING = 10;
/**
* Displays a level's high score table when accessed from the main menu.
* @param level The level's high score table.
* @param parentDialog The dialog that will be shown when this is closed.
*/
public static void displayHighScoresInMainMenu(int level, Stage parentDialog) {
Stage dialog = new Stage();
dialog.setTitle("High Score Table for Level " + level);
dialog.initModality(Modality.APPLICATION_MODAL);
VBox vbox = new VBox(SPACING);
vbox.setStyle("-fx-padding: 20; -fx-alignment: center;");
ArrayList<HighScore> highScores = HighScoreTableManager.getHighScores(level);
for (HighScore highScore : highScores) {
Label scoreLabel = new Label(highScore.getName() + " : " + highScore.getScore());
scoreLabel.setStyle("-fx-font-size: 18px; -fx-text-alignment: center;");
vbox.getChildren().add(scoreLabel);
}
Button backButton = new Button("Back to High Scores");
backButton.setOnAction(event -> {
dialog.close();
parentDialog.show();
});
vbox.getChildren().add(backButton);
Scene dialogScene = new Scene(vbox, HIGH_SCORE_TABLE_WIDTH, HIGH_SCORE_TABLE_HEIGHT);
dialog.setScene(dialogScene);
dialog.showAndWait();
}
/**
* Displays the high score table for a specific level after the player beats a level.
* @param level The level for which to display the high scores.
* @param scoreAchieved The score the player achieved in the level.
*/
public static void displayHighScoresAfterLevel(int level, int scoreAchieved) {
Stage dialog = new Stage();
dialog.setTitle("High Score Table for Level " + level);
dialog.initModality(Modality.APPLICATION_MODAL);
VBox vbox = new VBox(SPACING);
vbox.setStyle("-fx-padding: 20; -fx-alignment: center;");
ArrayList<HighScore> highScores = HighScoreTableManager.getHighScores(level);
for (HighScore highScore : highScores) {
Label scoreLabel = new Label(highScore.getName() + " : " + highScore.getScore());
scoreLabel.setStyle("-fx-font-size: 18px; -fx-text-alignment: center;");
vbox.getChildren().add(scoreLabel);
}
Label addSpace = new Label();
addSpace.setMinHeight(SPACING);
vbox.getChildren().add(addSpace);
Label recentScore = new Label("Your Score: " + scoreAchieved);
recentScore.setStyle("-fx-font-size: 18px; -fx-text-alignment: center;");
vbox.getChildren().add(recentScore);
String buttonText;
if (level == FINAL_LEVEL) {
buttonText = "Close";
} else {
buttonText = "Begin Next Level";
}
Button backButton = new Button(buttonText);
backButton.setOnAction(event -> {
dialog.close();
});
vbox.getChildren().add(backButton);
Scene dialogScene = new Scene(vbox, HIGH_SCORE_TABLE_WIDTH, HIGH_SCORE_TABLE_HEIGHT);
dialog.setScene(dialogScene);
dialog.showAndWait();
}
/**
* Updates the high score table of a specific level with the score a player has achieved.
* Updates so that only the 10 highest scores are maintained
* @param playerName The name of the player achieving the score.
* @param score The score achieved by the player.
* @param level The level for which the high score table should be updated.
*/
public static void updateHighScoreTable(String playerName, int score, int level) {
HighScore newHighScore = new HighScore(playerName, score);
ArrayList<HighScore> highScores = getHighScores(level);
highScores.add(newHighScore);
highScores.sort((h1, h2) -> Integer.compare(h2.getScore(), h1.getScore()));
if (highScores.size() > MAX_HIGH_SCORES) {
highScores.remove(MAX_HIGH_SCORES);
}
saveHighScoreTable(highScores, level);
}
/**
* Retrieves the high scores for a specific level.
* @param level The level for which to retrieve the high scores.
* @return A list of high scores for the specified level.
*/
public static ArrayList<HighScore> getHighScores(int level) {
String folderPath = "txt";
ArrayList<HighScore> highScores = new ArrayList<>();
File folder = new File(folderPath);
// If the folder is invalid, return empty list
if (!(folder.exists() && folder.isDirectory())) {
System.out.println("Folder does not exist or is not a directory: " + folderPath);
return highScores;
}
File[] files = folder.listFiles((dir, name) ->
name.matches("HighScoreTable" + level + ".txt"));
if (files == null || files.length == 0) {
System.out.println("No matching file found for level " + level);
return highScores;
}
File matchingFile = files[0];
try (Scanner scanner = new Scanner(matchingFile)) {
while (scanner.hasNextLine()) {
String[] splitHighScore = scanner.nextLine().split(" ");
String name = splitHighScore[0];
int score = Integer.parseInt(splitHighScore[1]);
HighScore highScore = new HighScore(name, score);
highScores.add(highScore);
}
} catch (IOException e) {
System.out.println("Error reading file: " + matchingFile.getName());
}
return highScores;
}
/**
* Saves a level's high score table to its high score table file.
* @param highScores The list of high scores to save.
* @param level The level for which the High Score Table is of
*/
public static void saveHighScoreTable(ArrayList<HighScore> highScores, int level) {
try {
String outputFile = "txt/HighScoreTable" + level + ".txt";
PrintWriter out = new PrintWriter(outputFile);
for (int i = 0; i < highScores.size(); i++) {
HighScore highScore = highScores.get(i);
out.print(highScore.getName() + " " + highScore.getScore());
if (i < highScores.size() - 1) {
out.println();
}
}
out.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}