Skip to content

Commit

Permalink
Added Highscore System
Browse files Browse the repository at this point in the history
  • Loading branch information
jsc2001 committed Aug 18, 2021
1 parent b9b87dc commit 128744e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
33 changes: 17 additions & 16 deletions Controller/src/HighScore/HighScoreController.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
package HighScore;

import java.io.*;
import java.security.Timestamp;
import java.util.Date;
import java.util.HashMap;

public class HighScoreController implements IHighScoreController
{
private HighScoreData currentHighScoreData;
private HashMap<Integer,HighScoreData> currentHighScoreData;

public HighScoreData getCurrentHighScoreData()
public HighScoreData getCurrentHighScoreData(int dimension)
{
return currentHighScoreData;
if(!currentHighScoreData.containsKey(dimension)) return new HighScoreData(new Date(),0);
return currentHighScoreData.get(dimension);
}

private final String highscoreDataLocation = "./highscore.txt";
private final String highscoreDataLocation = "./highscore.dat";

public HighScoreController()
{
try
{
this.currentHighScoreData = readHighScoreData();
readHighScoreDataFromFile();
} catch (IOException | ClassNotFoundException e)
{
this.currentHighScoreData = new HighScoreData(new Date(),0);
this.currentHighScoreData = new HashMap<>();
}
}

private HighScoreData readHighScoreData() throws IOException, ClassNotFoundException
private void readHighScoreDataFromFile() throws IOException, ClassNotFoundException
{
File f = new File(highscoreDataLocation);
if(!f.exists()) throw new FileNotFoundException();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(highscoreDataLocation));
return (HighScoreData) in.readObject();

currentHighScoreData = (HashMap<Integer,HighScoreData>) in.readObject();
}

private void writeHighScoreData(HighScoreData highScoreData) throws IOException
private void writeHighScoreDataToFile() throws IOException
{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(highscoreDataLocation));
os.writeObject(highScoreData);
os.writeObject(currentHighScoreData);
}

@Override
public void submitNewScore(int newScore)
public void submitNewScore(int newScore, int dimension)
{
if(currentHighScoreData.getScore()<newScore)
if(!currentHighScoreData.containsKey(dimension) || currentHighScoreData.get(dimension).getScore()<newScore)
{
currentHighScoreData = new HighScoreData(new Date(), newScore);
currentHighScoreData.put(dimension,new HighScoreData(new Date(), newScore));
try
{
writeHighScoreData(currentHighScoreData);
writeHighScoreDataToFile();
} catch (IOException e)
{
e.printStackTrace();
Expand Down
13 changes: 11 additions & 2 deletions Controller/src/HighScore/IHighScoreController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

public interface IHighScoreController
{
void submitNewScore(int newScore);
HighScoreData getCurrentHighScoreData();
/**
* @param newScore Neue Punktzahl (wird intern geprüft ob es Highscore wird)
* @param dimension Ausgewählte Dimension z.B. 4x4
*/
void submitNewScore(int newScore, int dimension);

/**
* @param dimension Ausgewählte Dimension z.B. 4x4
* @return HighScore-Daten
*/
HighScoreData getCurrentHighScoreData(int dimension);
}


5 changes: 3 additions & 2 deletions UI/src/View/Game/GameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public void createGameScene(Event event, Scene scene) throws IOException {

this.gameController.setScoreChangeListener(newScore -> {
setScoreLabel(newScore);
highScoreController.submitNewScore(newScore);
highScoreController.submitNewScore(newScore,tileCount);
setHighscore(highScoreController.getCurrentHighScoreData(tileCount).getScore());
});


Expand Down Expand Up @@ -247,7 +248,7 @@ public void createGameScene(Event event, Scene scene) throws IOException {
}
});

setHighscore(highScoreController.getCurrentHighScoreData().getScore());
setHighscore(highScoreController.getCurrentHighScoreData(tileCount).getScore());
}

/**
Expand Down

0 comments on commit 128744e

Please sign in to comment.