If you want to record the players scores, you have to save it in a file (highscores.dat per example). In my way to see things, you can't record inside a .jar file because de system is using it.
And for the records, you can create a class that encaspulate the highscore of one game, and then you create an array that contains the totality of highscores.
class HighScoreRecord implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private final String name;
private final int highScore;
HighScoreRecord( String name, int highScore ){
this.name = name;
this.highScore = highScore;
}
}
Dont forget to implement java.io.Serializable, otherwise you won't be able to save it. And don't forget to create a private static field of long type "serialVersionUID" because the JVM can atribute its own serial if there is none and it can cause incompatibility with files already recorded.
HighScoreRecord[] highscores = new HighScoreRecord[]{
new HighScoreRecord( "Player1", 1000 ),
new HighScoreRecord( "Player2", 800 ),
new HighScoreRecord( "Player3", 600 ),
new HighScoreRecord( "Player4", 400 ),
new HighScoreRecord( "Player5", 200 )
};
To save the records:
private void writeHighScores() {
FileOutputStream fileOutput = null;
ObjectOutputStream save = null;
try {
//HIGH_SCORES_FILE is any object of type File
fileOutput = new FileOutputStream( HIGH_SCORES_FILE );
save = new ObjectOutputStream( fileOutput );
save.writeObject( highscores );
}
catch ( FileNotFoundException ex ) {
//Exception handling
}
catch ( IOException ex ) {
//Exception handling
}
finally {
if( save != null )
try{
save.close();
} catch ( IOException ex ) {
//Exception handling
}
if( fileOutput != null )
try{
fileOutput.close();
} catch ( IOException ex ) {
//Exception handling
}
}
}
EDIT:
To load:
HighScoreRecord[] high = null;
FileInputStream fileInput = null;
ObjectInputStream load = null;
try {
fileInput = new FileInputStream( HIGH_SCORES_FILE );
load = new ObjectInputStream( fileInput );
high = (HighScoreRecord[]) load.readObject();
}
catch ( FileNotFoundException ex ) {
//Exception handling
}
catch ( IOException | ClassNotFoundException ex ) {
//Exception handling
}
finally {
if( load != null )
try{
load.close();
} catch ( IOException ex ) {
//Exception handling
}
if( fileInput != null )
try{
fileInput.close();
} catch ( IOException ex ) {
//Exception handling
}
}
highscores = high;
I hope I have helped.