HighscoreManager
This week i have been working on updating a previous version of our high score system. The first version of the system worked in three steps. The first step was to load a .txt file and read different scores such as ”1000” etc. The second step was to sort all of the scores with the highest score first. The third step was to add a new high score and check if the new score mad e it to the high score list.
The new system works similar but with some different results.
The new system works by connecting a score to a name. By having a struct called ScoreEntry i could easily connect a name and a score. This is how the struct looks like:
struct ScoreEntry
{
//Stores the users name
std::string name;
//Stores the users score
int score;
//Returns a string that connects the name and score as one string
std::string GetText()
{
return (name + " " + std::to_string(score));
}
};
So the highscore works by having a vector (a list) that holds X number of these ScoreEntry structs. The first element in the vector is equal to the player with highest score. By having them sorted in the vector it gets easier to draw all the user scores in score order later on.
Loading the highscores
When the game starts the old high scores are loaded by opening a .txt file where the previous scores are saved. Then i read a name and a score from the file and connecting them by creating a ScoreEntry struct and then adding he struct to the vector which holds all the other entries.
while (!file.eof())
{
ScoreEntry entry;
file >> entry.name;
file >> entry.score;
m_entries.push_back(entry);
}
Adding a new high score
When adding a new high score i check each existing high score in the vector and compare the new score with the score in the loop. If the new score is greater than the current one in the loop i add the new entry at that index in the vector which results in pushing each other index down in the list. What this means is that the score sorts itself when adding a new score. I don’t have to sort the whole vector using bubblesorting or something similar.
This is a short example of how this works…. (Not the full code for writing)
for (int i = 0; i < m_entries.size(); i++)
{
//Compare new entries score and the old ones
if (entry.score > m_entries[i].score)
{
//Add new score one step above the last score
m_entries.insert(m_entries.begin() + i, entry);
}
//Write entry to file
file << m_entries[i].name;
file << " " << m_entries[i].score;
}
Displaying the scores
As i said before, the list automatically sorts itself. So when i loop through all the entries i can easily just draw them in their index order to draw the with the highest score at place 1 and the next one on place 2 and so on.
I use the index as the number of which the player is on the highscore list. The user at index 0 will have a ”1.” next to its name and so on…
Example: ”1. David 5731”
for (int i = 0; i < m_entries.size(); i++)
{
m_text.setString(std::to_string(i + 1) + ". " + m_entries[i].GetText());
m_text.setPosition(m_position.x, m_position.y + i * 45);
drawManager->Draw(m_text, sf::RenderStates::Default);
}
The result is this