Repository navigation
Expand file tree
/
Copy pathUserData.java
More file actions
88 lines (69 loc) · 3.01 KB
/
Copy pathUserData.java
File metadata and controls
88 lines (69 loc) · 3.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
public class UserData {
static String filePath = "UserData.csv";
static int highScore = 0;
static int getHighScore() {
int highScoreFromFile = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
reader.readLine(); // Skip the header line
// Read each line in the file
while ((line = reader.readLine()) != null) {
String[] row = line.split(","); // Split by comma
// obtain high score present in column-1
highScoreFromFile = Math.max(highScoreFromFile, Integer.parseInt(row[1].trim()));
}
} catch (IOException e) {
System.err.println("Error reading the file.");
}
highScore = highScoreFromFile;
return highScoreFromFile;
}
// Update high score column
static void updateScore(GameUtil g, int score) {
UserData.highScore = Math.max(highScore, score);
// write highScore & score to file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) { // 'true' appends to file
// prepare new data to be appended to csv file
String outcome = g.victory ? "Win" : "Loss";
String difficulty = g.calculateDifficulty();
String[] data = {difficulty, String.valueOf(highScore), String.valueOf(score), outcome};
String line = String.join(",", data);
// write this data and move to next line
writer.write(line);
writer.newLine();
} catch (IOException e) {
System.out.println("Error reading the file.");
e.printStackTrace();
}
}
static void updateHighScoreButton(GameUtil g) {
JButton hsButton = (JButton) g.buttonPanel.getComponent(3);
hsButton.setText(String.valueOf(highScore));
}
static void resetScore() {
UserData.highScore = 0;
String firstRow = "";
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
// Read the first line
firstRow = reader.readLine();
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
return;
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
// Write the first row back to the file
if (firstRow != null) {
writer.write(firstRow);
writer.newLine(); // Add a newline if you want to maintain the format
}
} catch (IOException e) {
System.err.println("Error writing to the file: " + e.getMessage());
}
}
}