Skip to content
Snippets Groups Projects
Commit 7f53a1b8 authored by abhatta5's avatar abhatta5
Browse files

Merge branch 'ScoringSystem' of https://gitlab.une.edu.au/mkandel2/brick-breaker

Merge Joshua's high score code
parents ebe337c5 08ee8611
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 = 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
public void brickBroken() {
score += POINTS;
checkForNewHighScore();
}
// Compare Current Score and Hgihest Score
private void checkForNewHighScore() {
if (score > highScore) {
highScore = score;
saveHighScore(highScore);
}
}
// Reset score
public void resetScore() {
score = 0;
}
// Getters
public int getScore() {
return score;
}
public int getHighScore() {
return highScore;
}
}
0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment