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 "ScoreManager.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <limits>
|
|
||||||
|
|
||||||
ScoreManager::ScoreManager(const std::string& filename) : highscoreFile(filename)
|
ScoreManager::ScoreManager(const std::string& filename) : highscoreFile(filename) {
|
||||||
{
|
|
||||||
std::cout << "test";
|
|
||||||
loadHighscore();
|
loadHighscore();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScoreManager::loadHighscore() {
|
void ScoreManager::loadHighscore() {
|
||||||
std::ifstream file(highscoreFile);
|
std::ifstream file(highscoreFile);
|
||||||
if (file.is_open()) {
|
|
||||||
int value;
|
if (!file.is_open()) {
|
||||||
if (file >> value) {
|
std::cout << "No highscore file found, creating new file.\n";
|
||||||
if (value > 0) highScore = value;
|
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();
|
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() {
|
void ScoreManager::incrementScore() {
|
||||||
currentScore++;
|
currentScore++;
|
||||||
}
|
}
|
||||||
@ -30,14 +38,35 @@ void ScoreManager::resetScore() {
|
|||||||
currentScore = 0;
|
currentScore = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScoreManager::saveHighScore() {
|
void ScoreManager::saveHighScore(int finalScore, double finalTime) {
|
||||||
if (highScore == std::numeric_limits<int>::max()) return;
|
bool updated = false;
|
||||||
std::ofstream file(highscoreFile);
|
|
||||||
|
|
||||||
if (file.is_open()) {
|
if (finalScore < highScore) {
|
||||||
file << highScore;
|
highScore = finalScore;
|
||||||
file.close();
|
updated = true;
|
||||||
} else {
|
|
||||||
std::cerr << "Error: Could not write to highscore file" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,18 +3,23 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
class ScoreManager {
|
class ScoreManager {
|
||||||
public:
|
public:
|
||||||
ScoreManager(const std::string& filename);
|
ScoreManager(const std::string& filename);
|
||||||
|
|
||||||
void incrementScore();
|
void incrementScore();
|
||||||
void resetScore();
|
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; }
|
|
||||||
|
|
||||||
private:
|
int getCurrentScore() const { return currentScore; }
|
||||||
|
int getHighScore() const { return highScore; }
|
||||||
|
double getBestTime() const { return bestTime; }
|
||||||
|
|
||||||
|
private:
|
||||||
int currentScore = 0;
|
int currentScore = 0;
|
||||||
int highScore = std::numeric_limits<int>::max();
|
int highScore = std::numeric_limits<int>::max();
|
||||||
|
double bestTime = std::numeric_limits<double>::max();
|
||||||
|
|
||||||
std::string highscoreFile;
|
std::string highscoreFile;
|
||||||
|
|
||||||
void loadHighscore();
|
void loadHighscore();
|
||||||
};
|
};
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@ -34,7 +34,7 @@ int main()
|
|||||||
//TODO: Hier die Zeit abrufen.
|
//TODO: Hier die Zeit abrufen.
|
||||||
double currentScoreTime = 0.0;
|
double currentScoreTime = 0.0;
|
||||||
double startTime = 0.0;
|
double startTime = 0.0;
|
||||||
double highScoreTime = 43.4;
|
double highScoreTime = score.getBestTime();
|
||||||
|
|
||||||
//TODO: Hier die Anzahl der Züge abrufen.
|
//TODO: Hier die Anzahl der Züge abrufen.
|
||||||
int currentScoreTurns = 0;
|
int currentScoreTurns = 0;
|
||||||
@ -177,7 +177,7 @@ int main()
|
|||||||
|
|
||||||
if (currentScoreTime <= highScoreTime) highScoreTime = currentScoreTime;
|
if (currentScoreTime <= highScoreTime) highScoreTime = currentScoreTime;
|
||||||
if (currentScoreTurns <= highScoreTurns) highScoreTurns = currentScoreTurns;
|
if (currentScoreTurns <= highScoreTurns) highScoreTurns = currentScoreTurns;
|
||||||
score.saveHighScore();
|
score.saveHighScore(currentScoreTurns, currentScoreTime);
|
||||||
userInterface.showScore(currentScoreTime, highScoreTime, currentScoreTurns, highScoreTurns);
|
userInterface.showScore(currentScoreTime, highScoreTime, currentScoreTurns, highScoreTurns);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user