-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighScore.cpp
More file actions
53 lines (52 loc) · 1.13 KB
/
HighScore.cpp
File metadata and controls
53 lines (52 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "HighScore.h"
#include <fstream>
#include "Player.h"
namespace MemoryGame
{
void HighScore::saveHighScore(Player* player)
{
std::ofstream outFile("highscores.txt", std::ios::app);
if (!outFile)
{
throw std::runtime_error("Could not open file");
}
if (player->getPlayerName() != "") {
outFile << player->getPlayerName() << " " << player->getPlayerStep() << std::endl;
}
outFile.close();
}
void HighScore::readHighScores()
{
std::ifstream inFile("highscores.txt", std::ios::in);
if (!inFile)
{
throw std::runtime_error("Could not open file");
}
std::string playerName;
int step;
while (inFile >> playerName >> step)
{
Player player;
player.setPlayerName(playerName);
player.setPlayerStep(step);
scores.push_back(player);
}
inFile.close();
}
std::vector<Player> HighScore::getPlayersScore()
{
for (int i = 0; i < scores.size(); i++)
{
for (int j = i + 1; j < scores.size(); j++)
{
if (scores[i].getPlayerStep() > scores[j].getPlayerStep())
{
Player temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
}
}
}
return scores;
}
}