Skip to content

Commit e0218b2

Browse files
authored
Delete score (#76)
* Wiped scores and added to git ignore * Removed score.txt * Removed score.txt * Made it so score.txt is created on first start up of game. * Used SCOREFILE final string. * Added use of the boolean for create file * Made it so the score file will be created upon one retry.
1 parent 57531f5 commit e0218b2

File tree

4 files changed

+67
-52
lines changed

4 files changed

+67
-52
lines changed

score.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/main/java/com/stackedsuccess/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public static void setRoot(String fxml) throws IOException {
5555
*/
5656
@Override
5757
public void start(final Stage stage) throws IOException {
58+
ScoreRecorder.createScoreFile();
59+
5860
SceneManager.addScene(AppUI.MAIN_MENU, loadFxml("HomeScreen"));
5961

6062
scene = new Scene(SceneManager.getScene(AppUI.MAIN_MENU), 1300, 900);

src/main/java/com/stackedsuccess/ScoreRecorder.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.BufferedReader;
44
import java.io.BufferedWriter;
5+
import java.io.File;
56
import java.io.FileReader;
67
import java.io.FileWriter;
78
import java.io.IOException;
@@ -57,7 +58,9 @@ private static List<Integer> getAllScores() throws IOException {
5758
try (BufferedReader buffread = new BufferedReader(new FileReader(SCOREFILE))) {
5859
String line;
5960
while ((line = buffread.readLine()) != null) {
60-
scores.add(Integer.parseInt(line));
61+
if (!line.trim().isEmpty()) { // Skip empty lines
62+
scores.add(Integer.parseInt(line));
63+
}
6164
}
6265
}
6366
return scores;
@@ -77,4 +80,23 @@ private static void writeScores(List<Integer> scores) throws IOException {
7780
}
7881
}
7982
}
83+
84+
/** Create a score file if it does not exist. */
85+
public static void createScoreFile() {
86+
File scoreFile = new File(SCOREFILE);
87+
if (!scoreFile.exists()) {
88+
try {
89+
boolean isFileCreated = scoreFile.createNewFile();
90+
if (!isFileCreated) {
91+
// Retry creating the file
92+
isFileCreated = scoreFile.createNewFile();
93+
if (!isFileCreated) {
94+
throw new IOException("Failed to create score file after retrying.");
95+
}
96+
}
97+
} catch (IOException e) {
98+
throw new IllegalArgumentException("Creating score file", e);
99+
}
100+
}
101+
}
80102
}

src/main/java/com/stackedsuccess/controllers/HomeScreenController.java

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
import com.stackedsuccess.Main;
44
import com.stackedsuccess.SceneManager;
55
import com.stackedsuccess.SceneManager.AppUI;
6-
import java.io.IOException;
7-
import javafx.fxml.FXML;
8-
import javafx.fxml.FXMLLoader;
9-
import javafx.scene.Parent;
10-
import javafx.scene.control.Slider;
11-
import javafx.scene.input.KeyCode;
126
import java.io.BufferedReader;
137
import java.io.FileReader;
148
import java.io.IOException;
@@ -24,36 +18,36 @@
2418
import javafx.scene.input.KeyEvent;
2519

2620
public class HomeScreenController {
27-
@FXML Slider difficultySlider;
21+
@FXML Slider difficultySlider;
2822

29-
@FXML private Button pastScoresButton;
30-
@FXML private ListView<String> pastScores;
23+
@FXML private Button pastScoresButton;
24+
@FXML private ListView<String> pastScores;
3125

32-
@FXML
33-
public void initialize() {
34-
difficultySlider.requestFocus();
35-
List<String> scores = loadScoresFromFile("score.txt");
36-
pastScores.getItems().addAll(scores);
37-
}
26+
@FXML
27+
public void initialize() {
28+
difficultySlider.requestFocus();
29+
List<String> scores = loadScoresFromFile("score.txt");
30+
pastScores.getItems().addAll(scores);
31+
}
3832

39-
/**
40-
* Reads scores from a file and returns them as a list of strings.
41-
*
42-
* @param filePath the path to the score file
43-
* @return list of scores as strings
44-
*/
45-
private List<String> loadScoresFromFile(String filePath) {
46-
List<String> scores = new ArrayList<>();
47-
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
48-
String line;
49-
while ((line = reader.readLine()) != null) {
50-
scores.add(line);
51-
}
52-
} catch (IOException e) {
53-
throw new IllegalArgumentException("Issue regarding Score file", e);
54-
}
55-
return scores;
33+
/**
34+
* Reads scores from a file and returns them as a list of strings.
35+
*
36+
* @param filePath the path to the score file
37+
* @return list of scores as strings
38+
*/
39+
private List<String> loadScoresFromFile(String filePath) {
40+
List<String> scores = new ArrayList<>();
41+
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
42+
String line;
43+
while ((line = reader.readLine()) != null) {
44+
scores.add(line);
45+
}
46+
} catch (IOException e) {
47+
throw new IllegalArgumentException("Issue regarding Score file", e);
5648
}
49+
return scores;
50+
}
5751

5852
public void exitGame() {
5953
System.exit(0);
@@ -70,23 +64,23 @@ public void startGame() throws IOException {
7064
Main.setUi(AppUI.GAME);
7165
}
7266

73-
public void onKeyPressed(KeyEvent event) {
74-
difficultySlider.requestFocus();
75-
if (event.getCode() == KeyCode.SPACE) {
76-
try {
77-
startGame();
78-
} catch (IOException e) {
79-
// Do nothing for now
80-
}
81-
}
67+
public void onKeyPressed(KeyEvent event) {
68+
difficultySlider.requestFocus();
69+
if (event.getCode() == KeyCode.SPACE) {
70+
try {
71+
startGame();
72+
} catch (IOException e) {
73+
// Do nothing for now
74+
}
8275
}
76+
}
8377

84-
@FXML
85-
public void showPastScores() {
86-
if (pastScores.isVisible()) {
87-
pastScores.setVisible(false);
88-
} else {
89-
pastScores.setVisible(true);
90-
}
78+
@FXML
79+
public void showPastScores() {
80+
if (pastScores.isVisible()) {
81+
pastScores.setVisible(false);
82+
} else {
83+
pastScores.setVisible(true);
9184
}
85+
}
9286
}

0 commit comments

Comments
 (0)