HighScore Final

This commit is contained in:
pumcookie 2025-12-01 14:37:35 +01:00
parent c6cdebfc84
commit ef779381a6
4 changed files with 73 additions and 38 deletions

View File

@ -1 +1,2 @@
11.34
score 99
time 99999.99

View File

@ -1,27 +1,35 @@
#include "ScoreManager.h"
#include <fstream>
#include <iostream>
#include <limits>
ScoreManager::ScoreManager(const std::string& filename) : highscoreFile(filename)
{
std::cout << "test";
ScoreManager::ScoreManager(const std::string& filename) : highscoreFile(filename) {
loadHighscore();
}
void ScoreManager::loadHighscore() {
std::ifstream file(highscoreFile);
if (file.is_open()) {
int value;
if (file >> value) {
if (value > 0) highScore = value;
if (!file.is_open()) {
std::cout << "No highscore file found, creating new file.\n";
highScore = 9999;
bestTime = 99999.99;
return;
}
std::string key;
while (file >> key) {
if (key == "score") file >> highScore;
else if (key == "time") file >> bestTime;
}
file.close();
} else {
;
}
// If file contains useless values (like old format), reset
if (highScore <= 0 || highScore > 9999) highScore = 9999;
if (bestTime <= 0 || bestTime > 99999.99) bestTime = 99999.99;
}
void ScoreManager::incrementScore() {
currentScore++;
}
@ -30,14 +38,35 @@ void ScoreManager::resetScore() {
currentScore = 0;
}
void ScoreManager::saveHighScore() {
if (highScore == std::numeric_limits<int>::max()) return;
std::ofstream file(highscoreFile);
void ScoreManager::saveHighScore(int finalScore, double finalTime) {
bool updated = false;
if (file.is_open()) {
file << highScore;
file.close();
} else {
std::cerr << "Error: Could not write to highscore file" << std::endl;
if (finalScore < highScore) {
highScore = finalScore;
updated = true;
}
if (finalTime < bestTime) {
bestTime = finalTime;
updated = true;
}
// For safety: If file was default → always write
if (highScore == 9999 && bestTime == 99999.99)
updated = true;
if (!updated) return;
std::ofstream file(highscoreFile);
if (!file.is_open()) {
std::cerr << "❌ ERROR: Can't write highscore file\n";
return;
}
file << "score " << highScore << "\n";
file << "time " << bestTime << "\n";
file.close();
std::cout << "✅ Highscore updated!\n";
}

View File

@ -3,18 +3,23 @@
#include <limits>
class ScoreManager {
public:
public:
ScoreManager(const std::string& filename);
void incrementScore();
void resetScore();
void saveHighScore();
int getCurrentScore() const { return currentScore; }
int getHighScore() const { return (highScore == std::numeric_limits<int>::max()) ? 0 : highScore; }
void saveHighScore(int finalScore, double finalTime);
private:
int getCurrentScore() const { return currentScore; }
int getHighScore() const { return highScore; }
double getBestTime() const { return bestTime; }
private:
int currentScore = 0;
int highScore = std::numeric_limits<int>::max();
double bestTime = std::numeric_limits<double>::max();
std::string highscoreFile;
void loadHighscore();
};
};

View File

@ -34,7 +34,7 @@ int main()
//TODO: Hier die Zeit abrufen.
double currentScoreTime = 0.0;
double startTime = 0.0;
double highScoreTime = 43.4;
double highScoreTime = score.getBestTime();
//TODO: Hier die Anzahl der Züge abrufen.
int currentScoreTurns = 0;
@ -177,7 +177,7 @@ int main()
if (currentScoreTime <= highScoreTime) highScoreTime = currentScoreTime;
if (currentScoreTurns <= highScoreTurns) highScoreTurns = currentScoreTurns;
score.saveHighScore();
score.saveHighScore(currentScoreTurns, currentScoreTime);
userInterface.showScore(currentScoreTime, highScoreTime, currentScoreTurns, highScoreTurns);
}
else