Implementing Highscore for an App
I did an simulation of a game, a puzzle game.
Now I'm trying to implement a HighScore feature for the app.
When I open the App, the player will need to enter a name in order to continue. str variable.
When the player will complet a game he will need to hit a button CHECK in order to validate the game, if the game is valid a mesage will display his current Score(I have different puzzle sizes with different scores). This is saved into highscore and is updated everytime a new game is validated.
Now I want to add a menu, that when is clicked it will display the first 5 HighScores.
When the Highscore menu is selected I want to read a file and check if the players name is allready in the file(check if str is found): if it is not found add str+highscore to the file, if it is found, check if highscore from file is bigger than actual score and update highscore of that player if true, otherwise do nothing. Finaly after writing and updating(if necesary) to the file, read the file and display the name and scores for the best 5 players.
here is what I did till now :
This is in the menu listener, at the HighScore menu.
Code:
try{// Create file
String file = ((System.getenv("USERPROFILE"))+("\\My Documents\\"));
FileWriter fwrite = new FileWriter(file+"out.txt", true);
BufferedWriter writer = new BufferedWriter(fwrite);
FileReader fread = new FileReader(file+"out.txt");
BufferedReader reader = new BufferedReader(fread);
writer.write(ValidateGameListener.highscore+" "+MainUI.str);
writer.newLine();
//Close the output stream
writer.close();
while((file = reader.readLine()) != null) {
System.out.println(file);
JOptionPane.showMessageDialog(gameController.view, file);
}
}
catch (Exception error){//Catch exception if any
System.err.println("Error: " + error.getMessage());
}
The problem that I can't solve is that I can't manage to update score for specific player, everytime a new player with the curent score is inserted into file.
And It's displaying the file line by line(One line appears in the windows, I press ok, next line is shown, till end of file). I want to display them as a list in only one dialog window.
Also how to separate the name from score in order to check which score is bigger ?
Can someone help me with this and give me some ideas ?