HitoriMitMaven/src/main/java/domain/HitoriGameMain.java

139 lines
4.1 KiB
Java

package domain;
import java.io.*;
public class HitoriGameMain extends GameSolver {
private static final long serialVersionUID = 1L;
private final HitoriGameTimer timer; // Timer für das Spiel
private final HitoriGameScores scores; // Highscore-Verwaltung
private long savedTime; // Gespeicherte Zeit für den Timer
public HitoriGameMain(int[][] initialBoard) {
super(initialBoard);
this.timer = new HitoriGameTimer();
this.scores = new HitoriGameScores();
this.savedTime = 0;
}
public HitoriGameTimer getTimer() {
return timer;
}
public HitoriGameScores getScores() {
return scores;
}
// Speichert den aktuellen Zustand des Spiels
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeLong(getElapsedTimeInSeconds());
out.writeInt(mistakeCount);
out.writeObject(blackCells);
out.writeObject(whiteCells);
}
// Stellt den gespeicherten Zustand des Spiels wieder her
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
long savedTime = in.readLong();
this.mistakeCount = in.readInt();
this.blackCells = (boolean[][]) in.readObject();
this.whiteCells = (boolean[][]) in.readObject();
this.timer.setElapsedTime(savedTime);
}
// Steuerung des Timers
public void startTimer() {
timer.startTimer();
}
public void pauseTimer() {
timer.pauseTimer();
}
public void stopTimer() {
timer.stopTimer();
}
public void resetTimer() {
timer.resetTimer();
}
// Gibt die verstrichene Zeit zurück
public long getElapsedTimeInSeconds() {
return savedTime > 0 ? savedTime : timer.getElapsedTimeInSeconds();
}
// Highscore-Verwaltung
public void addHighScore(String playerName, long timeInSeconds) {
scores.addHighScore(playerName, timeInSeconds, getMistakeCount());
}
public void deleteHighScores() {
scores.deleteHighScores();
}
public java.util.List<String> getHighScoresWithAverage() {
return scores.getHighScoresWithAverage();
}
public void loadHighScoresFromFile() {
scores.loadHighScoresFromFile();
}
public void saveHighScoresToFile() {
scores.saveHighScoresToFile();
}
// Setzt den Spielzustand
public void setGameState(boolean[][] blackCells, boolean[][] whiteCells, int mistakeCount, long elapsedTime) {
this.blackCells = new boolean[blackCells.length][blackCells[0].length];
this.whiteCells = new boolean[whiteCells.length][whiteCells[0].length];
for (int i = 0; i < blackCells.length; i++) {
this.blackCells[i] = blackCells[i].clone();
this.whiteCells[i] = whiteCells[i].clone();
}
this.mistakeCount = mistakeCount;
this.savedTime = elapsedTime;
}
// Speichert das Spiel in einer Datei
public void saveGameState() {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("gamestate.dat"))) {
oos.writeObject(this);
} catch (IOException e) {
e.printStackTrace();
}
}
// Lädt das Spiel aus einer Datei
public static HitoriGameMain loadGameState() {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("gamestate.dat"))) {
HitoriGameMain loadedGame = (HitoriGameMain) ois.readObject();
loadedGame.timer.resetTimer();
return loadedGame;
} catch (IOException | ClassNotFoundException e) {
return null;
}
}
// Stellt den gespeicherten Zustand des Spiels wieder her
public void restoreGameState(HitoriGameMain savedGame) {
if (savedGame != null) {
this.setGameState(savedGame.blackCells, savedGame.whiteCells,
savedGame.mistakeCount, savedGame.savedTime);
}
}
@Override
public void reset() {
super.reset();
savedTime = 0; // Timer wird nicht zurückgesetzt
}
}