|
From: Markus R. <god...@us...> - 2009-07-29 15:35:12
|
Module: performous
Branch: master
Commit: e7b84f6167e152d3ab84a98b17f89f26ed5d79f5
Author: Markus Raab <un...@ma...>
Date: Thu Jul 23 16:33:20 2009 +0200
highscore is now a int number between 0 and 10000
---
game/highscore.cc | 19 +++++++++++--------
game/highscore.hh | 11 +++++++----
2 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/game/highscore.cc b/game/highscore.cc
index 46bdc87..ef74176 100644
--- a/game/highscore.cc
+++ b/game/highscore.cc
@@ -74,15 +74,16 @@ void HighScore::load()
std::string sscore = str.substr(8, str.length()-8);
if (sscore.empty()) throw HighScoreException("Did not find score", linenum);
+ int score = 0;
try {
- double score = boost::lexical_cast<int>(sscore) / 10000.0;
- if (score < 0.0 || score > 1.0) throw HighScoreException("Number not between 0 and 10000", linenum);
- if (playernum>0 && m_scores[playernum-1].score < score) throw HighScoreException("Lower ranked highscore is higher", linenum);
- m_scores[playernum].score = score;
+ score = boost::lexical_cast<int>(sscore);
} catch (boost::bad_lexical_cast const& blc)
{
- throw HighScoreException("Did not find valid double number", linenum);
+ throw HighScoreException("Did not find valid int number", linenum);
}
+ if (score < 0 || score > 10000) throw HighScoreException("Number not between 0 and 10000", linenum);
+ if (playernum>0 && m_scores[playernum-1].score < score) throw HighScoreException("Lower ranked highscore is higher", linenum);
+ m_scores[playernum].score = score;
playernum++;
@@ -98,11 +99,13 @@ void HighScore::save()
out.exceptions ( std::ofstream::eofbit | std::ofstream::failbit | std::ofstream::badbit );
for (size_t i=0; i<m_scores.size();i++)
{
+ if (m_scores[i].score <= 0) break; // maybe change to 500?
+
try {
out << "#PLAYER" << i << ":"
<< m_scores[i].name << std::endl;
out << "#SCORE" << i << ":"
- << m_scores[i].score*10000 << std::endl;
+ << m_scores[i].score << std::endl;
} catch (std::ofstream::failure const&) {
throw HighScoreException("Unexpected I/O error", i);
}
@@ -110,7 +113,7 @@ void HighScore::save()
out << "E" << std::endl;
}
-void HighScore::addNewHighscore(std::string name, double score)
+void HighScore::addNewHighscore(std::string name, int score)
{
HighScoreItem hsi;
hsi.name = name;
@@ -130,7 +133,7 @@ int main()
try {
HighScore hi ("", "highscore.txt");
hi.load();
- double new_score = 0.9;
+ int new_score = 9000;
if (hi.reachedNewHighscore(new_score))
{
std::cout << "Reached new highscore" << std::endl;
diff --git a/game/highscore.hh b/game/highscore.hh
index ea50b93..032eb14 100644
--- a/game/highscore.hh
+++ b/game/highscore.hh
@@ -20,7 +20,7 @@ struct HighScoreException: public std::runtime_error {
single item of a highscore.*/
struct HighScoreItem {
std::string name;
- double score;
+ int score;
// std::string song;
/**Operator for sorting by score.*/
@@ -40,15 +40,18 @@ class HighScore {
void save();
/**Check if you reached a new highscore.
+ @param score is a value between 0 and 10000
+ values below 500 will lead to returning false
@return true if the score make it into the top.
@return false if addNewHighscore does not make sense
for that score.*/
- bool reachedNewHighscore(double score)
+ bool reachedNewHighscore(int score)
{
- return score > m_scores.back().score;
+ if (score < 500) return false;
+ return score > m_scores[2].score;
}
/**Add a new entry to the highscore.*/
- void addNewHighscore(std::string name, double score);
+ void addNewHighscore(std::string name, int score);
private:
static const int m_maxEntries = 10;
std::string m_path;
|