HighScore Final
This commit is contained in:
parent
c6cdebfc84
commit
ef779381a6
@ -1 +1,2 @@
|
||||
11.34
|
||||
score 99
|
||||
time 99999.99
|
||||
@ -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;
|
||||
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();
|
||||
} else {
|
||||
std::cerr << "Error: Could not write to highscore file" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "✅ Highscore updated!\n";
|
||||
}
|
||||
|
||||
|
||||
@ -8,13 +8,18 @@ class ScoreManager {
|
||||
|
||||
void incrementScore();
|
||||
void resetScore();
|
||||
void saveHighScore();
|
||||
void saveHighScore(int finalScore, double finalTime);
|
||||
|
||||
int getCurrentScore() const { return currentScore; }
|
||||
int getHighScore() const { return (highScore == std::numeric_limits<int>::max()) ? 0 : highScore; }
|
||||
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();
|
||||
};
|
||||
4
main.cpp
4
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user