From ef779381a60f73a567035119b49cce7f13d2cef7 Mon Sep 17 00:00:00 2001 From: pumcookie Date: Mon, 1 Dec 2025 14:37:35 +0100 Subject: [PATCH] HighScore Final --- Highscores.txt | 3 +- ScoreManager.cpp | 73 +++++++++++++++++++++++++++++++++--------------- ScoreManager.h | 31 +++++++++++--------- main.cpp | 4 +-- 4 files changed, 73 insertions(+), 38 deletions(-) diff --git a/Highscores.txt b/Highscores.txt index c8467e2..f2587d5 100644 --- a/Highscores.txt +++ b/Highscores.txt @@ -1 +1,2 @@ -11.34 \ No newline at end of file +score 99 +time 99999.99 \ No newline at end of file diff --git a/ScoreManager.cpp b/ScoreManager.cpp index 6cfc66c..6f0b9d6 100644 --- a/ScoreManager.cpp +++ b/ScoreManager.cpp @@ -1,27 +1,35 @@ #include "ScoreManager.h" #include #include -#include -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; - } - file.close(); - } else { - ; - } + + 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(); + + // 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::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"; } + diff --git a/ScoreManager.h b/ScoreManager.h index 45aaec6..3b8316f 100644 --- a/ScoreManager.h +++ b/ScoreManager.h @@ -3,18 +3,23 @@ #include class ScoreManager { - public: - ScoreManager(const std::string& filename); +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::max()) ? 0 : highScore; } + void incrementScore(); + void resetScore(); + void saveHighScore(int finalScore, double finalTime); - private: - int currentScore = 0; - int highScore = std::numeric_limits::max(); - std::string highscoreFile; - void loadHighscore(); - }; \ No newline at end of file + int getCurrentScore() const { return currentScore; } + int getHighScore() const { return highScore; } + double getBestTime() const { return bestTime; } + +private: + int currentScore = 0; + int highScore = std::numeric_limits::max(); + double bestTime = std::numeric_limits::max(); + + std::string highscoreFile; + + void loadHighscore(); +}; diff --git a/main.cpp b/main.cpp index 6a8db51..2ffe49e 100644 --- a/main.cpp +++ b/main.cpp @@ -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