Skip to content
Snippets Groups Projects
Commit b3b30e37 authored by Joshua Alejandro's avatar Joshua Alejandro
Browse files

ScoringSystem will now load from highscore text file

parent 3779a59a
No related branches found
No related tags found
No related merge requests found
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ScoringSystem {
private int score; // Current Score
private int highScore; // Highest Score
private static final int POINTS = 1; // Point Value
private static final String HIGHSCORE_FILE = "src/main/resources/highscore.txt";
public ScoringSystem() {
this.score = 0;
this.highScore = 0;
this.highScore = loadHighScore();
}
private int loadHighScore() {
try (BufferedReader reader = new BufferedReader(new FileReader(HIGHSCORE_FILE))) {
String line = reader.readLine();
if (line != null) {
return Integer.parseInt(line.trim());
}
} catch (IOException e) {
System.err.println("Loading Error: " + e.getMessage());
}
return 0;
}
private void saveHighScore(int newHighScore) {
try (FileWriter writer = new FileWriter(HIGHSCORE_FILE)) {
writer.write(Integer.toString(newHighScore));
} catch (IOException e) {
System.err.println("Saving Error: " + e.getMessage());
}
}
// Add point to each broken brick
......@@ -14,10 +42,11 @@ public class ScoringSystem {
checkForNewHighScore();
}
// Compare Current Score and highest score
// Compare Current Score and Hgihest Score
private void checkForNewHighScore() {
if (score > highScore) {
highScore = score;
saveHighScore(highScore);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment